Skip to content
This repository was archived by the owner on Nov 25, 2024. It is now read-only.

Commit 1103304

Browse files
gtm-nayanPuruVJ
andauthored
feat: expose a way to capture scroll state from shell (#216)
Co-authored-by: Puru Vijay <[email protected]>
1 parent 66a4aff commit 1103304

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

.changeset/bright-coats-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/site-kit': patch
3+
---
4+
5+
feat: expose a way to capture scroll state from Shell

packages/site-kit/src/lib/components/Shell.svelte

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,28 @@ The main shell of the application. It provides a slot for the top navigation, th
2222
2323
/** @type {HTMLElement} */
2424
let main_el;
25+
let scroll_restored = false;
2526
2627
afterNavigate(() => {
27-
main_el.scrollTop = 0;
28+
if (!scroll_restored) {
29+
main_el.scrollTop = 0;
30+
}
31+
scroll_restored = false;
2832
});
33+
34+
/** @type {import('@sveltejs/kit').Snapshot<number>} */
35+
export const snapshot = {
36+
capture() {
37+
return main_el.scrollTop;
38+
},
39+
restore(scroll_top) {
40+
main_el.scrollTop = scroll_top;
41+
42+
// Restore is not called for the first navigation to a page,
43+
// use this flag to track whether to reset the scroll to top or not in afterNavigate
44+
scroll_restored = true;
45+
}
46+
};
2947
</script>
3048

3149
<Icons />

0 commit comments

Comments
 (0)