-
-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for anchor links #157
Comments
This is an interesting issue and I'd be willing to support looking for i agree that this would break links that want to point to an anchor. We could fix that for when people leverage the What do you think? |
Yes, sounds good! But I'm wondering if the following algorithm would be able to handle "same-page anchor-links":
|
wow... it actually works! + the default behavior still seems to work... sometimes. It still does create a race condition when using the Here's my prototype for now:loc.subscribe(async (newLoc) => {
// Find if the URL matches a same-page anchor link
if (newLoc.hash.match(/^#[^\/]\w*/)) {
if (!lastLoc) {
// Could be another behavior, but this one seems right to me
window.location.hash = '#'
return true // don't know if `true` is needed here
}
// Find the "page" (aka fake "location") of last URL
// Basically removes the anchor link part
const lastLocPage = lastLoc.hash.split('#')[1]
// The new URL should be the last page + the added anchor link part
window.location.hash = lastLocPage + newLoc.hash
return true // don't know if `true` is needed here
}
// Proceed like the rest of code
lastLoc = newLoc
// ... rest of function We would then insert the automatic scrolling (stated in the description) algorithm somewhere. To make this work we still need to manually (for now) add the regex to match the possible anchor link (something like |
Note that jumping to an element via a fragment does not only scroll the element into view, it also changes the tab order context to be that element. So the next time you press tab it will focus the first focusable element within or after the target element. This is helpful for accessibility features like "skip to content" links. I tried reproducing this behavior in JS but so far it turned out to be tricky. If you just try to |
@brunnerh You'll need to set the tab index of the target element to |
Unfortunately that is usually not what you want. Non-interactive elements that are just sections of a page are not supposed to be in the tab order of the document. Maybe one could set the tab index, focus the element and remove the tab index again, but unless that behaves correctly across browsers that is not a viable solution. |
Yep! That's a pretty common practice when doing keyboard navigation work in JS. |
I'm also looking into a solution—specifically to support the skip-to-main link. Right now clicking/activating skip-to-main link changes the url, which I don't enjoy, but more problematic is that it will load the default page if you aren't viewing it already. I see that @vigenere23 has a solution posted, and reports it works, but considering it about 7 months later is this the most current state of thinking for the solution? |
Good morning/evening ... has a solution been found ? I switched from PageJS to svelte-spa-router because I need hash based routing. Now my documentation anchors links dont work in SSrouter.. Also, If you paste a url in the browser it doesnt work at all |
Anchor links are the trailing
#
part in URL that usually corresponds to an ID on the page. The default and standard browser behavior is to automatically scroll to the element that corresponds to that id on page load. But since our app is an SPA, thehash
starts at the first#
symbol and thus preventing such behavior (ex:#/home#about
will try to scroll to element with id/home#about
instead ofabout
).Here is a way to achieve such a thing (would need to be inserted at the right place):
We would also need to add a suffix to the parsed path regex pattern (like
(#.*)?$
) to match the anchor link ending.On my side, when adding the suffix to the path and adding the code snippet inside a
routeLoaded()
event handler in my project, it works without any glitch (even withrestoreScrollState
activated).Known problem and possible solution
StandardSame-page anchor links may be problematic. Since the URL is often just the hash without the rest (like#about
), the browser's default behavior will the to replace the entire hash of the URL with this, thus breaking it. A possible solution would be to listen to location hash change and if the URL looks broken (aka with a#
not followed by a/
), then go back to previous route but with the anchor link appended (instead or replaced like the default behavior).The text was updated successfully, but these errors were encountered: