From 28cb64b2e0e7695bc45c9592cdcf25424ccdbf3a Mon Sep 17 00:00:00 2001 From: Nathaniel Daniel Date: Thu, 12 May 2022 22:59:39 -0700 Subject: [PATCH] Expose function to manually restore scroll state --- Router.svelte | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Router.svelte b/Router.svelte index ece9f4d..7741d80 100644 --- a/Router.svelte +++ b/Router.svelte @@ -192,6 +192,22 @@ export function link(node, opts) { } } +/** + * Tries to restore the scroll state from the given history state. + * + * @param {object} state - The history state to restore from. + */ +export function restoreScroll(state) { + // If this exists, then this is a back navigation: restore the scroll position + if (state) { + window.scrollTo(state.__svelte_spa_router_scrollX, state.__svelte_spa_router_scrollY) + } + else { + // Otherwise this is a forward navigation: scroll to top + window.scrollTo(0, 0) + } +} + // Internal function used by the link function function updateLink(node, opts) { let href = opts.href || node.getAttribute('href') @@ -470,14 +486,7 @@ if (restoreScrollState) { window.addEventListener('popstate', popStateChanged) afterUpdate(() => { - // If this exists, then this is a back navigation: restore the scroll position - if (previousScrollState) { - window.scrollTo(previousScrollState.__svelte_spa_router_scrollX, previousScrollState.__svelte_spa_router_scrollY) - } - else { - // Otherwise this is a forward navigation: scroll to top - window.scrollTo(0, 0) - } + restoreScroll(previousScrollState); }) }