Motion
useAnimate
Plays motion from your code, for something an event starts: a wrong code shaking, an item flying to the cart, a success card building in. animate(target, values, options) takes the same values and timing as v-animate, and follows the app's spring, motion: false and reduced motion. The end stays as the element's own style. Pass a list of steps and it plays them one after another as one sequence. To play motion as elements come in, use v-animate with after and stagger in the template.
Install
import { useAnimate, animate } from 'vael-ui/motion'Examples
// In a component: useAnimate, scoped to it.
import { useTemplateRef } from 'vue'
import { useAnimate } from 'vael-ui/motion'
const { animate } = useAnimate(useTemplateRef('card'))
// One element: shake it.
function wrong() {
animate('.field', { x: [0, -8, 8, -4, 0] }, { duration: 300 })
}
// A sequence: each step after the one before, unless its `at` says otherwise.
async function sent() {
await animate([
['.card', { opacity: [0, 1], scale: [0.92, 1] }, { name: 'card' }],
['.check path', { pathLength: [0, 1] }, { at: 'card:60%' }],
['.title', { opacity: [0, 1], y: [6, 0] }, { at: '-0.15' }],
])
}
// Outside a component, say in shake.ts: plain animate, given the element itself.
import { animate } from 'vael-ui/motion'
export function shake(el: Element) {
return animate(el, { x: [0, -8, 8, -4, 0] }, { duration: 300 })
}