Skip to content

Commit

Permalink
persist scroll state (#542)
Browse files Browse the repository at this point in the history
Co-authored-by: Rich Harris <[email protected]>
  • Loading branch information
Rich-Harris and Rich-Harris authored Dec 15, 2023
1 parent 508ed69 commit baca1cd
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

1 comment on commit baca1cd

@vercel
Copy link

@vercel vercel bot commented on baca1cd Dec 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.