---
url: /reference/items/Motion/examples.md
---
# Examples
## Action-driven playback
Autoplay is off by default, so the card only moves when asked to. The buttons are [`Action`](/reference/items/Action/) components targeting the `Motion` instance with the arrow syntax, the hover pattern combines `play()` and `reverse()` for a symmetric in/out animation, and the last button runs a one-off spin with `animate()` — clicking Play afterwards returns to the animation declared by the options.
:::code-group
```js
import { registerComponents } from '@studiometa/js-toolkit';
import { Action } from '@studiometa/ui';
import { Motion } from '@studiometa/ui-motion';
registerComponents(Action, Motion);
```
:::
## Full playback control
Every playback method on one animation: `play()`, `pause()`, `reverse()`, `stop()`, `cancel()` and `complete()`, plus a range input scrubbing through the animation with `seek()`. Stop commits the styles the element reached, while cancel reverts it to its pre-animation styles.
:::code-group
```js
import { registerComponents } from '@studiometa/js-toolkit';
import { Action } from '@studiometa/ui';
import { Motion } from '@studiometa/ui-motion';
registerComponents(Action, Motion);
```
:::
## Reacting to events with `DataBind`
The `motion-*` events bubble, so a single routing [`Action`](/reference/items/Action/) on an ancestor catches all five of them and forwards a label to a [`DataBind`](/reference/items/DataBind/) in a separate subtree — the status text never shares an element with the animation.
:::code-group
```js
import { registerComponents } from '@studiometa/js-toolkit';
import { Action, DataBind } from '@studiometa/ui';
import { Motion } from '@studiometa/ui-motion';
registerComponents(Action, DataBind, Motion);
```
:::
## Gestures
The `hover`, `press` and `inView` options apply keyframes while their state holds, through Motion's own gesture functions: hover filters out touch emulation, press responds to pointer and keyboard alike, and in-view uses a real `IntersectionObserver`. When the state ends, a new animation plays forward to the base values of the properties the gesture touched — the base values are read from the element itself, so there is nothing to declare.
:::code-group
```js
import { registerComponents } from '@studiometa/js-toolkit';
import { Motion } from '@studiometa/ui-motion';
registerComponents(Motion);
```
:::
## Spring entrance and exit for a `Dialog`
The [`Dialog`](/reference/items/Dialog/) component handles the top layer, focus and scroll lock, and both its lifecycle events bubble, so an [`Action`](/reference/items/Action/) can route them: the `open` event plays a spring entrance on the `Motion` box and the `close` event plays it in reverse — physics a CSS transition cannot express. Every closing interaction (button, backdrop, Esc) just calls `Dialog.close()`.
A `Motion` is not one of the dialog's [transition children](/reference/items/Dialog/js-api#what-the-dialog-waits-for), so the exit would be cut off when the native dialog hides. Handing each animation to [`event.detail.waitUntil()`](/reference/items/Dialog/js-api#extending-the-lifecycle-with-waituntil) is what makes the dialog wait for it.
::: tip Guard the `detail`
A `