@vueuse/motion

repository·main·Indexed 25 days ago

https://github.com/vueuse/motion

A Vue composables library providing smooth, declarative animations for components, HTML, and SVG elements. Inspired by Framer Motion and built on Popmotion, it features the v-motion directive, the useMotion composable, and support for SSR and Nuxt. It includes built-in animation presets and supports spring and keyframe transitions.

Tokens
15.4K
Snippets
46
Records
89
Agent score
82%

What's inside @vueuse/motion

  1. Overview of @vueuse/motion

    main

    @vueuse/motion provides composables to add smooth animations to your Vue components. It is a declarative API inspired by Framer Motion and uses Popmotion for its animation engine.

    Key features include:

    • Smooth animations powered by Popmotion.
    • Declarative API for easy component motion.
    • 20+ presets available for plug-and-play usage.
    • SSR Ready and first-class support for Nuxt 3.
    • TypeScript support.
    • Lightweight footprint with a bundle size of <20kb.
  2. Create and Trigger Custom Variants

    main

    You can define custom variants within the :variants prop and trigger them programmatically using the useMotions hook.

    1. Define your custom variants in the :variants object.
    2. Pass the name of the custom variant to the v-motion directive (e.g., v-motion="'customName'").
    3. Use useMotions() to get the motion instance for that element and update its .variant.value property.
    <template>
      <div v-motion="'customElement'" :initial="{ scale: 1, }" :variants="{ custom: { scale: 2, transition: { type: 'spring', stiffness: 100 } } }" />
    </template>
    
    <script setup>
    import { useMotions } from '@vueuse/motion'
    
    // Access the motion instance using useMotions.
    const { customElement } = useMotions()
    
    // Trigger the custom variant
    const yourCustomEvent = () => {
      customElement.variant.value = 'custom'
    }
    </script>
  3. Use the `<Motion>` component

    main

    The <Motion> component allows you to implement animations directly within your template. It is an alternative to the v-motion directive that works better in Server-Side Rendering (SSR) environments. You can specify which element to render using the is prop (defaults to div).

    <template>
      <Motion is="p" preset="slideVisibleLeft">Text in Motion!</Motion>
    </template>
  4. Use the v-motion directive

    main

    The v-motion directive allows you to define animations directly in your Vue templates without wrapper components. It works on HTML elements, SVG elements, and Vue components. You can define animation states using specific variant props or a single :variants object.

    <template>
      <div
        v-motion
        :initial="{ opacity: 0, y: 100 }"
        :enter="{ opacity: 1, y: 0, scale: 1 }"
        :variants="{ custom: { scale: 2 } }"
        :hovered="{ scale: 1.2 }"
        :delay="200"
        :duration="1200"
      />
    </template>
  5. Use the useMotion composable for animations

    main

    The useMotion composable is the core way to create animations within the Composition API setup hook. It allows you to bind animations to a target element using variants.

    useMotion accepts three parameters:

    1. target: An HTML or SVG element, or a Vue ref pointing to one.
    2. variants: An object defining different animation states (e.g., initial, enter).
    3. (Implicitly) Other configuration options.

    Calling useMotion returns a Motion Instance, which provides methods to control and trigger animations programmatically.

    <script setup>
    import { ref } from 'vue'
    import { useMotion } from '@vueuse/motion'
    
    const target = ref<HTMLElement>()
    
    const motionInstance = useMotion(target, {
        initial: {
            opacity: 0,
            y: 100
        },
        enter: {
            opacity: 1,
            y: 0
        }
    })
    </script>
    
    <template>
      <div ref="target">Animated Element</div>
    </template>
  6. Register the MotionPlugin in your Vue app

    main

    To use the motion features, you must register the MotionPlugin in your Vue application entry file (e.g., main.js or app.ts).

    import { createApp } from "vue";
    import { MotionPlugin } from "@vueuse/motion";
    import App from "./App.vue";
    
    const app = createApp(App);
    
    app.use(MotionPlugin);
    
    app.mount("#app");