-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
fix: hash link to new page focuses the correct element #10856
Conversation
🦋 Changeset detectedLatest commit: e73396f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
@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 |
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... |
@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. |
Great. Now Firefox is being inconsistent. We might have to go with option 1 or 2:
It would also trigger the focus, focusin, blur, focusout events, and possibly affect styles because of the extra HTML element... |
Found a fix(?) for the issue where Firefox sets the history state to Here's the link to the bug in the Firefox issue tracker https://bugzilla.mozilla.org/show_bug.cgi?id=1199924 |
|
preview: https://svelte-dev-git-preview-kit-10856-svelte.vercel.app/ this is an automated message |
6261a87
into
main
This reverts commit 6261a87.
…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.
fixes #3770
As suggested by mrkishi and jasonlyu:
This PR replaces the current
scrollIntoView()
withwindow.location.replace
to scroll to and focus the deep linked element correctly sincelocation.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 uselocation.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:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.