forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix scroll bailout logic when targeting fixed/sticky elements (vercel…
…#53873) ### What? When navigating to a new page with fixed or sticky positioned element as the first element, we were bailing on scroll to top behavior, which often isn't expected. ### Why? Currently, we decide to bail on scroll to top behavior on navigation if the content that is swapped into view is visible within the viewport. Since fixed/sticky positioned elements are often intended to be relative to the current viewport, it's most likely not the case that you'd want it to be considered in this heuristic. For example, if you were scrolled far down on a page, and you navigated to a page that makes use of a sticky header, you would not be scrolled to the top of the page because that sticky header is technically visible within the viewport. ### How? I've updated the previous implementation that was intended to skip targeting invisible elements to also skip over fixed or sticky elements. This should help by falling back to the next level of the layout tree to determine which element to scroll to. I've deleted the `// TODO-APP` comments as I couldn't think of a scenario in which we'd need a global scrollTop handler -- if we've bailed on every element up the tree, it's likely the page wasn't scrollable. Some additional considerations: - Is the warning helpful or annoying? - Is the parallel route trade-off an acceptable one? (ie, a parallel modal slot might not be considered in the content visibility check unless if it’s fixed positioned) Closes NEXT-1393 Fixes vercel#47475
- Loading branch information
Showing
6 changed files
with
130 additions
and
25 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
34 changes: 18 additions & 16 deletions
34
test/e2e/app-dir/parallel-routes-and-interception/app/parallel-scroll/@modal/nav/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 |
---|---|---|
@@ -1,21 +1,23 @@ | ||
export default function Page() { | ||
return ( | ||
<div | ||
style={{ | ||
position: 'fixed', | ||
top: 0, | ||
left: 0, | ||
width: '100%', | ||
height: '100%', | ||
backgroundColor: 'rgba(0, 0, 0, 0.5)', | ||
display: 'flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
zIndex: 1000, | ||
}} | ||
id="modal" | ||
> | ||
MODAL | ||
<div> | ||
<div | ||
style={{ | ||
position: 'fixed', | ||
top: 0, | ||
left: 0, | ||
width: '100%', | ||
height: '100%', | ||
backgroundColor: 'rgba(0, 0, 0, 0.5)', | ||
display: 'flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
zIndex: 1000, | ||
}} | ||
id="modal" | ||
> | ||
MODAL | ||
</div> | ||
</div> | ||
) | ||
} |
16 changes: 16 additions & 0 deletions
16
test/e2e/app-dir/router-autoscroll/app/fixed-first-element/page.tsx
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,16 @@ | ||
export default function FixedFirstElementPage() { | ||
return ( | ||
<> | ||
<nav style={{ position: 'fixed' }}>Fixed nav bar</nav> | ||
<div id="content-that-is-visible" style={{ paddingTop: 40 }}> | ||
Content which is not hidden. | ||
</div> | ||
{ | ||
// Repeat 500 elements | ||
Array.from({ length: 500 }, (_, i) => ( | ||
<div key={i}>{i}</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
16 changes: 16 additions & 0 deletions
16
test/e2e/app-dir/router-autoscroll/app/sticky-first-element/page.tsx
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,16 @@ | ||
export default function FixedFirstElementPage() { | ||
return ( | ||
<> | ||
<nav style={{ position: 'sticky', top: 0 }}>Sticky nav bar</nav> | ||
<div id="content-that-is-visible" style={{ paddingTop: 40 }}> | ||
Content which is not hidden. | ||
</div> | ||
{ | ||
// Repeat 500 elements | ||
Array.from({ length: 500 }, (_, i) => ( | ||
<div key={i}>{i}</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