--- 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 its lifecycle events are [extendable](/reference/items/Dialog/js-api#extending-the-choreography-with-waituntil): registering a promise with `event.detail.waitUntil()` makes the dialog wait for it. The `open` event plays a spring entrance on the `Motion` box, the `close` event plays it in reverse, and the dialog stays painted until the exit settles — physics a CSS transition cannot express. Every closing interaction (button, backdrop, Esc) just calls `Dialog.close()`. :::code-group ```js import { registerComponents } from '@studiometa/js-toolkit'; import { Action, Dialog } from '@studiometa/ui'; import { Motion } from '@studiometa/ui-motion'; registerComponents(Action, Dialog, Motion); ``` ::: ## Scrubbing with `TimerProgress` A single element carries `Action`, `Motion` and [`TimerProgress`](/reference/items/Timer/): the timer emits a `0 → 1` ratio on every frame, and the co-located `Action` hands it to `Motion.seek()`. The effect folds the ratio into a `0 → 1 → 0` triangle wave (`1 - Math.abs(2 * ratio - 1)`), so one timer cycle yoyos the animation out and back — a looping, pausable, restartable timeline without a line of custom JavaScript. For a plain forward loop, pass the ratio through unchanged; Motion can also yoyo on its own with `data-option-transition='{ "repeat": 10, "repeatType": "reverse" }'`, without any timer. :::code-group ```js import { registerComponents } from '@studiometa/js-toolkit'; import { Action, TimerProgress } from '@studiometa/ui'; import { Motion } from '@studiometa/ui-motion'; registerComponents(Action, TimerProgress, Motion); ``` :::