Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useAnimate.md
1---2category: Animation3---45# useAnimate67Reactive [Web Animations API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API).89## Usage1011### Basic Usage1213The `useAnimate` function returns the animation instance and control functions.1415```vue16<script setup lang="ts">17import { useAnimate } from '@vueuse/core'18import { useTemplateRef } from 'vue'1920const el = useTemplateRef('el')21const {22isSupported,23animate,2425// actions26play,27pause,28reverse,29finish,30cancel,3132// states33pending,34playState,35replaceState,36startTime,37currentTime,38timeline,39playbackRate,40} = useAnimate(el, { transform: 'rotate(360deg)' }, 1000)41</script>4243<template>44<span ref="el" style="display:inline-block">useAnimate</span>45</template>46```4748### Custom Keyframes4950Either an array of keyframe objects, or a keyframe object, or a `ref`. See [Keyframe Formats](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats) for more details.5152```ts53import { useAnimate } from '@vueuse/core'54import { useTemplateRef } from 'vue'5556const el = useTemplateRef('el')57// ---cut---58const keyframes = { transform: 'rotate(360deg)' }59// Or60const keyframes = [61{ transform: 'rotate(0deg)' },62{ transform: 'rotate(360deg)' },63]64// Or65const keyframes = ref([66{ clipPath: 'circle(20% at 0% 30%)' },67{ clipPath: 'circle(20% at 50% 80%)' },68{ clipPath: 'circle(20% at 100% 30%)' },69])7071useAnimate(el, keyframes, 1000)72```7374### Options7576The third argument accepts a duration number or an options object with the following additional properties on top of [KeyframeAnimationOptions](https://developer.mozilla.org/en-US/docs/Web/API/Element/animate#parameters):7778```ts79import { useAnimate } from '@vueuse/core'8081useAnimate(el, keyframes, {82duration: 1000,83// Start playing immediately (default: true)84immediate: true,85// Commit the end styling state to the element (default: false)86commitStyles: false,87// Persist the animation (default: false)88persist: false,89// Callback when animation is initialized90onReady(animate) {91console.log('Animation ready', animate)92},93// Callback when an error occurs94onError(e) {95console.error('Animation error', e)96},97})98```99100### Delaying Start101102Set `immediate: false` to prevent the animation from starting automatically.103104```ts105import { useAnimate } from '@vueuse/core'106107const { play } = useAnimate(el, keyframes, {108duration: 1000,109immediate: false,110})111112// Start the animation manually113play()114```115116## Type Declarations117118```ts119export interface UseAnimateOptions120extends KeyframeAnimationOptions, ConfigurableWindow {121/**122* Will automatically run play when `useAnimate` is used123*124* @default true125*/126immediate?: boolean127/**128* Whether to commits the end styling state of an animation to the element being animated129* In general, you should use `fill` option with this.130*131* @default false132*/133commitStyles?: boolean134/**135* Whether to persists the animation136*137* @default false138*/139persist?: boolean140/**141* Executed after animation initialization142*/143onReady?: (animate: Animation) => void144/**145* Callback when error is caught.146*/147onError?: (e: unknown) => void148}149export type UseAnimateKeyframes = MaybeRef<150Keyframe[] | PropertyIndexedKeyframes | null151>152export interface UseAnimateReturn extends Supportable {153animate: ShallowRef<Animation | undefined>154play: () => void155pause: () => void156reverse: () => void157finish: () => void158cancel: () => void159pending: ComputedRef<boolean>160playState: ComputedRef<AnimationPlayState>161replaceState: ComputedRef<AnimationReplaceState>162startTime: WritableComputedRef<CSSNumberish | number | null>163currentTime: WritableComputedRef<CSSNumberish | null>164timeline: WritableComputedRef<AnimationTimeline | null>165playbackRate: WritableComputedRef<number>166}167/**168* Reactive Web Animations API169*170* @see https://vueuse.org/useAnimate171* @param target172* @param keyframes173* @param options174*/175export declare function useAnimate(176target: MaybeComputedElementRef,177keyframes: UseAnimateKeyframes,178options?: number | UseAnimateOptions,179): UseAnimateReturn180```181