Skip to content

Commit 622b0f7

Browse files
committed
Refactor into single transition method
1 parent a2149cf commit 622b0f7

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ TODO:
66

77
- layout shifting during transition
88
- instead of shared-element class, data- attributes?
9-
- better way instead of having to register per-page -- possibly call prepare functions with {from, to} allowlist
10-
- update comments on transition hooks
9+
- reduced motion
10+
- sveltekit:prefetch messing things up
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import { afterNavigate, beforeNavigate } from '$app/navigation';
2-
import { getTransitionContext } from '$lib/utils/resource-context';
2+
import { setContext, getContext, hasContext } from 'svelte';
3+
import { writable } from 'svelte/store';
4+
5+
const contextKey = 'transition';
6+
7+
export function initTransitionContext() {
8+
if (hasContext(contextKey)) return getContext(contextKey);
9+
return setContext(contextKey, writable({}));
10+
}
11+
12+
export function getTransitionContext() {
13+
if (!hasContext(contextKey)) {
14+
return initTransitionContext();
15+
}
16+
return getContext(contextKey);
17+
}
318

419
// Call this hook on this first page before you start the page transition.
520
// For Shared Element Transitions, you need to call the transition.start()
@@ -11,9 +26,16 @@ export const preparePageTransition = () => {
1126
const transitionStore = getTransitionContext();
1227
let unsub;
1328

29+
function updateStore(key, value) {
30+
transitionStore.update((current) => ({
31+
...current,
32+
[key]: value
33+
}));
34+
}
35+
1436
// before navigating, start a new transition
1537
beforeNavigate(({ to }) => {
16-
unsub?.();
38+
unsub?.(); // clean up previous subscription
1739

1840
// Feature detection
1941
if (!document.createDocumentTransition) {
@@ -23,16 +45,11 @@ export const preparePageTransition = () => {
2345
const transitionKey = to.pathname;
2446
const transition = document.createDocumentTransition();
2547
transition.start(async () => {
48+
// set transition data for afterNavigate hook to pick up
2649
await new Promise((resolver) => {
27-
transitionStore.update((current) => ({
28-
...current,
29-
[transitionKey]: { transition, resolver }
30-
}));
50+
updateStore(transitionKey, { transition, resolver });
3151
});
32-
transitionStore.update((current) => ({
33-
...current,
34-
[transitionKey]: null
35-
}));
52+
updateStore(transitionKey, null);
3653
});
3754
});
3855

src/lib/utils/resource-context.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/routes/__layout.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import '../app.css';
33
import Navbar from '$lib/Navbar.svelte';
44
import Footer from '$lib/Footer.svelte';
5-
import { initTransitionContext } from '$lib/utils/resource-context';
6-
import { preparePageTransition } from '$lib/utils/use-page-transition';
5+
import { initTransitionContext, preparePageTransition } from '$lib/utils/page-transition';
76
87
initTransitionContext();
98
preparePageTransition();

src/routes/index.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<p class="mt-4 text-xl">
99
SvelteKit version of the <a
1010
href="https://codelabs.developers.google.com/create-an-instant-and-seamless-web-app#5"
11-
>Shared Element Transitions Google CodeLab</a
11+
class="underline">Shared Element Transitions Google CodeLab</a
1212
>. Only works in Chrome Canary with the documentTransition API flag enabled and may break at any
1313
time due to the API changing.
1414
</p>

0 commit comments

Comments
 (0)