question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hash links to new pages don't focus the correct element

See original GitHub issue

Describe 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:open
  • Created 2 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mrkishicommented, Mar 11, 2022

Just spitballing, but would it be possible for us to manually set the hash after navigation? Something like:

  • User clicks href="/route#target-hash", so we capture the link and route
  • Instead of history.pushState(state, '', '/route#target-hash') we do history.pushState(state, '', '/route')
  • Render route
  • hash_navigating = true; location.replace('#target-hash'); (not actually hash_navigating but something similar)
0reactions
mefabacommented, Oct 29, 2022

The reproduction link is outdated, todos section of it is throwing error. @Rich-Harris , is this bug still exists?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found