From 54e6b43ac7fdaff94115940bc84fca7c9b74c9dc Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 15 Dec 2023 15:51:09 -0500 Subject: [PATCH] persist scroll state --- src/routes/tutorial/[slug]/+page.svelte | 16 ++++++++++++++++ src/routes/tutorial/[slug]/Sidebar.svelte | 14 +++----------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/routes/tutorial/[slug]/+page.svelte b/src/routes/tutorial/[slug]/+page.svelte index 1c660f572..144581bff 100644 --- a/src/routes/tutorial/[slug]/+page.svelte +++ b/src/routes/tutorial/[slug]/+page.svelte @@ -123,6 +123,21 @@ history.pushState({}, '', `?${q}`); } } + + /** @type {HTMLElement} */ + let sidebar; + + /** @type {import('./$types').Snapshot} */ + export const snapshot = { + capture: () => { + const scroll = sidebar.scrollTop; + sidebar.scrollTop = 0; + return scroll; + }, + restore: (scroll) => { + sidebar.scrollTop = scroll; + } + }; @@ -164,6 +179,7 @@
{ diff --git a/src/routes/tutorial/[slug]/Sidebar.svelte b/src/routes/tutorial/[slug]/Sidebar.svelte index 9c16fb68d..63fbb2838 100644 --- a/src/routes/tutorial/[slug]/Sidebar.svelte +++ b/src/routes/tutorial/[slug]/Sidebar.svelte @@ -1,6 +1,5 @@