From 05b702938f64714a3a635db09b1065a125572c69 Mon Sep 17 00:00:00 2001 From: Daniil Khanin Date: Fri, 5 Oct 2018 00:13:08 +0300 Subject: [PATCH] implement sapper no scroll --- templates/src/client/app.ts | 7 ++++--- templates/src/client/start/index.ts | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/templates/src/client/app.ts b/templates/src/client/app.ts index 9456a63..36018ad 100644 --- a/templates/src/client/app.ts +++ b/templates/src/client/app.ts @@ -106,16 +106,17 @@ export function scroll_state() { }; } -export function navigate(target: Target, id: number): Promise { +export function navigate(target: Target, id: number, has_scroll = false): Promise { if (id) { // popstate or initial navigation cid = id; } else { + const current_scroll = scroll_state() // clicked on a link. preserve scroll state - scroll_history[cid] = scroll_state(); + scroll_history[cid] = current_scroll; id = cid = ++uid; - scroll_history[cid] = { x: 0, y: 0 }; + scroll_history[cid] = has_scroll ? current_scroll : { x: 0, y: 0 }; } cid = id; diff --git a/templates/src/client/start/index.ts b/templates/src/client/start/index.ts index 240fdd8..a34f6b2 100644 --- a/templates/src/client/start/index.ts +++ b/templates/src/client/start/index.ts @@ -102,8 +102,9 @@ function handle_click(event: MouseEvent) { if (url.pathname === location.pathname && url.search === location.search) return; const target = select_route(url); + const has_scroll = a.hasAttribute('sapper-noscroll') if (target) { - navigate(target, null); + navigate(target, null, has_scroll); event.preventDefault(); history.pushState({ id: cid }, '', url.href); }