A Svele/SvelteKit plugin that adds new hooks for customizing mount / unmount transitions.
yarn add svelte-transitioner
This plugin adds the following hooks:
onGlobalInit
: a hook responsible for executing custom code when mounting any of the child componentsonInit
: a hook responsible for executing custom code on the current componentglobalIntro
: a hook that can execute the same custom transition when mounting any of the child componentsintro
: a hook that can execute a transition when mounting a componentglobalOutro
: a hook that can execute the same custom transition when unmounting any of the child componentsoutro
: a hook that can execute a transition when unmounting a componentfor the on*Init
hooks, they will be called like the following:
onInit((node) => {
// node is the current mounted node
})
as for the *intro
or *outro
callbacks, it can be used in two ways:
intro((node, t) => {
// node is the current mounted node
// t is the current transition animation
// 1000 is the duration of the transition
}, 1000)
// Or you can use a GSAP Timeline callback instead, like the following
intro((node) => {
const tl = gsap.timeline({
paused: true
})
tl.to(node, {
duration: 1,
yPercent: -100,
})
return tl
})
One thing to keep in mind, in order for all the above hooks to work, we have to use some wrapper components:
TransitionLayout
: this should be used in the parent components that will contains all the transitioned components.Transitioner
: this should be wrapped around the components that you want to apply transitions to.So for example in a SvelteKit project, we can have the following:
/routes/__layout.svelte
:
<script>
import { onGlobalInit, globalIntro, globalOutro, TransitionLayout } from 'svelte-transitioner'
onGlobalInit((node) => {
})
globalIntro((node) => {
return gsap.timeline({ paused: true })
})
globalOutro((node, t) => {
})
</script>
<TransitionLayout>
<slot />
</TransitionLayout>
/routes/index.svelte
:
<script>
import { onInit, intro, outro, Transitioner } from 'svelte-transitioner'
onInit((node) => {
})
intro((node) => {
return gsap.timeline({ paused: true })
})
outro((node, t) => {
})
</script>
<Transitioner>
index
<a href="/one">one.svelte</a>
</Transitioner>
/routes/one.svelte
:
<script>
import { onInit, intro, outro, Transitioner } from 'svelte-transitioner'
onInit((node) => {
})
intro((node) => {
return gsap.timeline({ paused: true })
})
outro((node, t) => {
})
</script>
<Transitioner>
One
<a href="/">index.svelte</a>
</Transitioner>