AutoAnimate
repository·master·Indexed 12 days ago
https://github.com/formkit/auto-animateA zero-configuration, framework-agnostic animation utility for adding smooth transitions to web applications. It monitors DOM mutations to animate adding, removing, or moving elements. Compatible with Vue, React, Solid, Angular, Preact, and Qwik, providing dedicated hooks, directives, and an AnimationController for programmatic management. Version 0.10.0.
What's inside AutoAnimate
- AutoAnimate is a zero-config, drop-in animation utility designed to add smooth transitions to web applications with minimal effort. It is framework-agnostic and can be used with Vue, React, Solid, or any other JavaScript-based application.
Install @formkit/auto-animate
masterInstall AutoAnimate using your preferred package manager to add smooth transitions to your web application.
# yarn yarn add @formkit/auto-animate # npm npm install @formkit/auto-animate # pnpm pnpm add @formkit/auto-animateExtend autoAnimate with an AutoAnimationPlugin
masterInstead of standard options, you can pass a function as the configuration to create a custom plugin. This allows you to define custom
KeyframeEffects for different DOM actions:add,remove, orremain(moving).A plugin can return either a single
KeyframeEffector a tuple containing[KeyframeEffect, AutoAnimationPluginOptions]to provide custom CSS styles during the animation.import autoAnimate, { AutoAnimationPlugin, AutoAnimationPluginOptions } from '@formkit/auto-animate' const myPlugin: AutoAnimationPlugin = (el, action, newCoords, oldCoords) => { if (action === 'add') { return [ el.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 500 })[0], { styleReset: { color: 'red' } } // Custom styles during animation ] } // ... handle 'remove' and 'remain' return [] } autoAnimate(el, myPlugin)Install the auto-animate Vue plugin
masterYou can install AutoAnimate as a Vue plugin to make the
v-auto-animatedirective available globally throughout your application. You can optionally provide global default options or a plugin during installation.import { autoAnimatePlugin } from '@formkit/auto-animate/vue' // Basic installation app.use(autoAnimatePlugin) // Installation with global default options app.use(autoAnimatePlugin, { duration: 300 })Configure AutoAnimateOptions
masterYou can pass a configuration object to
autoAnimateto customize the transition behavior. These options apply to all elements within the observed tree.const config: Partial<AutoAnimateOptions> = { duration: 250, // Transition duration in milliseconds easing: 'ease-in-out', // 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out' | string disrespectUserMotionPreference: false // If true, ignores 'prefers-reduced-motion' settings }Use AutoAnimate in Angular via the AutoAnimateDirective
masterTo add automatic animations to an Angular application, use the
AutoAnimateDirective. This directive can be applied to any element using the[auto-animate]selector. It is a standalone directive that accepts anoptionsinput, allowing you to pass configuration compatible withAutoAnimateOptions.Because it uses an Angular
effect, the animation logic will automatically re-initialize whenever theoptionsinput changes.import { AutoAnimateDirective } from './path-to-angular-package'; // In your component template: // <div [auto-animate]="{ duration: 300 }"> // <!-- Elements changing inside here will animate --> // </div>Use the v-auto-animate Vue directive
masterFor Vue applications, use the
vAutoAnimatedirective to easily attach animation logic to an element via the template.<template> <!-- Basic usage --> <div v-auto-animate> <div v-for="item in items" :key="item.id">{{ item.text }}</div> </div> <!-- With custom options --> <div v-auto-animate="{ duration: 500 }"> ... </div> </template>useAutoAnimate signature and return type
masterThe
useAutoAnimatehook is a generic function designed for Preact elements.Signature:
useAutoAnimate<T extends Element>(options?: Partial<AutoAnimateOptions> | AutoAnimationPlugin): [Object<T>, (enabled: boolean) => void]Parameters:
options: An optional configuration object of typePartial<AutoAnimateOptions>or anAutoAnimationPlugin.
Returns: A tuple containing:
element: Aref(Object) of typeTto be attached to the target DOM element.setEnabled: A function(enabled: boolean) => voidused to enable or disable the animation controller.
Initialize autoAnimate
masterUse the
autoAnimatefunction to add automatic animations to a parent element and its children. It monitors DOM mutations (adding, removing, or moving elements) and applies smooth transitions.By default, it respects the user's
prefers-reduced-motionsetting. If the browser is not supported or motion is reduced, animations will be disabled unlessdisrespectUserMotionPreference: trueis passed in the config.import autoAnimate from '@formkit/auto-animate' const parent = document.querySelector('.parent') const controls = autoAnimate(parent, { duration: 300, easing: 'ease-in-out' }) // 'controls' can be used to enable/disable animations laterUse the v-auto-animate directive
masterThe
v-auto-animatedirective can be applied to an HTML element or a Vue component to automatically animate changes to its children (e.g., additions, removals, or reordering).If you installed the plugin via
app.use(autoAnimatePlugin), you can use the directive directly in your templates. You can passAutoAnimateOptionsor anAutoAnimationPluginas the directive value.<!-- Basic usage --> <ul v-auto-animate> <li v-for="item in items" :key="item.id">{{ item.text }}</li> </ul> <!-- Usage with specific options --> <ul v-auto-animate="{ duration: 500 }"> <li v-for="item in items" :key="item.id">{{ item.text }}</li> </ul>Use the useAutoAnimate hook in Qwik
masterThe
useAutoAnimatehook provides a way to add automatic animations to Qwik components. It returns a tuple containing a signal to be attached to the parent element and a function to control the animation state.parentRef: ASignal<T | undefined>whereTis anHTMLElement. You must bind this signal to the element you want to animate using therefattribute.setEnabled: A QRL function used to enable or disable animations. It accepts either abooleanor a callback function(isEnabled: boolean) => void.
To use it, pass an optional
AutoAnimateOptionsobject or anAutoAnimationPluginto the hook.import { useAutoAnimate } from '@formkit/auto-animate/qwik'; export const MyComponent = component$(() => { const [parent, setEnabled] = useAutoAnimate(); return ( <ul> <ul ref={parent}> {/* Animated items here */} </ul> <button onClick$={() => setEnabled(false)}>Disable Animations</button> </ul> ); });Use the useAutoAnimate composable
masterThe
useAutoAnimatecomposable provides a more programmatic way to apply animations using a template ref. It returns a tuple containing:- A
Refto the element (or component) to be animated. - A
setEnabledfunction to enable or disable animations dynamically.
This is useful when you need fine-grained control over when animations are active or when working with complex component lifecycles.
import { useAutoAnimate } from '@formkit/auto-animate/vue' export default { setup() { const [animate] = useAutoAnimate({ duration: 300 }) const toggleAnimations = (enabled: boolean) => { animate(enabled) } return { animate, toggleAnimations } } }<template> <ul ref="animate"> <li v-for="item in items" :key="item.id">{{ item.text }}</li> </ul> </template>- A