Skip to content

Commit 9a2d6f5

Browse files
committed
Refactor
1 parent 39d4c42 commit 9a2d6f5

File tree

4 files changed

+17
-22
lines changed

4 files changed

+17
-22
lines changed

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ SvelteKit version of the [Shared Element Transitions Google CodeLab](https://cod
44

55
TODO:
66

7-
- optimize images
8-
- stretch goal: transition back from fruit page
9-
- remove React-isms (useX)
10-
- clean up console logs
117
- layout shifting during transition
128
- instead of shared-element class, data- attributes?
139
- better way instead of having to register per-page -- possibly call prepare functions with {from, to} allowlist
10+
- update comments on transition hooks

src/lib/utils/use-page-transition.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { afterNavigate, beforeNavigate } from '$app/navigation';
22
import { getTransitionContext } from '$lib/utils/resource-context';
33
import { onDestroy } from 'svelte';
4-
import { get } from 'svelte/store';
5-
import { page as pageStore } from '$app/stores';
64

75
// Call this hook on this first page before you start the page transition.
86
// For Shared Element Transitions, you need to call the transition.start()
@@ -16,7 +14,7 @@ export const prepareTransitionFromPage = () => {
1614
beforeNavigate(({ to }) => {
1715
// Feature detection
1816
if (!document.createDocumentTransition) {
19-
return null;
17+
return;
2018
}
2119

2220
const transitionKey = to.pathname;
@@ -28,6 +26,10 @@ export const prepareTransitionFromPage = () => {
2826
[transitionKey]: { transition, resolver }
2927
}));
3028
});
29+
transitionStore.update((current) => ({
30+
...current,
31+
[transitionKey]: null
32+
}));
3133
});
3234
});
3335
};
@@ -39,11 +41,10 @@ export const prepareTransitionFromPage = () => {
3941
// shared elements.
4042
export const prepareTransitionToPage = () => {
4143
const transitionStore = getTransitionContext();
42-
const page = get(pageStore);
43-
const transitionKey = page.url.pathname;
4444
let unsub;
4545

46-
afterNavigate(() => {
46+
afterNavigate(({ to }) => {
47+
const transitionKey = to.pathname;
4748
unsub = transitionStore.subscribe((transitions) => {
4849
const transition = transitions[transitionKey];
4950
if (!transition) {
@@ -56,9 +57,5 @@ export const prepareTransitionToPage = () => {
5657

5758
onDestroy(() => {
5859
unsub?.();
59-
transitionStore.update((current) => ({
60-
...current,
61-
[transitionKey]: null
62-
}));
6360
});
6461
};

src/routes/__layout.svelte

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
initTransitionContext();
88
</script>
99

10-
<svelte:head>
11-
<title>Shared Element Transitions in SvelteKit</title>
12-
</svelte:head>
13-
1410
<Navbar />
1511
<main class="text-slate-700 max-w-4xl m-auto">
1612
<slot />

src/routes/index.svelte

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
<svelte:head>
2+
<title>Shared Element Transitions in SvelteKit</title>
3+
</svelte:head>
4+
15
<div class="flex-col items-center justify-center py-4 px-4">
26
<h1 class="text-4xl font-bold mt-4">Shared Element Transitions in SvelteKit</h1>
37

48
<p class="mt-4 text-xl">
5-
SvelteKit version of the [Shared Element Transitions Google
6-
CodeLab](https://codelabs.developers.google.com/create-an-instant-and-seamless-web-app#5). Only
7-
works in Chrome Canary with the documentTransition API flag enabled and may break at any time
8-
due to the API changing.
9+
SvelteKit version of the <a
10+
href="https://codelabs.developers.google.com/create-an-instant-and-seamless-web-app#5"
11+
>Shared Element Transitions Google CodeLab</a
12+
>. Only works in Chrome Canary with the documentTransition API flag enabled and may break at any
13+
time due to the API changing.
914
</p>
1015
</div>

0 commit comments

Comments
 (0)