Add scroll predicate support for dynamic scroll targets#94
Add scroll predicate support for dynamic scroll targets#94
Conversation
|
Thanks so much for the PR! I'm wondering if this might better be implemented in userland? The That said, I'm curious about your use case. Have you needed this across multiple projects? it might be worth discussing this in an issue first. What do you think? |
|
...I just realized that const swup = new Swup({
plugins: [
new SwupFragmentPlugin({
rules: [
{
from: "/blog",
to: "/blog/:slug",
containers: ["#main"],
name: "blog-detail", // give your rule a name
},
],
}),
],
hooks: {
"visit:start": (visit) => {
modifyVisitScroll(visit);
},
},
});
function modifyVisitScroll(visit: Visit) {
// optional: target only one specific type of fragment visit
if (visit.fragmentVisit?.name !== "blog-detail") return;
// Enable scrolling when searchParams changed
const fromParams = new URL(visit.from.url).searchParams;
const toParams = new URL(visit.to.url).searchParams;
// Enable scrolling when params changed. Just for illustration!
visit.scroll.reset = fromParams.toString() !== toParams.toString();
} |
|
Thanks for looking into this.
Just for one website, but would affect any website where navigation occurs to a URL with query parameters that should have different scroll behaviour based on the presence or absence of query parameters. Example navigation pattern (where overlay is rendered via the fragments plugin) These target urls will be handled by the same FragmentRule definition (it does not account for query params), but should have different scroll behaviour. While it’s possible to achieve this with hooks, having the scroll behaviour defined at the The Fragments plugin already supports setting the scroll option, having a scroll callback avoids needing conditional logic in the visit hook to check which rule matched before setting scroll behaviour. The code changes are also quite minimal and mimic what’s already in place for the |
|
You are right, the change really is minimal. And having all fragment-visit-related config in one place actually is a strong argument. Ready to merge this as soon as the README is updated :) |
The `visit.scroll` was already accessible via the visit parameter, so the separate `scroll` argument was unnecessary.
|
@hirasso finished this finally. Looks like the two task runners failed. Could you restart them? |
|
Published in version |
Description
Extends the
fragmentVisit.scrolloption with aScrollPredicatethat recieves theVisitobject to set the scroll behaviour dynamically.This can be usefull for routes with query parameters where the scroll should reset based on the presence of query parameters.
Checks
mainbranchnpm run lint)npm run test)Additional information