Skip to content

Commit a55a848

Browse files
committed
Use beforeNavigate
1 parent 604af12 commit a55a848

File tree

4 files changed

+11
-16
lines changed

4 files changed

+11
-16
lines changed

src/lib/ListItem.svelte

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@
33
export let href;
44
55
import Icon from '$lib/Icon.svelte';
6-
import { getPageTransitionTrigger } from '$lib/utils/use-page-transition';
7-
8-
const transitionNextState = getPageTransitionTrigger();
9-
10-
function handleClick() {
11-
// TODO: this only works on click, not back/forward
12-
transitionNextState();
13-
}
146
</script>
157

168
<li class="p-3 hover:bg-slate-100 hover:text-slate-900">
17-
<a {href} class="block flex items-center" on:click={handleClick}>
9+
<a {href} class="block flex items-center">
1810
<Icon src={item.image} name={item.name} className="shared-element" />
1911
<div class="text-xl">{item.name}</div>
2012
</a>

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

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

@@ -8,10 +8,10 @@ import { onDestroy } from 'svelte';
88
// Document Object Model (DOM) modification or setting of new shared
99
// elements inside the callback so that this hook returns the promise and
1010
// defers to the callback resolve.
11-
export const getPageTransitionTrigger = () => {
11+
export const prepareTransitionFromPage = () => {
1212
const transitionStore = getTransitionContext();
1313

14-
return () => {
14+
beforeNavigate(() => {
1515
// Feature detection
1616
if (!document.createDocumentTransition) {
1717
return null;
@@ -23,15 +23,15 @@ export const getPageTransitionTrigger = () => {
2323
transitionStore.set({ transition, resolver });
2424
});
2525
});
26-
};
26+
});
2727
};
2828

2929
// Call this hook on the second page. Inside the useEffect hook, you can
3030
// refer to the actual DOM element and set them as shared elements with the
3131
// transition.setElement() method. When the resolver function is called,
3232
// the transition is initiated between the captured images and newly set
3333
// shared elements.
34-
export const pageTransition = () => {
34+
export const prepareTransitionToPage = () => {
3535
const transitionStore = getTransitionContext();
3636
let unsub;
3737
afterNavigate(() => {

src/routes/fruits/[name].svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
export let nutrition;
66
77
import Nutrition from '$lib/Nutrition.svelte';
8-
import { pageTransition } from '$lib/utils/use-page-transition';
8+
import { prepareTransitionToPage } from '$lib/utils/use-page-transition';
99
10-
pageTransition();
10+
prepareTransitionToPage();
1111
</script>
1212

1313
<div class={'flex flex-col items-center justify-center py-4 px-4 sm:flex-row'}>

src/routes/fruits/index.svelte

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

710
<div class={'flex-col items-center justify-center py-4 px-4'}>

0 commit comments

Comments
 (0)