Better error message when the scrollBehavior selector is invalid
See original GitHub issueWhat problem does this feature solve?
Currently if you pass a hash id with unescaped CSS special characters /[!"#$%&'()*+,\-./:;<=>?@[\\]^{|}~]/
to a scrollBehavior selector, execution will fail:
'XXX' is not a valid selector
Because of this url-styled hashes (e.g. "#one/two"
or "#main/secondary"
), both valid ids in HTML5, cannot be passed directly from the to.hash
or from.hash
parameters.
This is because behind the scenes document.querySelector
, which relies on CSS character compliance, is used in all but a very small number of circumstances (i.e. /^#\d/
). This feature proposes broadening the use case for document.getElementById
to all selectors that have the appearance of an id, specifically ungrouped selectors starting with a hash (i.e. /^#[^, ]+$/
)
What does the proposed API look like?
API doesn’t change, except the following would work:
scrollBehavior(to, from, savedPosition) {
return {
selector: to.hash,
}
}
// where:
window.location = '/one/two#three/four';
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Scroll behaviour VueJS not working properly - Stack Overflow
Without transition, the scrollBehavior return {selector: to.hash} works fine, since the content is instantly mounted, and the anchor exists in ...
Read more >scroll-behavior - CSS: Cascading Style Sheets - MDN Web Docs
The scroll-behavior CSS property sets the behavior for a scrolling box when scrolling is triggered by the navigation or CSSOM scrolling APIs ...
Read more >click - Cypress Documentation
Arguments ; multiple, false, Serially click multiple elements ; scrollBehavior, scrollBehavior, Viewport position to where an element should be scrolled before ...
Read more >How to scroll to the first invalid control once a form has been ...
Believe it or not, but it's not a good idea to disable a submit button ... immediately what went wrong, because an error...
Read more >CSS scroll-behavior property - W3Schools
The scroll-behavior property specifies whether to smoothly animate the scroll position, instead of a straight jump, when the user clicks on a link...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Glad to hear it makes more sense! The docs should indeed note that
querySelector
is used behind the scenes and that it doesn’t always work.to be clear it would be
el: document.getElementById(to.hash.slice(1))
, but that would work without escaping the hash!Another possibility would be to always use an
document.getElementById
ifel.startsWith('#')
and write a specific warning if no element is found that shows thatgetElementById
is used whenel
starts with#
as well as pointing out that we can useel: document.querySelector(to.hash)
(which is less boilerplate than the one withslice
). I think this option makes the most sense in the end because it will still allow to just doel: to.hash
, will work in most scenarios without the user needing to escape the CSS selector and will still warn when failing providing a way to make it work no matter what.I will write an RFC for this, it’s worth one
Vue Router doesn’t replicate the hash scrolling from the browser, we use
scrollTo
to scroll to the position passed by the user and provideselector
for convenience.Note the fragment of the url has multiple use cases, anchoring to an id of the page is the most common one but only one of them.
I think it would be better to deprecate
selector
and introduce a newel
option. I updated the comment above, let me know what you think!