-
Notifications
You must be signed in to change notification settings - Fork 26k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: app router hash scrolling should respect scroll-padding (#51268)
When navigating to a route with a hash parameter, the layout router jumps to the element by scrolling to the node's `offsetTop` value. However, this will ignore `scroll-padding`, which deviates from browser behavior. It looks like this isn't an issue in the pages router which currently makes use of [`scrollIntoView`](https://github.com/vercel/next.js/blob/canary/packages/next/src/shared/lib/router/router.ts#L2262). Closes NEXT-1171 Fixes #49612 ---------
- Loading branch information
Showing
4 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
8 changes: 8 additions & 0 deletions
8
test/e2e/app-dir/navigation/app/hash-with-scroll-offset/global.css
This file contains 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,8 @@ | ||
* { | ||
margin: 0; | ||
padding: 0; | ||
box-sizing: border-box; | ||
font-size: 14px; | ||
line-height: 1; | ||
scroll-padding-top: 20px; | ||
} |
41 changes: 41 additions & 0 deletions
41
test/e2e/app-dir/navigation/app/hash-with-scroll-offset/page.js
This file contains 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,41 @@ | ||
import Link from 'next/link' | ||
import './global.css' | ||
|
||
export default function HashPage() { | ||
// Create list of 5000 items that all have unique id | ||
const items = Array.from({ length: 5000 }, (_, i) => ({ id: i })) | ||
|
||
return ( | ||
<div style={{ fontFamily: 'sans-serif', fontSize: '16px' }}> | ||
<p>Hash Page</p> | ||
<Link href="/hash#hash-6" id="link-to-6"> | ||
To 6 | ||
</Link> | ||
<Link href="/hash#hash-50" id="link-to-50"> | ||
To 50 | ||
</Link> | ||
<Link href="/hash#hash-160" id="link-to-160"> | ||
To 160 | ||
</Link> | ||
<Link href="/hash#hash-300" id="link-to-300"> | ||
To 300 | ||
</Link> | ||
<Link href="#hash-500" id="link-to-500"> | ||
To 500 (hash only) | ||
</Link> | ||
<Link href="/hash#top" id="link-to-top"> | ||
To Top | ||
</Link> | ||
<Link href="/hash#non-existent" id="link-to-non-existent"> | ||
To non-existent | ||
</Link> | ||
<div> | ||
{items.map((item) => ( | ||
<div key={item.id}> | ||
<div id={`hash-${item.id}`}>{item.id}</div> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
) | ||
} |
This file contains 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