-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[fix] scroll to elements provided via URL hash #2668
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
benmccann
merged 9 commits into
sveltejs:master
from
mikenikles:mikenikles/2664-anchor-links-broken
Oct 29, 2021
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9dfd215
Scroll to elements provided via URL hash.
e823a2b
Fix the URL hash scroll behavior.
f1f2c98
Fix lint errors.
6a3b909
Add a test case where manual scrolling overwrites the URL hash.
2332af2
Update .changeset/wicked-plums-count.md
benmccann 906729e
Update packages/kit/src/runtime/client/renderer.js
mikenikles f7ba2b7
fix(router): update scroll and focus order
bluwy 6fece3e
chore: update suggestions
bluwy eec3dd7
chore: update test name
bluwy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@sveltejs/kit': patch | ||
| --- | ||
|
|
||
| [fix] scroll to elements provided via URL hash |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/kit/test/apps/basics/src/routes/anchor-with-manual-scroll/_tests.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import * as assert from 'uvu/assert'; | ||
|
|
||
| /** | ||
| * A suite of tests that checks that SvelteKit URL anchor is ignored when the | ||
| * onMount() lifecycle method contains custom scroll logic. | ||
| * @type {import('test').TestMaker} | ||
| */ | ||
| export default function (test) { | ||
| test( | ||
| 'url-supplied anchor is ignore with onMount() scrolling on direct page load', | ||
|
bluwy marked this conversation as resolved.
Outdated
|
||
| '/anchor-with-manual-scroll/anchor#go-to-element', | ||
| async ({ page, js }) => { | ||
| if (js) { | ||
| const p = await page.$('#abcde'); | ||
| assert.ok(p && (await p.isVisible())); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| test( | ||
| 'url-supplied anchor is ignored with onMount() scrolling on navigation to page', | ||
| '/anchor-with-manual-scroll', | ||
| async ({ page, clicknav, js }) => { | ||
| await clicknav('[href="/anchor-with-manual-scroll/anchor#go-to-element"]'); | ||
| if (js) { | ||
| const p = await page.$('#abcde'); | ||
| assert.ok(p && (await p.isVisible())); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
20 changes: 20 additions & 0 deletions
20
packages/kit/test/apps/basics/src/routes/anchor-with-manual-scroll/anchor.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <script> | ||
| import { onMount } from 'svelte'; | ||
|
|
||
| onMount(() => { | ||
| document.getElementById('abcde')?.scrollIntoView(); | ||
| }); | ||
| </script> | ||
|
|
||
| <div>They (don't) see me...</div> | ||
| <div style="background-color: peru;"> | ||
| <p id="go-to-element">The browser scrolls to me</p> | ||
| </div> | ||
| <p id="abcde">I take precedence</p> | ||
|
|
||
| <style> | ||
| div { | ||
| background-color: hotpink; | ||
| height: 180vh; | ||
| } | ||
| </style> |
15 changes: 15 additions & 0 deletions
15
packages/kit/test/apps/basics/src/routes/anchor-with-manual-scroll/index.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <h1>Welcome to a test project</h1> | ||
| <a href="/anchor-with-manual-scroll/anchor#go-to-element">Anchor demo</a> | ||
|
|
||
| <style> | ||
| :global(body) { | ||
| background-color: tan; | ||
| display: flex; | ||
| flex-direction: column; | ||
| } | ||
|
|
||
| a { | ||
| display: block; | ||
| margin: 20px; | ||
| } | ||
| </style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import * as assert from 'uvu/assert'; | ||
|
|
||
| /** | ||
| * A suite of tests that checks that SvelteKit URL anchor is respected and the | ||
| * element with the anchor ID is scrolled into view. | ||
| * @type {import('test').TestMaker} | ||
| */ | ||
| export default function (test) { | ||
| test( | ||
| 'url-supplied anchor works on direct page load', | ||
| '/anchor/anchor#go-to-element', | ||
| async ({ page, js }) => { | ||
| if (js) { | ||
| const p = await page.$('#go-to-element'); | ||
| assert.ok(p && (await p.isVisible())); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| test( | ||
| 'url-supplied anchor works on navigation to page', | ||
| '/anchor', | ||
| async ({ page, clicknav, js }) => { | ||
| await clicknav('[href="/anchor/anchor#go-to-element"]'); | ||
| if (js) { | ||
| const p = await page.$('#go-to-element'); | ||
| assert.ok(p && (await p.isVisible())); | ||
| } | ||
| } | ||
| ); | ||
| } |
11 changes: 11 additions & 0 deletions
11
packages/kit/test/apps/basics/src/routes/anchor/anchor.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <div>They (don't) see me...</div> | ||
| <div style="background-color: peru;"> | ||
| <p id="go-to-element">The browser scrolls to me</p> | ||
| </div> | ||
|
|
||
| <style> | ||
| div { | ||
| background-color: hotpink; | ||
| height: 180vh; | ||
| } | ||
| </style> |
15 changes: 15 additions & 0 deletions
15
packages/kit/test/apps/basics/src/routes/anchor/index.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <h1>Welcome to a test project</h1> | ||
| <a href="/anchor/anchor#go-to-element">Anchor demo</a> | ||
|
|
||
| <style> | ||
| :global(body) { | ||
| background-color: tan; | ||
| display: flex; | ||
| flex-direction: column; | ||
| } | ||
|
|
||
| a { | ||
| display: block; | ||
| margin: 20px; | ||
| } | ||
| </style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.