swup / scroll-plugin

A swup plugin for smooth scrolling 🏄‍♂️
https://swup.js.org/plugins/scroll-plugin
MIT License
3 stars 8 forks source link

Allow to ignore the current page visit from the outside #52

Closed hirasso closed 1 year ago

hirasso commented 1 year ago

Related: #47

Description

For the new Fragment Plugin, we need a way to tell the Scroll Plugin to do nothing for the current page visit:

/**
 * Disable the scroll plugin for fragment visits
 */
disableScrollPluginForCurrentVisit() {
    // We still want scrolling if there is a hash in the target link
    if (this.swup.scrollToElement) return;

    const scrollPlugin = this.swup.findPlugin('ScrollPlugin');
    if (scrollPlugin) scrollPlugin.ignorePageVisit = true;
}

Doing this would also help with #47 I suppose:

swup.on('clickLink', (e) => {
  if (e.delegateTarget.matches('.ignore-me')) return;

  const scrollPlugin = swup.findPlugin('ScrollPlugin');
  if (scrollPlugin) scrollPlugin.ignorePageVisit = true;
})

~A bit verbose but the most flexible I could come up with to be able to use if from another plugin.~

Added an attribute check for clickLink. Now it's possible to ignore the current page visit using the attribute data-swup-preserve-scroll:

<!-- Scroll plugin will ignore the page visit started from a click on this link -->
<a href="/my-page/" data-swup-preserve-scroll>My Link</a>
hirasso commented 1 year ago

Alternative wordings to make it clearer that this is a one-off variable that will be reset after each visit:

hirasso commented 1 year ago

@daun, how would you implement this? There was some discussion about not using a flag and instead a callback as a plugin option. Would you like to take care of that? From the Fragment Plugin, we could then overwrite that option on-the-fly, and reset it after a fragment visit.

daun commented 1 year ago

I'd love to take a stab at passing along a context object for each visit that hook handlers can see and modify. Having a scroll flag on there would solve this beautifully. It'd have to wait until v4, though. If it can't wait, we could solve it via flag for now and forget all about in v4 🤠

hirasso commented 1 year ago

😄 Let's wait then. I hope that v4 is not too far away. Introducing a new option just to deprecate it again after 4 weeks sounds a bit too agile to me 🫢