Motion guide · 2 of 8
Any element
Motion isn't only for vael-ui components. v-animate gives any of your elements an entrance and an exit, right in the template.
In and out
initial sets where the element comes in from. When Vue removes it, it leaves the way it came, faster and quieter, unless you give it an exit. Inside a component that closes, such as a dialog or a menu, it leaves with the panel instead. For the usual entrances, pick one by name: v-animate.fade, .rise, .drop, .pop or .blur. Add a spring the same way, as in v-animate.pop.bouncy, and tune it with a value: v-animate.rise="{ delay: 0.1 }".
Names
The word after the colon, as in v-animate:rows, is a name. An element on its own doesn't need one. You name elements when they belong together:
- Elements under one name that come in at the same time are one group, so
staggerplays them one after another. - Inside a vael-ui component, such as a Dialog, its
motion.targets.rowssets how the elements namedrowsmove, and they come and go with the component. after: 'rows'lets another element wait for them.- Pages under one name can wait for each other with
swap: 'wait'.
Name: elements with the same name are one group, so stagger plays them one after another and after: 'rows' lets another element wait for them.
Add a name only when something else has to point at the element: a stagger group, after, or a component's motion.targets. Otherwise leave it off.
<!-- On its own: no name. -->
<p v-animate.rise>Saved</p>
<!-- One group: they come in one after another. -->
<li
v-for="row in rows"
:key="row.id"
v-animate:rows.rise="{ stagger: true }"
>…</li>
<!-- In a component: they move with the dialog. -->
<Dialog :motion="{ targets: { rows: { initial: { y: 6 } } } }">
<li v-for="row in rows" :key="row.id" v-animate:rows>…</li>
</Dialog>Shown and hidden
Beside v-show, v-animate plays its exit when the element is hidden and its entrance when it's shown again, as it does for v-if.
When it scrolls into view
when: 'visible' waits until the element is on screen, then plays once. Elements that share a name, such as v-animate:tiles, play in page order, one after another with a stagger. stagger: true spaces them by the stagger token; 'end', 'center' or 'edges' start the wave there instead. In a grid, 'center' and 'edges' go by where each element sits: the wave starts from the middle cell, or from the corners.
Across a grid
Elements under one name that come in together stagger by where each sits. In a grid, stagger: 'center' starts from the middle cell and spreads outward, 'edges' starts from the corners and closes in, and 'random' goes in a new shuffled order each time. In a single row or column, they go in page order.
While it's in view
states.inView holds a look while the element is on screen and lets it go when it scrolls away. viewport sets when it counts: amount ('some', 'all' or 0 to 1), a margin around the screen, and once to keep it once seen. when: 'visible' takes the same viewport. Give inView its own viewport when it should count differently from when: 'visible', and onAfterEnter and onAfterLeave to run your code as it turns on and off, such as marking the section you're reading.
Linked to scroll
useScrollProgress() gives the page's scroll, or an element's way through the screen, as a ref from 0 to 1. Pass it to progress as a getter, progress: () => scroll, and the keyframes follow the scroll without re-rendering your component. offset sets where it starts and ends: ['start end', 'end start'] runs from entering to having left. useScrollLength() gives how far it can scroll, in px, so scroll.value * length.value is the distance scrolled.
Play it again
replay plays initial again each time its value changes, without remounting the element. Use it for a count going up, a status change or a failed copy. With appear: false, it plays only on those changes, not when the element mounts. With appear: 'later', what's there when the page first renders stays still, and anything added after comes in. Inside a component's panel, that's what's there when it opens. Keyframes in animate play again from the start too, so a line draws itself again for each new period.
Once per page load
appear: 'once' plays a name's entrance once per page load. It plays the first time the name has elements to show, and rows that arrive in the next 600 ms join it, such as a second list under the same name or data loading in batches. Later mounts under that name show without the entrance. Hide and show the lists below: only the plain one plays again.
Slide to a new place
layout: true slides an element to wherever the layout puts it, such as when a list reorders or a sibling grows, and eases it to a new size when it gets one. layout: 'position' only slides. A delay waits before it slides, holding its old place meanwhile.
From one place to another
Two elements with the same layoutId act as one element. When one leaves and the other arrives, the new one grows out of the old one's box. Add layout: 'position' to keep its size on the way, so its text never stretches.
Pages
A route change is an element leaving and another coming in. Put v-animate with a name on the routed component. The old page leaves where it was as the new one comes in; swap: 'wait' brings the new one in once the old one has gone.
<script setup lang="ts">
import { vAnimate } from 'vael-ui/motion'
const page = {
initial: { opacity: 0, y: 12 },
exit: { opacity: 0, y: -12 },
swap: 'wait',
} as const
</script>
<template>
<RouterView v-slot="{ Component, route }">
<component :is="Component" :key="route.path" v-animate:page="page" />
</RouterView>
</template>Hold a look
animate sets a look the element holds while it's on the page. Change it, and the element animates there from wherever it is, on a spring unless you set a duration: animate: { rotate: open ? 180 : 0 }. With keyframe arrays, progress (0 to 1) holds them at one point instead of playing them, so a swipe or a scroll can scrub them. With a spring, they follow it with a short lag.
What you can animate
Besides x, y, scale, rotate, the 3D turns and opacity, a look can hold any CSS property that animates: a color, a filter such as a blur, a clipPath for a wipe, and your own custom properties. A plain number means px, or degrees for rotate. Move things with transforms and opacity; keep colors, blur and clips for small touches.
v-animate="{
initial: {
opacity: 0,
y: 8, // px
rotate: -4, // degrees
color: 'var(--ui-text-muted)',
filter: 'blur(4px)',
clipPath: 'inset(0 100% 0 0)',
'--glow': 0,
},
}"Turn in 3D
rotateX and rotateY turn an element toward you or away, and perspective sets how deep that looks: a card flip is animate: { rotateY: flipped ? 180 : 0, perspective: 800 }. skewX and skewY slant it, and scaleX, scaleY and scaleZ scale one axis, such as a bar growing from its base.
Draw a stroke
pathLength draws an SVG stroke from 0 (none) to 1 (all of it): initial: { pathLength: 0 } draws a check in. pathOffset moves where the drawn part starts, and pathSpacing sets the gap after it, so a short stroke repeats along the path. animate() sets pathLength="1" on the path, so pathOffset and any dash you set in CSS are fractions of its length.
Follow its content
v-animate.size eases an element to its content's new height, and to its new width when the content sizes that, instead of jumping.
From code
For motion an event starts, such as a wrong code or an item added to the cart, animate(el, values, options) plays the same values and timing as v-animate from your code, and leaves the end as the element's own style. Await it, or steer it with pause(), reverse(), time and speed. useAnimate(useTemplateRef('row')) scopes selectors to one element and stops what it started when the component unmounts. It follows the app's spring, motion: false and reduced motion. To play several in order, give it a list of steps (see Choreography).
Tween a number
useTween(source) is a number that eases to each new value of its source, from wherever it is: a total, a percentage, a value you draw yourself. It rides the app's spring unless you give it a spring, or a duration and easing, and jumps with motion off or reduced motion.
Dragging
v-draggable reorders your own lists, and useSortableGroup moves items between them. For anything you throw or swipe away, use useDrag: the element follows your finger, and onRelease decides where it goes. Link a look to the drag with a getter in animate: rotate: () => mapRange(drag.x.value, [-200, 200], [-12, 12]). The getter runs every frame without re-rendering. An exit on a spring keeps the speed of the throw.
Your own components
useMotion gives your own component the same motion as vael-ui's. Bind its transition to Vue's <Transition>. The component then follows your app's motion, animates in and out, and animates its named children with it.