AutoAnimate

repository·master·Indexed 12 days ago

https://github.com/formkit/auto-animate

A 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.

Tokens
4.5K
Snippets
17
Records
20
Agent score
96%

What's inside AutoAnimate

  1. Overview of AutoAnimate

    master
    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.
  2. Install @formkit/auto-animate

    master

    Install 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-animate
  3. Extend autoAnimate with an AutoAnimationPlugin

    master

    Instead 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, or remain (moving).

    A plugin can return either a single KeyframeEffect or 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)
  4. Install the auto-animate Vue plugin

    master

    You can install AutoAnimate as a Vue plugin to make the v-auto-animate directive 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 })
  5. Configure AutoAnimateOptions

    master

    You can pass a configuration object to autoAnimate to 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
    }
  6. Use AutoAnimate in Angular via the AutoAnimateDirective

    master

    To 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 an options input, allowing you to pass configuration compatible with AutoAnimateOptions.

    Because it uses an Angular effect, the animation logic will automatically re-initialize whenever the options input changes.

    import { AutoAnimateDirective } from './path-to-angular-package';
    
    // In your component template:
    // <div [auto-animate]="{ duration: 300 }">
    //   <!-- Elements changing inside here will animate -->
    // </div>
  7. Use the v-auto-animate Vue directive

    master

    For Vue applications, use the vAutoAnimate directive 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>
  8. useAutoAnimate signature and return type

    master

    The useAutoAnimate hook 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 type Partial<AutoAnimateOptions> or an AutoAnimationPlugin.

    Returns: A tuple containing:

    1. element: A ref (Object) of type T to be attached to the target DOM element.
    2. setEnabled: A function (enabled: boolean) => void used to enable or disable the animation controller.
  9. Initialize autoAnimate

    master

    Use the autoAnimate function 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-motion setting. If the browser is not supported or motion is reduced, animations will be disabled unless disrespectUserMotionPreference: true is 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 later
  10. Use the v-auto-animate directive

    master

    The v-auto-animate directive 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 pass AutoAnimateOptions or an AutoAnimationPlugin as 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>
  11. Use the useAutoAnimate hook in Qwik

    master

    The useAutoAnimate hook 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.

    1. parentRef: A Signal<T | undefined> where T is an HTMLElement. You must bind this signal to the element you want to animate using the ref attribute.
    2. setEnabled: A QRL function used to enable or disable animations. It accepts either a boolean or a callback function (isEnabled: boolean) => void.

    To use it, pass an optional AutoAnimateOptions object or an AutoAnimationPlugin to 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>
      );
    });
  12. Use the useAutoAnimate composable

    master

    The useAutoAnimate composable provides a more programmatic way to apply animations using a template ref. It returns a tuple containing:

    1. A Ref to the element (or component) to be animated.
    2. A setEnabled function 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>