Hash links to new pages don't focus the correct element
See original GitHub issueDescribe the bug
If you click a link like <a href="#foo">
, the browser will focus an element like <h2 id="foo">
, which in effect (since <h2>
elements aren’t typically focusable) actually means that pressing Tab
will focus the next focusable element after the <h2>
.
But if you click a link like <a href="/other#foo">
, SvelteKit will navigate to /other
and scroll to #foo
, but it won’t focus the element.
Reproduction
https://stackblitz.com/edit/sveltejs-kit-template-default-nrjdx2
Logs
No response
System Info
System:
OS: macOS 12.0.1
CPU: (10) arm64 Apple M1 Max
Memory: 79.22 MB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
npm: 8.1.2 - ~/.nvm/versions/node/v16.13.1/bin/npm
Browsers:
Chrome: 98.0.4758.80
Firefox: 96.0.3
Safari: 15.1
npmPackages:
@sveltejs/adapter-auto: workspace:* => 1.0.0-next.18
@sveltejs/kit: workspace:* => 1.0.0-next.260
@sveltejs/site-kit: ^2.0.2 => 2.0.2
svelte: ^3.43.0 => 3.44.2
Severity
serious, but I can work around it
Additional Information
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Hash Tag Links That Don't Headbutt The Browser Window
Using hash-tag links with a fixed position header can be problematic, as the element may be hidden underneath the header as the browser...
Read more >Google Chrome doesn't follow hash (#) pointers in the URL?
I understand that to navigate directly to a specific element on a page on uses a hash sign after the URL with the...
Read more ><a>: The Anchor element - HTML: HyperText Markup Language
The HTML element (or anchor element), with its href attribute, creates a hyperlink to web pages, files, email addresses, locations in the ...
Read more >page focus when hash does not exist on load - Stack Overflow
Try this after element is added var hash = location.hash; if (hash && hash.substr(1).length) { var hashLink = document.
Read more >Links in HTML documents - W3C
The href attribute makes this anchor the source anchor of exactly one link. Authors may also create an A element that specifies no...
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
Just spitballing, but would it be possible for us to manually set the hash after navigation? Something like:
href="/route#target-hash"
, so we capture the link and routehistory.pushState(state, '', '/route#target-hash')
we dohistory.pushState(state, '', '/route')
hash_navigating = true; location.replace('#target-hash');
(not actuallyhash_navigating
but something similar)The reproduction link is outdated, todos section of it is throwing error. @Rich-Harris , is this bug still exists?