# Motion

Motion is CSS-first, token-driven and close to platform behavior: short fades, a drawer that
slides from its edge, popovers and menus that grow a little as they appear, and color changes on
buttons, links and the switch. Nothing waits on a transition to work.

- **Transitions only, no keyframe animations.** Every component uses
  [`transition`](https://developer.mozilla.org/en-US/docs/Web/CSS/transition). Dialogs, drawers and
  popovers animate in and out of the top layer with
  [`@starting-style`](https://developer.mozilla.org/en-US/docs/Web/CSS/@starting-style) and
  [`transition-behavior: allow-discrete`](https://developer.mozilla.org/en-US/docs/Web/CSS/transition-behavior)
  ([CSS Transitions 2](https://drafts.csswg.org/css-transitions-2/#transition-behavior-property)).
  An engine without them opens and closes the element with no transition.
- **Tokens drive every duration and easing.** `--dui-motion-duration-fast`, `-normal` and `-slow`
  are primitives times `--dui-motion-scale` (default `1`), so one value on `:root` makes all motion
  slower (`2`) or off (`0`). Easings: `--dui-motion-easing-standard` for state changes,
  `--dui-motion-easing-enter` for things arriving, `--dui-motion-easing-exit` for things leaving, and
  `--dui-motion-easing-spring` for popover, menu and drawer entrances: a critically damped spring written with
  [`linear()`](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Values/easing-function/linear), set only
  inside `@supports`, so an engine without `linear()` keeps the enter curve.
- **Movement is a separate knob.** `--dui-motion-movement` is `1` by default. Distances
  (`--dui-motion-distance`, `--dui-motion-zoom`, `--dui-motion-press-scale`, the drawer's off-screen offset) and the duration of
  every [`translate`](https://developer.mozilla.org/en-US/docs/Web/CSS/translate) or
  [`scale`](https://developer.mozilla.org/en-US/docs/Web/CSS/scale) transition are multiplied by it.
- **Reduced motion keeps fades and drops movement.** Under
  [`prefers-reduced-motion: reduce`](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion)
  ([Media Queries 5](https://www.w3.org/TR/mediaqueries-5/#prefers-reduced-motion)) movement becomes `0`:
  the drawer fades in place, popovers fade without growing, the switch thumb jumps while its color
  still fades. This is what [2.3.3 Animation from Interactions](https://www.w3.org/WAI/WCAG22/Understanding/animation-from-interactions.html)
  asks for. It says that motion animation triggered by interaction can be disabled. **Our choice:** keep opacity
  and color transitions under reduced motion, because a fade is not motion animation and it still
  shows where a change came from.
- **`data-motion` on any element.** `data-motion="reduced"` behaves exactly like the media query for
  everything inside. `data-motion="none"` sets `--dui-motion-enabled` to `0`, so every duration is `0s`
  even where a `--dui-motion-scale` is set inside it. An empty `data-motion` is a scope
  for your own `--dui-motion-*` overrides: motion tokens are re-declared on it, as theme tokens are on
  `[data-theme]` ([CSS Variables](https://www.w3.org/TR/css-variables-1/#cycles)).
  **Our choice:** no `data-motion="full"`, so a page can never override a user's reduced-motion setting.
- **Essential feedback never animates.** Focus indicators, validation errors and `aria-invalid`,
  alert and live-region text, and loading state (`aria-busy`) change at once. **Our choice:** text
  fields do not animate their border at all, because the same border shows the invalid state.
- **View transitions are progressive enhancement only.** domestic-ui starts none and nothing depends on
  one. When your script calls `document.startViewTransition()`
  ([View Transition API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API),
  [CSS View Transitions](https://drafts.csswg.org/css-view-transitions-1/)), the base layer gives its
  pseudo-elements the motion tokens inside `@supports`, so they follow the scale and reduced motion too.

| Transition | Components | Duration | Moves |
| --- | --- | --- | --- |
| Fade and lift | Dialog, toast | `--dui-motion-duration-normal` | `translate` by `--dui-motion-distance` |
| Slide and fade | Drawer | `--dui-motion-duration-slow`, spring | `translate` by 100% of its width |
| Scale and fade | Popover, menu | `--dui-motion-duration-fast`, spring | `scale` from `--dui-motion-zoom` |
| Color | Buttons, links, switch track | `--dui-motion-duration-fast` | No |
| Press | Buttons (`:active`) | `--dui-motion-duration-fast` | `scale` to `--dui-motion-press-scale` (0.98) |
| Thumb slide | Switch | `--dui-motion-duration-fast` | `translate` |

**Our choice:** 120, 200 and 300ms, longer for larger surfaces, with a decelerating (or spring) enter and an
accelerating exit, because short transitions keep the UI responsive and the curves match how things
arrive and leave. [`examples/motion.html`](https://domestic-ui.odonckers.dev/principles/motion.md) shows each transition, the scale, and reduced
and no-motion scopes side by side.

## Page transitions (opt-in)

`css/domestic.css` does not turn on page transitions. A site that wants them adds the
"Page transitions CSS" snippet below to its own stylesheet:
[`@view-transition { navigation: auto }`](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@view-transition)
makes same-origin navigations cross-fade ([CSS View Transitions Level 2](https://www.w3.org/TR/css-view-transitions-2/)),
a unique `view-transition-name` keeps persistent chrome such as the header in place, and the motion tokens set the
duration and easing, so `data-motion="none"` on the root element makes it instant. Under
`prefers-reduced-motion: reduce` it is off ([2.3.3 Animation from Interactions](https://www.w3.org/WAI/WCAG22/Understanding/animation-from-interactions.html)).
Browsers without support navigate as before, with no script. These docs use it: the app bar and the Docs
sidebar stay while main cross-fades. Smashing Magazine lists it among
[front-end features for designers](https://smashingmagazine.com/2024/12/new-front-end-features-for-designers-in-2025), for comparison.

## Live example

### Fade: dialog

```html
<button type="button" commandfor="fade-dialog" command="show-modal">Open dialog</button>
<dialog id="fade-dialog" data-dialog aria-labelledby="fade-dialog-title">
  <header>
    <h2 id="fade-dialog-title">Fade in, fade out</h2>
  </header>
  <p>Enters with <code>--dui-motion-easing-enter</code> and leaves with <code>--dui-motion-easing-exit</code>.</p>
  <form method="dialog">
    <footer>
      <button type="submit" autofocus>Close</button>
    </footer>
  </form>
</dialog>
```

### Page transitions CSS

In the `<head>` of the live example above.

```html
<style id="page-transitions">
  /* Opt in on every page of your site: same-origin navigations cross-fade. */
  @view-transition {
    navigation: auto;
  }

  @media (prefers-reduced-motion: reduce) {
    @view-transition {
      navigation: none;
    }
  }

  /* Chrome that stays put while the content cross-fades: one unique name each. */
  body > header {
    view-transition-name: page-header;
  }

  ::view-transition-group(*),
  ::view-transition-old(*),
  ::view-transition-new(*) {
    animation-duration: var(--dui-motion-duration-normal);
    animation-timing-function: var(--dui-motion-easing-standard);
  }
</style>
```

### Slide: drawer

```html
<button type="button" commandfor="slide-drawer" command="show-modal">Open drawer</button>
<dialog id="slide-drawer" data-dialog="drawer end" closedby="any" aria-labelledby="slide-drawer-title">
  <header>
    <h2 id="slide-drawer-title">Drawer</h2>
    <button type="button" commandfor="slide-drawer" command="close" data-button="quiet small">Close</button>
  </header>
  <p>The off-screen offset is 100% times <code>--dui-motion-movement</code>.</p>
</dialog>
```

### Scale: popover and menu

```html
<div class="cluster">
  <button type="button" popovertarget="scale-popover">Popover</button>
  <div id="scale-popover" popover data-popover>
    <p>Grows and fades in.</p>
  </div>
  <dui-menu>
    <button type="button" popovertarget="scale-menu">Menu</button>
    <div id="scale-menu" popover data-popover>
      <button type="button">Rename</button>
      <button type="button">Duplicate</button>
    </div>
  </dui-menu>
</div>
```

### Color and state

```html
<div class="cluster">
  <button type="button">Hover me</button>
  <button type="button" data-button="primary">Primary</button>
  <a href="#state-title">A link</a>
  <label><input type="checkbox" role="switch" name="state-switch"> Switch</label>
</div>
```

### Toast

```html
<button type="button" data-toast="success">Save</button>
<dui-toaster role="status"></dui-toaster>
```

### Motion scale

```html
<div class="panel cluster" data-motion style="--dui-motion-scale: 3">
  <button type="button">Hover me</button>
  <label><input type="checkbox" role="switch" name="slow-switch"> Slow switch</label>
  <button type="button" popovertarget="slow-popover">Slow popover</button>
  <div id="slow-popover" popover data-popover>
    <p>Three times the normal duration.</p>
  </div>
</div>
```

### Reduced and no motion

```html
<div class="grid">
  <section class="panel stack" data-motion="reduced" aria-labelledby="reduced-title">
    <h3 id="reduced-title">data-motion="reduced"</h3>
    <label><input type="checkbox" role="switch" name="reduced-switch"> Switch</label>
    <div class="cluster">
      <button type="button" popovertarget="reduced-popover">Popover</button>
      <div id="reduced-popover" popover data-popover>
        <p>Fades without growing.</p>
      </div>
      <button type="button" commandfor="reduced-drawer" command="show-modal">Drawer</button>
    </div>
    <dialog id="reduced-drawer" data-dialog="drawer end" closedby="any" aria-labelledby="reduced-drawer-title">
      <header>
        <h2 id="reduced-drawer-title">Reduced motion</h2>
        <button type="button" commandfor="reduced-drawer" command="close" data-button="quiet small">Close</button>
      </header>
      <p>Fades in place instead of sliding.</p>
    </dialog>
  </section>
  <section class="panel stack" data-motion="none" aria-labelledby="none-title">
    <h3 id="none-title">data-motion="none"</h3>
    <label><input type="checkbox" role="switch" name="none-switch"> Switch</label>
    <div class="cluster">
      <button type="button" popovertarget="none-popover">Popover</button>
      <div id="none-popover" popover data-popover>
        <p>Appears at once.</p>
      </div>
      <button type="button" commandfor="none-drawer" command="show-modal">Drawer</button>
    </div>
    <dialog id="none-drawer" data-dialog="drawer end" closedby="any" aria-labelledby="none-drawer-title">
      <header>
        <h2 id="none-drawer-title">No motion</h2>
        <button type="button" commandfor="none-drawer" command="close" data-button="quiet small">Close</button>
      </header>
      <p>Appears at once.</p>
    </dialog>
  </section>
</div>
```

### View transition

```html
<div class="stack">
  <ol id="vt-list" class="stack" style="--stack-gap: var(--dui-space-2xs)">
    <li>Draft</li>
    <li>Review</li>
    <li>Publish</li>
  </ol>
  <p><button type="button" data-reverse="vt-list">Reverse the list</button></p>
</div>
```
