Skip to content

useAnimate

Category
Export Size
2.31 kB
Last Changed
5 days ago

Reactive Web Animations API

Demo

Usage

Basic Usage

The useAnimate function returns the animation instance and control functions.

tsx
import { 
useAnimate
} from '@reause/core'
import {
useRef
} from 'react'
const
el
=
useRef
<HTMLSpanElement>(null)
const {
isSupported
,
animate
,
// actions
play
,
pause
,
reverse
,
finish
,
cancel
,
// states
pending
,
playState
,
replaceState
,
startTime
,
currentTime
,
timeline
,
playbackRate
,
} =
useAnimate
(
el
, {
transform
: 'rotate(360deg)' }, 1000)
return <
span
ref={
el
} style={{ display: 'inline-block' }}>
useAnimate
</span>

Custom Keyframes

Either an array of keyframe objects, or a keyframe object, or a controllable state. See Keyframe Formats for more details.

tsx
import { 
useAnimate
} from '@reause/core'
const
keyframes
= {
transform
: 'rotate(360deg)' }
// Or const
keyframes
= [
{
transform
: 'rotate(0deg)' },
{
transform
: 'rotate(360deg)' },
] // Or const
keyframes
= {
current
: [
{
clipPath
: 'circle(20% at 0% 30%)' },
{
clipPath
: 'circle(20% at 50% 80%)' },
{
clipPath
: 'circle(20% at 100% 30%)' },
] }
useAnimate
(el,
keyframes
, 1000)

Options

The third argument accepts a duration number or an options object with the following additional properties on top of KeyframeAnimationOptions:

tsx
import { 
useAnimate
} from '@reause/core'
useAnimate
(el, keyframes, {
duration
: 1000,
// Start playing immediately (default: true)
immediate
: true,
// Commit the end styling state to the element (default: false)
commitStyles
: false,
// Persist the animation (default: false)
persist
: false,
// Initial playback rate (default: 1)
playbackRate
: 1,
// Callback when animation is initialized
onReady
(
animate
) {
console
.
log
('Animation ready',
animate
)
}, // Callback when an error occurs
onError
(
e
) {
console
.
error
('Animation error',
e
)
}, })

Delaying Start

Set immediate: false to prevent the animation from starting automatically.

tsx
import { 
useAnimate
} from '@reause/core'
const {
play
} =
useAnimate
(el, keyframes, {
duration
: 1000,
immediate
: false,
}) // Start the animation manually
play
()

Type Declarations

Toggle
ts
export interface UseAnimateOptions extends KeyframeAnimationOptions, ConfigurableWindow {
    immediate?: boolean;
    commitStyles?: boolean;
    persist?: boolean;
    playbackRate?: number;
    onReady?: (animate: Animation) => void;
    onError?: (e: unknown) => void;
}

export type UseAnimateKeyframes = Keyframe[] | PropertyIndexedKeyframes | null

export interface UseAnimateReturn {
    isSupported: boolean;
    animate: Animation | undefined;
    play: () => void;
    pause: () => void;
    reverse: () => void;
    finish: () => void;
    cancel: () => void;
    pending: boolean;
    playState: AnimationPlayState;
    replaceState: AnimationReplaceState;
    startTime: number | CSSNumberish | null;
    currentTime: CSSNumberish | null;
    timeline: AnimationTimeline | null;
    playbackRate: number;
}

export type ElementTarget<T extends TargetElement = TargetElement> = RefObject<T | null>

export type ElementTargetOrArray<T extends TargetElement = TargetElement> = ElementTarget<T> | ElementTarget<T>[]

export function useAnimate(target: ElementTarget, keyframes: UseAnimateKeyframes, options?: number | UseAnimateOptions): UseAnimateReturn

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
e608a - feat(ci): align CI, packaging and release with VueUse
669e1 - fix(core): resolve useAnimate audit findings (#518)
3ecf3 - fix: resolve CI test and build regressions
edba9 - feat(core): add useAnimate (#64)

Released under the MIT License. v0.1.8