Compare commits

...

6 Commits

Author SHA1 Message Date
Rich Harris
4fdc7055c1 -> v0.23.1 2018-10-15 22:10:18 -04:00
Rich Harris
cca417a85a simplify, preserve monomorphism 2018-10-15 22:07:23 -04:00
Rich Harris
635c13a175 Merge branch 'tags-from-other-pages' of https://github.com/DayBr3ak/sapper into DayBr3ak-tags-from-other-pages 2018-10-15 21:43:39 -04:00
Rich Harris
2e3aef8b21 simplify 2018-10-15 21:36:40 -04:00
Benjamin GROENEVELD
a399d87d9b handle tag click from another page 2018-10-13 21:59:47 +02:00
Benjamin GROENEVELD
a68c62ce91 Fix hash link reliability (fix #434) 2018-10-13 16:45:14 +02:00
7 changed files with 78 additions and 18 deletions

View File

@@ -1,5 +1,10 @@
# sapper changelog
## 0.23.1
* Scroll to deeplink that matches current URL ([#472](https://github.com/sveltejs/sapper/pull/472))
* Scroll to deeplink on another page ([#341](https://github.com/sveltejs/sapper/issues/341))
## 0.23.0
* Overhaul internal APIs ([#468](https://github.com/sveltejs/sapper/pull/468))

View File

@@ -1,6 +1,6 @@
{
"name": "sapper",
"version": "0.23.0",
"version": "0.23.1",
"description": "Military-grade apps, engineered by Svelte",
"bin": {
"sapper": "./sapper"

View File

@@ -106,7 +106,8 @@ export function scroll_state() {
};
}
export function navigate(target: Target, id: number, noscroll = false): Promise<any> {
export function navigate(target: Target, id: number, noscroll?: boolean, hash?: string): Promise<any> {
let scroll: ScrollPosition;
if (id) {
// popstate or initial navigation
cid = id;
@@ -137,13 +138,12 @@ export function navigate(target: Target, id: number, noscroll = false): Promise<
if (redirect) {
return goto(redirect.location, { replaceState: true });
}
render(data, nullable_depth, scroll_history[id], token);
render(data, nullable_depth, scroll_history[id], noscroll, hash, token);
if (document.activeElement) document.activeElement.blur();
});
}
function render(data: any, nullable_depth: number, scroll: ScrollPosition, token: {}) {
function render(data: any, nullable_depth: number, scroll: ScrollPosition, noscroll: boolean, hash: string, token: {}) {
if (current_token !== token) return;
if (root_component) {
@@ -182,8 +182,20 @@ function render(data: any, nullable_depth: number, scroll: ScrollPosition, token
});
}
if (scroll) {
scrollTo(scroll.x, scroll.y);
if (!noscroll) {
if (hash) {
// scroll is an element id (from a hash), we need to compute y.
const deep_linked = document.querySelector(hash);
if (deep_linked) {
scroll = {
x: 0,
y: deep_linked.getBoundingClientRect().top
};
}
}
scroll_history[cid] = scroll;
if (scroll) scrollTo(scroll.x, scroll.y);
}
Object.assign(root_props, data);

View File

@@ -13,7 +13,7 @@ import {
set_cid
} from '../app';
import prefetch from '../prefetch/index';
import { Store } from '../types';
import { Store, ScrollPosition } from '../types';
export default function start(opts: {
target: Node,
@@ -36,16 +36,11 @@ export default function start(opts: {
return Promise.resolve().then(() => {
const { hash, href } = location;
const deep_linked = hash && document.getElementById(hash.slice(1));
scroll_history[uid] = deep_linked ?
{ x: 0, y: deep_linked.getBoundingClientRect().top } :
scroll_state();
history.replaceState({ id: uid }, '', href);
if (!initial_data.error) {
const target = select_route(new URL(location.href));
if (target) return navigate(target, uid);
if (target) return navigate(target, uid, false, hash);
}
});
}
@@ -84,7 +79,7 @@ function handle_click(event: MouseEvent) {
const href = String(svg ? (<SVGAElement>a).href.baseVal : a.href);
if (href === location.href) {
event.preventDefault();
if (!location.hash) event.preventDefault();
return;
}
@@ -104,7 +99,7 @@ function handle_click(event: MouseEvent) {
const target = select_route(url);
if (target) {
const noscroll = a.hasAttribute('sapper-noscroll');
navigate(target, null, noscroll);
navigate(target, null, noscroll, url.hash);
event.preventDefault();
history.pushState({ id: cid }, '', url.href);
}

View File

@@ -1 +1,2 @@
<div style="height: 9999px"></div>
<div style="height: 9999px"></div>
<p id="bar">element</p>

View File

@@ -1,6 +1,24 @@
<a href="tall-page#foo">scroll to foo</a>
<div style="height: 9999px"></div>
<div id="foo">
<a href="another-tall-page">link</a>
<a href="another-tall-page" sapper-noscroll>link</a>
</div>
{#if barLink}
<a href="another-tall-page#bar">link</a>
{/if}
</div>
<script>
export default {
data() {
return {
barLink: false
};
},
oncreate() {
this.set({ barLink: true })
}
}
</script>

View File

@@ -33,6 +33,24 @@ describe('scroll', function() {
assert.ok(scrollY > 0, scrollY);
});
it('scrolls to any deeplink if it was already active', async () => {
await page.goto(`${base}/tall-page#foo`);
await start();
let scrollY = await page.evaluate(() => window.scrollY);
assert.ok(scrollY > 0, scrollY);
scrollY = await page.evaluate(() => {
window.scrollTo(0, 0)
return window.scrollY
});
assert.ok(scrollY === 0, scrollY);
await page.click('[href="tall-page#foo"]');
scrollY = await page.evaluate(() => window.scrollY);
assert.ok(scrollY > 0, scrollY);
});
it('resets scroll when a link is clicked', async () => {
await page.goto(`${base}/tall-page#foo`);
await start();
@@ -59,4 +77,15 @@ describe('scroll', function() {
assert.ok(scrollY > 0);
});
it('scrolls into a deeplink on a new page', async () => {
await page.goto(`${base}/tall-page#foo`);
await start();
await prefetchRoutes();
await page.click('[href="another-tall-page#bar"]');
await wait(50);
const scrollY = await page.evaluate(() => window.scrollY);
assert.ok(scrollY > 0);
});
});