Transition
<AnimeTransition> works like Vue's
<Transition>, with
AnimeJS running the enter and leave animations instead of CSS classes. It
takes one child, toggled by v-if, v-show or a changed key.
Chain steps with keyframes
import type { AnimeTransitionAnimation } from '#nanime/types'
import { random } from '#nanime/utils'
const animation: AnimeTransitionAnimation = {
enter: {
x: () => [random(-40, 40), 0],
y: [60, -6, 0],
scale: [0.6, 1.04, 1],
opacity: [0, 1, 1],
duration: 480,
ease: 'outQuart',
},
leave: {
x: () => [0, random(-60, 60)],
y: [0, -8, 70],
scale: [1, 1.06, 0.6],
opacity: [1, 1, 0],
duration: 480,
ease: 'inOut(2)',
},
}
const visible = ref(true)
<template>
<div class="grid h-16 place-items-center">
<AnimeTransition :animation="animation">
<div v-if="visible" class="size-12 rounded-lg bg-primary" />
</AnimeTransition>
</div>
</template>
A function value runs each time the element leaves, so each leave goes somewhere new.
API
{ enter, leave }. Defaults to 'fade': 300ms in and 200ms out.
Defaults sets another for the app.animation.animation.'default'.false.waapi.animate() instead of animate().
The animation props then take waapi.animate() params. Defaults to
false.false.With reduced motion on, the element jumps to its end state. See Accessibility.
Vue's transition events pass through, and nanime calls the done
callback of @enter and @leave when the animation finishes.
Set enter and leave with animation
<AnimeTransition animation="slide-up" />
<AnimeTransition :animation="{ enter: 'slide-up', leave: 'fade' }" />
With animation="fade" and enter-animation="scale", the element enters
with scale and leaves with fade.
Swap keyed elements with mode
const modes = ['default', 'in-out', 'out-in'] as const
const mode = ref<typeof modes[number]>('out-in')
const current = ref('A')
function swap() {
current.value = current.value === 'A' ? 'B' : 'A'
}
<template>
<div class="flex h-14 items-center gap-2">
<AnimeTransition
:mode="mode"
:enter-animation="{ opacity: [0, 1], x: [-24, 0], duration: 600 }"
:leave-animation="{ opacity: 0, x: 24, duration: 600 }"
>
<div :key="current" class="...">
{{ current }}
</div>
</AnimeTransition>
</div>
</template>
out-in waits for the old element to finish leaving, in-out enters the
new one first, and default runs both at once.
Keep transitions smooth during hydration with waapi
<AnimeTransition waapi :enter-animation="{ opacity: [0, 1], y: [8, 0], duration: 300 }">
<div v-if="open" class="panel" />
</AnimeTransition>
Hydration keeps the main thread busy, which stalls animate(). The browser
runs waapi.animate() animations off the
main thread, so they keep playing.
waapi.animate() cannot run, such as one with keyframe objects
or a function value, runs on animate() and logs a warning in development.Finish animations when the tab is hidden
AnimeJS pauses its animations while the browser tab is hidden, so a
leaving element stays half faded. Set completeOnTabHidden to jump the
component's running animations to their end instead.
To turn it on for every instance, set it in nanime.components in
app.config.ts:
export default defineAppConfig({
nanime: {
components: { transition: { completeOnTabHidden: true } },
},
})
transitionGroup and layoutGroup take the same key.
See also
<AnimeTransitionGroup>: animatev-forlists- Transition styles: the built-in style names
- Defaults: set the props for the whole app