Skip to content

fix: hash link to new page focuses the correct element #10856

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

Merged

Conversation

eltigerchino
Copy link
Member

@eltigerchino eltigerchino commented Oct 10, 2023

fixes #3770

As suggested by mrkishi and jasonlyu:

This PR replaces the current scrollIntoView() with window.location.replace to scroll to and focus the deep linked element correctly since location.replace is almost 1:1 behaviour with natively clicking a hash link in the browser.

https://stackblitz.com/edit/sveltejs-kit-template-default-pxskqg?file=src%2Froutes%2F%2Blayout.svelte

Note:
Firefox has a bug where the history.state is set to null if we use location.replace with a hash that's the same as the current url hash. We circumvent this by manually restoring the history state after the fact.

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@changeset-bot
Copy link

changeset-bot bot commented Oct 10, 2023

🦋 Changeset detected

Latest commit: e73396f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@benmccann
Copy link
Member

@s3812497 the tests seem to be having an issue on this PR. I reran them to check if it's a fluke, but lots are still failing

@eltigerchino
Copy link
Member Author

Thanks for catching this. Sorry, looks like my test was a fluke. It doesn't seem to work on firefox and safari. Back to the drawing board...

@eltigerchino eltigerchino deleted the fix-hash-link-to-new-page-focus branch October 11, 2023 11:51
@eltigerchino
Copy link
Member Author

eltigerchino commented Oct 11, 2023

@benmccann turns out it was an implementation issue. The focus was getting reset to the body after scrolling and focusing to the fragment identifier. It should work correctly now.

@eltigerchino
Copy link
Member Author

eltigerchino commented Oct 12, 2023

Great. Now Firefox is being inconsistent. We might have to go with option 1 or 2:

  1. Make the target focusable, focus it and revert it. the downside is it would cause the focus ring to show up. And it's not the same as the normal hash link behaviour. But it doesn't create any dummy element.
  2. Append a dummy element before the target element if the target element isn't focusable. focus it and remove it.

It would also trigger the focus, focusin, blur, focusout events, and possibly affect styles because of the extra HTML element...
...but it works in all three browsers.

@benmccann benmccann marked this pull request as draft October 25, 2023 21:51
@eltigerchino
Copy link
Member Author

eltigerchino commented May 22, 2024

Found a fix(?) for the issue where Firefox sets the history state to null if we call location.replace but with the same hash as before. This is also a potential alternative to #10032

Here's the link to the bug in the Firefox issue tracker https://bugzilla.mozilla.org/show_bug.cgi?id=1199924

@benmccann
Copy link
Member

Here's the link to the bug in the Firefox issue tracker https://bugzilla.mozilla.org/show_bug.cgi?id=1199924

9 years ago - so we probably shouldn't wait for them to fix this 😆 Although they did just fix a 25 yr old bug https://news.ycombinator.com/item?id=40431444

@Rich-Harris
Copy link
Member

preview: https://svelte-dev-git-preview-kit-10856-svelte.vercel.app/

this is an automated message

@elliott-with-the-longest-name-on-github elliott-with-the-longest-name-on-github merged commit 6261a87 into main Jun 5, 2025
14 checks passed
@elliott-with-the-longest-name-on-github elliott-with-the-longest-name-on-github deleted the fix-hash-link-to-new-page-focus branch June 5, 2025 17:33
@github-actions github-actions bot mentioned this pull request Jun 5, 2025
eltigerchino added a commit that referenced this pull request Jun 6, 2025
dummdidumm pushed a commit that referenced this pull request Jun 6, 2025
…ing hash router (#13861)

This PR fixes a regression caused by #10856 which broke hash router navigation.

We can't apply the same fix for hash routing because location.replace doesn't work since the browser interprets the entire hash as the element it should focus but we're using it as a pathname replacement. element.focus() doesn't work on Safari and Ubuntu Firefox so that's moot too.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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