jonaskuske / smoothscroll-anchor-polyfill

βš“ Apply smooth scroll to anchor links, polyfill scroll-behavior
https://jonaskuske.github.io/smoothscroll-anchor-polyfill
MIT License
41 stars 3 forks source link

Bailout if defaultPrevented === true #32

Closed jonaskuske closed 4 years ago

jonaskuske commented 4 years ago
<a id="next" href="#">Next slide</a>
<script>
  document.querySelector('#next').addEventListener('click', e => e.preventDefault())
</script>

In browsers with native support, clicking the anchor from the example above does not scroll the page – .preventDefault() stops the default href="#" scroll behavior.

smoothscroll-anchor-polyfill currently ignores this, its clickHandler starts scrolling even if event.defaultPrevented === true. Needs fix.

iamkeir commented 4 years ago

Just encountered this too - is there a short-term workaround before it is officially fixed/released?

jonaskuske commented 4 years ago

@iamkeir Yep, change line 264

from this:

      if (evt.metaKey || evt.ctrlKey || evt.shiftKey || evt.button !== 0) return;

to this

      if (evt.metaKey || evt.ctrlKey || evt.shiftKey || evt.button !== 0 || evt.defaultPrevented) return;

and you should be good :)

And I'll write a test for it and release the new version this week. πŸ‘πŸ»

jonaskuske commented 4 years ago

@iamkeir Fixed and published :)

iamkeir commented 4 years ago

You’re the best!! Thank you for such a swift fix. Happy Easter.

iamkeir commented 4 years ago

I'm being a bit special... where can I download the new dist version?

iamkeir commented 4 years ago

Ah, got it: https://unpkg.com/smoothscroll-polyfill/dist/smoothscroll.min.js https://unpkg.com/smoothscroll-anchor-polyfill

jonaskuske commented 4 years ago

Yep! If you don't get the latest version, specify it explicitly with https://unpkg.com/smoothscroll-anchor-polyfill@1.3.2 – otherwise unpkg might still give you an older version due to caching. This way you also won't run into trouble if a v2 with breaking changes is published at some point.
(I should also update the docs to recommend specifying the version as part of the URL)

iamkeir commented 4 years ago

Thanks dude - I also wondered why the dist folder was empty? Anyway all good, thank you very much :) Do you have a tip jar?

jonaskuske commented 4 years ago

I also wondered why the dist folder was empty

The repository only contains the source code. When publishing the package to the npm registry, that source code is (more or less) automatically compiled into the dist files you can then use via unpkg and similar CDNs and registries. But they don't need to stay in the repo, the repo just includes the code and instructions necessary to predictably regenerate them.

And thanks, but nope, not looking for tips :)