Skip to content

Commit

Permalink
persist scroll state
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 15, 2023
1 parent 508ed69 commit 54e6b43
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
16 changes: 16 additions & 0 deletions src/routes/tutorial/[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@
history.pushState({}, '', `?${q}`);
}
}
/** @type {HTMLElement} */
let sidebar;
/** @type {import('./$types').Snapshot<number>} */
export const snapshot = {
capture: () => {
const scroll = sidebar.scrollTop;
sidebar.scrollTop = 0;
return scroll;
},
restore: (scroll) => {
sidebar.scrollTop = scroll;
}
};
</script>
<svelte:head>
Expand Down Expand Up @@ -164,6 +179,7 @@
<SplitPane id="main" type="horizontal" min="360px" max="50%" pos="33%">
<section slot="a" class="content">
<Sidebar
bind:sidebar
index={data.index}
exercise={data.exercise}
on:select={(e) => {
Expand Down
14 changes: 3 additions & 11 deletions src/routes/tutorial/[slug]/Sidebar.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script>
import { createEventDispatcher } from 'svelte';
import { afterNavigate } from '$app/navigation';
import Modal from '$lib/components/Modal.svelte';
import Menu from './Menu.svelte';
Expand All @@ -10,22 +9,15 @@
/** @type {import('$lib/types').Exercise} */
export let exercise;
/** @type {HTMLElement} */
export let sidebar;
const dispatch = createEventDispatcher();
const namespace = 'learn.svelte.dev';
const copy_enabled = `${namespace}:copy_enabled`;
/** @type {HTMLElement} */
let sidebar;
let show_modal = false;
afterNavigate(async () => {
// TODO ideally we would associate scroll state with
// history. That's a little tricky to do right now,
// so for now just always reset sidebar scroll
sidebar.scrollTop = 0;
});
</script>

<Menu {index} current={exercise} />
Expand Down

0 comments on commit 54e6b43

Please sign in to comment.