Skip to content

Commit a2149cf

Browse files
committed
Refactor transition code into root layout
1 parent 9a2d6f5 commit a2149cf

File tree

4 files changed

+7
-30
lines changed

4 files changed

+7
-30
lines changed

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { afterNavigate, beforeNavigate } from '$app/navigation';
22
import { getTransitionContext } from '$lib/utils/resource-context';
3-
import { onDestroy } from 'svelte';
43

54
// Call this hook on this first page before you start the page transition.
65
// For Shared Element Transitions, you need to call the transition.start()
76
// method before the next page begins to render, and you need to do the
87
// Document Object Model (DOM) modification or setting of new shared
98
// elements inside the callback so that this hook returns the promise and
109
// defers to the callback resolve.
11-
export const prepareTransitionFromPage = () => {
10+
export const preparePageTransition = () => {
1211
const transitionStore = getTransitionContext();
12+
let unsub;
1313

14+
// before navigating, start a new transition
1415
beforeNavigate(({ to }) => {
16+
unsub?.();
17+
1518
// Feature detection
1619
if (!document.createDocumentTransition) {
1720
return;
@@ -32,16 +35,6 @@ export const prepareTransitionFromPage = () => {
3235
}));
3336
});
3437
});
35-
};
36-
37-
// Call this hook on the second page. Inside the useEffect hook, you can
38-
// refer to the actual DOM element and set them as shared elements with the
39-
// transition.setElement() method. When the resolver function is called,
40-
// the transition is initiated between the captured images and newly set
41-
// shared elements.
42-
export const prepareTransitionToPage = () => {
43-
const transitionStore = getTransitionContext();
44-
let unsub;
4538

4639
afterNavigate(({ to }) => {
4740
const transitionKey = to.pathname;
@@ -54,8 +47,4 @@ export const prepareTransitionToPage = () => {
5447
resolver();
5548
});
5649
});
57-
58-
onDestroy(() => {
59-
unsub?.();
60-
});
6150
};

src/routes/__layout.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import Navbar from '$lib/Navbar.svelte';
44
import Footer from '$lib/Footer.svelte';
55
import { initTransitionContext } from '$lib/utils/resource-context';
6+
import { preparePageTransition } from '$lib/utils/use-page-transition';
67
78
initTransitionContext();
9+
preparePageTransition();
810
</script>
911

1012
<Navbar />

src/routes/fruits/[name].svelte

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
export let nutrition;
66
77
import Nutrition from '$lib/Nutrition.svelte';
8-
import {
9-
prepareTransitionToPage,
10-
prepareTransitionFromPage
11-
} from '$lib/utils/use-page-transition';
12-
13-
prepareTransitionToPage();
14-
prepareTransitionFromPage();
158
</script>
169

1710
<svelte:head>

src/routes/fruits/index.svelte

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22
export let items;
33
44
import ListItem from '$lib/ListItem.svelte';
5-
import {
6-
prepareTransitionFromPage,
7-
prepareTransitionToPage
8-
} from '$lib/utils/use-page-transition';
9-
10-
prepareTransitionFromPage();
11-
prepareTransitionToPage();
125
</script>
136

147
<svelte:head>

0 commit comments

Comments
 (0)