feat(site): preserve scroll position on framework switch (pagereveal) (#608)

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Darius Cepulis
2026-02-25 14:29:54 -06:00
committed by GitHub
co-authored by Claude Opus 4.6
parent 1c6293ea39
commit 420742e7c9
3 changed files with 63 additions and 28 deletions
@@ -17,7 +17,6 @@ interface Props {
}
export default function JSPickerClient({ currentFramework, currentStyle, currentSlug }: Props) {
// TODO: use astro view transitions to preserve scroll position when switching from the same slug to the same slug
const handleFrameworkChange = (newFramework: SupportedFramework | null) => {
if (newFramework === null) return;
if (!isValidFramework(newFramework)) return;
@@ -30,10 +29,20 @@ export default function JSPickerClient({ currentFramework, currentStyle, current
});
if (shouldReplace) {
// Maintaining the current slug, navigate without pushing onto the history stack
// Base UI's scroll lock transfers html.scrollTop → body.scrollTop
const scrollLocked = document.documentElement.hasAttribute('data-base-ui-scroll-locked');
const scrollY = scrollLocked ? document.body.scrollTop : window.scrollY;
try {
sessionStorage.setItem(
'vjs-page-scroll',
JSON.stringify({ url: new URL(url, window.location.origin).pathname, scrollY })
);
} catch {
// Ignore storage errors
}
window.location.replace(url);
} else {
// Changing slug, use normal navigation
window.location.href = url;
}
};