Skip to content

Commit ebc6a23

Browse files
committed
fix cross page anchor navigation scrolling and mobile cross page navigation scroll positioning
1 parent 8827283 commit ebc6a23

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

packages/dev/s2-docs/src/client.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,25 @@ let updateRoot = hydrate({
1616
// update the URL in the browser.
1717
async function navigate(pathname: string, push = false) {
1818
let res = fetchRSC<ReactElement>(pathname.replace('.html', '.rsc'));
19+
let currentPath = location.pathname;
20+
let [newBasePath, newPathAnchor] = pathname.split('#');
21+
1922
updateRoot(res, () => {
2023
if (push) {
2124
history.pushState(null, '', pathname);
2225
push = false;
2326
}
27+
28+
// Reset scroll if navigating to a different page without an anchor, primarily for the mobile case.
29+
// Otherwise, make sure to scroll the anchor into view if any
30+
if (currentPath !== newBasePath && !newPathAnchor) {
31+
window.scrollTo(0, 0);
32+
} else if (newPathAnchor) {
33+
let element = document.getElementById(newPathAnchor);
34+
if (element) {
35+
element.scrollIntoView();
36+
}
37+
}
2438
});
2539
}
2640

0 commit comments

Comments
 (0)