useScopeMotion creates a scope for animations and other Motion effects within an element. It provides:
- DOM selectors scoped to the supplied element.
- Shared cleanup for animations, scroll and view listeners, gestures, and resize observers.
- Reactive transition and media query options.
- Automatic cleanup when options change or the component unmounts.
<script setup lang="ts">
import { ref } from "vue"
import { animate, useScopeMotion } from "motion-v"
const root = ref<HTMLElement | null>(null)
useScopeMotion(root, () => {
animate("li", { opacity: [0, 1] })
})
</script>
<template>
<ul ref="root">
<li>First item</li>
<li>Second item</li>
</ul>
</template>In this example, the "li" selector only matches children of the element assigned to root.
Usage
Import
useScopeMotion is available via motion-v 2.9 and later.
import { useScopeMotion } from "motion-v"Call it with a DOM ref and a setup function: useScopeMotion(domRef, setup, options?).
The setup function receives a MotionScope. Motion effects started synchronously inside it are collected by the scope. The setup function can also return a cleanup function for other resources.
<script setup lang="ts">
import { ref } from "vue"
import { animate, useScopeMotion } from "motion-v"
const root = ref<HTMLElement | null>(null)
useScopeMotion(root, (scope) => {
const element = root.value
if (!element) return
const handleClick = scope.bind(() => {
animate("li", { x: [0, 20, 0] })
})
element.addEventListener("click", handleClick)
return () => element.removeEventListener("click", handleClick)
})
</script>
<template>
<ul ref="root">
<li>Click to animate</li>
</ul>
</template>Use scope.bind for event handlers that start Motion effects after setup. This ensures those effects belong to the same scope and are cleaned up with it.
Reactive options
The optional options argument accepts a value, ref, or getter. When reactive options change, the current scope is cancelled and a new one is created with the latest options.
<script setup lang="ts">
import { ref } from "vue"
import { animate, useScopeMotion } from "motion-v"
const root = ref<HTMLElement | null>(null)
const options = ref({ transition: { duration: 0.5 } })
useScopeMotion(root, () => {
animate("li", { opacity: [0, 1] })
}, options)
function updateDuration() {
options.value = { transition: { duration: 1 } }
}
</script>
<template>
<button @click="updateDuration">Use a longer duration</button>
<ul ref="root">
<li>Scoped item</li>
</ul>
</template>The transition option sets the default transition for every animate() call made in this scope.
Media conditions
Use matchMedia to run setup only while a media condition matches. When the condition changes, the scope is cancelled and setup runs again if the new conditions match.
<script setup lang="ts">
import { ref } from "vue"
import { animate, useScopeMotion } from "motion-v"
const root = ref<HTMLElement | null>(null)
useScopeMotion(root, () => {
animate("li", { x: [0, 20] })
}, {
matchMedia: "(prefers-reduced-motion: no-preference)",
})
</script>
<template>
<ul ref="root">
<li>Animated when motion is allowed</li>
</ul>
</template>matchMedia also accepts named conditions, such as { desktop: "(min-width: 800px)" }. With named conditions, setup runs when any of them matches. Their latest results are available through scope.media.
Automatic cleanup
The composable uses a post-flush watchEffect, so the DOM ref is resolved after mounting. When the effect re-runs or the component unmounts, it calls cancel() on the current scope.
Cancelling a scope stops its collected effects, runs its cleanup functions, removes its media listeners, and restores the inline styles of animated elements to their previous values.
API
domRef
A Vue ref for the element or component that roots the scope. DOM selectors used inside the scope resolve within this element.
setup
A function that receives the current MotionScope. It can return a cleanup function.
Motion effects started inside setup, or inside callbacks wrapped with scope.run() or scope.bind(), are collected by the scope.
options
An optional value, ref, or getter with the following properties:
transition: The default transition for animations in the scope.matchMedia: A media query, Motion media shorthand, or object of named conditions.
Return value
Returns ShallowRef<MotionScope | null>. Its value is null before the post-flush setup runs and after the component unmounts. While the scope exists, its value is the current MotionScope.
MotionScope
A scope provides the following properties and methods:
state:"active","stopped", or"cancelled". Async work can check this before starting an animation.media: The latest result of each namedmatchMediacondition.run(callback): Runs a callback inside the scope so effects it starts are collected.bind(callback): Returns a callback that runs inside the scope, useful for event handlers.ignore(callback): Runs a callback outside the scope so effects it starts are not collected.stop(): Stops collected effects and removes media listeners, leaving inline styles as they are.cancel(): Likestop(), but also restores the previous inline styles of animated elements.refresh(): Cancels the current effects, then runs setup again if the media conditions match.