vael-ui v0.5.7

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.

Which one?

  • useAnimate(): use it in a component. Selectors match inside your element, and what it started stops when the component unmounts.
  • animate: use it only outside a component, such as a shared shake(el) helper that other code calls with an element, or a test.

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 })
}

Parameters

NameTypeDescription
elementReadonly<ShallowRef<Element | ComponentPublicInstance | null>>useAnimate only: the element selectors match inside. Leave it out and bind the returned scope with ref="scope".
targetElement | ComponentPublicInstance | string | ArrayLike<Element>What moves: an element, a component, a list of elements, or a selector (inside the scope, with useAnimate).
valuesMotionValuesWhere it moves to: x, y, scale, rotate, opacity, pathLength, any CSS property. An array is keyframes, from the first to the last. From wherever it is when there is no first.
options.spring / duration / easingSpringName | Spring / number / stringIts timing: a spring by name or numbers, or a duration in ms and an easing. Unset, the app's spring.
options.delaynumberSeconds before it starts.
options.staggerboolean | number | 'end' | 'center' | 'edges' | 'random'With several elements, one after another: true, seconds between each, or an order.
options.times / repeat / repeatType / repeatDelaynumber[] / number / 'loop' | 'reverse' | 'mirror' / numberKeyframes: where each falls (0 to 1), how many more times they play (Infinity loops), how they repeat and the seconds held between runs.
stepsSequenceStep[]Instead of a target: a sequence, each step [target, values, options]. A step takes the options above plus at and name.
step options.atSequenceAtWhere the step starts. Unset: when the step before ends. '+0.2' / '-0.15': seconds after or before that. '50%': halfway through the step before. 1.2: seconds from the start. 'card': with the step named card; 'card:60%': 60% of the way through it. TypeScript suggests the names your steps declare.
step options.namestringA name for the step, for a later step's at.

Returns

NameTypeDescription
controlsAnimationControlsAwait it for the end (never for an endless loop), or steer it: pause(), play(), reverse(), finish(), stop(), and time (ms) and speed to read or set. A sequence is steered as one.
scopeShallowRef<Element | null>useAnimate only: the scope element, the one you passed or one to bind.
animatetypeof animateuseAnimate only: animate, scoped to the element and stopped on unmount.