Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit b8a6bae

Browse files
committed
handle tag click from another page
1 parent abcac75 commit b8a6bae

File tree

5 files changed

+64
-20
lines changed

5 files changed

+64
-20
lines changed

templates/src/client/app.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,17 @@ export function scroll_state() {
106106
};
107107
}
108108

109-
export function navigate(target: Target, id: number, noscroll = false): Promise<any> {
109+
export function navigate(target: Target, id: number, scroll_to?: ScrollPosition | string): Promise<any> {
110+
let scroll: ScrollPosition | string;
110111
if (id) {
111112
// popstate or initial navigation
112113
cid = id;
114+
scroll = scroll_to ? scroll_to : scroll_history[id];
113115
} else {
114-
const current_scroll = scroll_state();
115-
116116
// clicked on a link. preserve scroll state
117-
scroll_history[cid] = current_scroll;
118-
117+
scroll_history[cid] = scroll_state();
119118
id = cid = ++uid;
120-
scroll_history[cid] = noscroll ? current_scroll : { x: 0, y: 0 };
119+
scroll = scroll_to ? scroll_to : { x: 0, y: 0 };
121120
}
122121

123122
cid = id;
@@ -137,13 +136,12 @@ export function navigate(target: Target, id: number, noscroll = false): Promise<
137136
if (redirect) {
138137
return goto(redirect.location, { replaceState: true });
139138
}
140-
141-
render(data, nullable_depth, scroll_history[id], token);
139+
render(data, nullable_depth, scroll, token);
142140
if (document.activeElement) document.activeElement.blur();
143141
});
144142
}
145143

146-
function render(data: any, nullable_depth: number, scroll: ScrollPosition, token: {}) {
144+
function render(data: any, nullable_depth: number, scroll: ScrollPosition | string, token: {}) {
147145
if (current_token !== token) return;
148146

149147
if (root_component) {
@@ -183,7 +181,18 @@ function render(data: any, nullable_depth: number, scroll: ScrollPosition, token
183181
}
184182

185183
if (scroll) {
186-
scrollTo(scroll.x, scroll.y);
184+
let scrollPos: ScrollPosition;
185+
if (typeof scroll === 'string') {
186+
// scroll is an element id (from a hash), we need to compute y.
187+
const deep_linked = document.getElementById(scroll);
188+
scrollPos = deep_linked ?
189+
{ x: 0, y: deep_linked.getBoundingClientRect().top } :
190+
scroll_state();
191+
} else {
192+
scrollPos = scroll;
193+
}
194+
scroll_history[cid] = scrollPos;
195+
scrollTo(scrollPos.x, scrollPos.y);
187196
}
188197

189198
Object.assign(root_props, data);

templates/src/client/start/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
set_cid
1414
} from '../app';
1515
import prefetch from '../prefetch/index';
16-
import { Store } from '../types';
16+
import { Store, ScrollPosition } from '../types';
1717

1818
export default function start(opts: {
1919
target: Node,
@@ -35,17 +35,13 @@ export default function start(opts: {
3535

3636
return Promise.resolve().then(() => {
3737
const { hash, href } = location;
38-
39-
const deep_linked = hash && document.getElementById(hash.slice(1));
40-
scroll_history[uid] = deep_linked ?
41-
{ x: 0, y: deep_linked.getBoundingClientRect().top } :
42-
scroll_state();
38+
const scroll_to = hash ? hash.slice(1) : scroll_state();
4339

4440
history.replaceState({ id: uid }, '', href);
4541

4642
if (!initial_data.error) {
4743
const target = select_route(new URL(location.href));
48-
if (target) return navigate(target, uid);
44+
if (target) return navigate(target, uid, scroll_to);
4945
}
5046
});
5147
}
@@ -104,7 +100,16 @@ function handle_click(event: MouseEvent) {
104100
const target = select_route(url);
105101
if (target) {
106102
const noscroll = a.hasAttribute('sapper-noscroll');
107-
navigate(target, null, noscroll);
103+
let scroll_to: ScrollPosition | string;
104+
if (noscroll) {
105+
scroll_to = scroll_state();
106+
} else if (url.hash) {
107+
scroll_to = url.hash.slice(1);
108+
} else {
109+
scroll_to = { x: 0, y: 0 };
110+
}
111+
112+
navigate(target, null, scroll_to);
108113
event.preventDefault();
109114
history.pushState({ id: cid }, '', url.href);
110115
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
<div style="height: 9999px"></div>
1+
<div style="height: 9999px"></div>
2+
<p id="bar">element</p>

test/apps/scroll/src/routes/tall-page.html

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,21 @@
33
<div id="foo">
44
<a href="another-tall-page">link</a>
55
<a href="another-tall-page" sapper-noscroll>link</a>
6-
</div>
6+
{#if barLink}
7+
<a href="another-tall-page#bar">link</a>
8+
{/if}
9+
</div>
10+
11+
<script>
12+
export default {
13+
data() {
14+
return {
15+
barLink: false
16+
};
17+
},
18+
19+
oncreate() {
20+
this.set({ barLink: true })
21+
}
22+
}
23+
</script>

test/apps/scroll/test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,16 @@ describe('scroll', function() {
5959

6060
assert.ok(scrollY > 0);
6161
});
62+
63+
it('scrolls into a deeplink on a new page', async () => {
64+
await page.goto(`${base}/tall-page#foo`);
65+
await start();
66+
await prefetchRoutes();
67+
68+
await page.click('[href="another-tall-page#bar"]');
69+
await wait(50);
70+
71+
const scrollY = await page.evaluate(() => window.scrollY);
72+
assert.ok(scrollY > 0);
73+
});
6274
});

0 commit comments

Comments
 (0)