# Motion

Short, quiet and only where it explains a change: where something came from, or where it went.

API [Dialog](/ui/storybook/?path=/docs/ui-primitives-dialog--docs) [Sheet](/ui/storybook/?path=/docs/ui-primitives-sheet--docs) [Skeleton](/ui/storybook/?path=/docs/ui-primitives-skeleton--docs) [DropdownMenu](/ui/storybook/?path=/docs/ui-primitives-dropdownmenu--docs) [Tooltip](/ui/storybook/?path=/docs/ui-primitives-tooltip--docs) [useAutoHideHeader](/ui/storybook/?path=/docs/ui-hooks-useautohideheader--docs) [useScrollInsets](/ui/storybook/?path=/docs/ui-hooks-usescrollinsets--docs)

**Animate only to show where something came from or went, keep it to 200ms at most, and leave it out for anyone who asked the system for less motion.** In a tool used all day, motion that does not explain anything is waiting time.

```tsx
import { CI_RUN } from "@orz/fixtures/ci-run.ts";
import { Button } from "@orz/ui/components/button";
import { List, ListItem } from "@orz/ui/components/list";
import { Skeleton } from "@orz/ui/components/skeleton";
import { CircleCheckIcon, CircleXIcon, RotateCwIcon } from "lucide-react";
import { useEffect, useState } from "react";

const JOB_MS = 1200;

export function ArrivingJobs() {
  const [finished, setFinished] = useState(CI_RUN.jobs.length);
  const running = finished < CI_RUN.jobs.length;
  useEffect(() => {
    if (finished === CI_RUN.jobs.length) return;
    const timer = setTimeout(() => setFinished(finished + 1), JOB_MS);
    return () => clearTimeout(timer);
  }, [finished]);
  return (
    <div className="grid gap-3">
      <div className="flex items-center justify-between gap-2">
        <span className="font-medium text-ui">
          {CI_RUN.workflow} #{CI_RUN.id}
        </span>
        <Button
          variant="outline"
          size="sm"
          disabled={running}
          onClick={() => setFinished(0)}
        >
          <RotateCwIcon data-icon="inline-start" /> Re-run all jobs
        </Button>
      </div>
      <List purpose="collection">
        <ol aria-label="Jobs" className="text-ui">
          {CI_RUN.jobs.slice(0, finished).map((job) => (
            <ListItem key={job.id} asChild>
              <li className="flex min-h-row items-center gap-2 px-3 py-1.5 animate-in fade-in slide-in-from-top-1 duration-150 ease-out">
                {job.status === "failed" ? (
                  <CircleXIcon
                    aria-label="failed"
                    className="size-4 shrink-0 text-destructive"
                  />
                ) : (
                  <CircleCheckIcon
                    aria-label="passed"
                    className="size-4 shrink-0 text-success"
                  />
                )}
                <span className="flex-1">{job.name}</span>
                <span className="text-muted-foreground text-ui-sm tabular-nums">
                  {job.duration}s
                </span>
              </li>
            </ListItem>
          ))}
          {running && (
            <ListItem asChild>
              <li className="py-0.5">
                <Skeleton className="h-row w-full" />
                <span className="sr-only">
                  {CI_RUN.jobs[finished]?.name} is running
                </span>
              </li>
            </ListItem>
          )}
        </ol>
      </List>
    </div>
  );
}
```

Press **Re-run all jobs**. A `Skeleton` holds the place of the job that is running, and each job that finishes fades and drops in from just above in 150ms, so the eye finds the row that changed. With reduced motion turned on, the row fades in where it stands: `@orz/ui`'s stylesheet takes the drop out, as it does for its own overlays.

## What moves, and how long

The components already animate, and an app adds little to them. This is how the default style moves them; another [style](/ui/guidelines/foundations/styles/index.md) may move a part its own way.

| What                                           | Motion                                                                                                                                                           | Duration   |
| ---------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- |
| A menu, popover, select, hover card or tooltip | Fades in where it stands                                                                                                                                         | 100ms      |
| A dialog and its overlay                       | Fades in where it stands                                                                                                                                         | 100ms      |
| A sheet                                        | Fades in and slides 12px in from its edge                                                                                                                        | 100ms      |
| A menu, `FilterMenu` or select on a phone      | Fades in and slides 12px up from the bottom edge, as a sheet does; a submenu takes its place without moving, and one sheet handing over to another plays nothing | 100ms      |
| An auto-hiding header, once scrolling stops    | Settles fully in or out                                                                                                                                          | 150ms      |
| A block a link jumps to (`revealBlock`)        | A tint that fades out                                                                                                                                            | 2s         |
| A `Skeleton`                                   | Pulses until the content arrives                                                                                                                                 | until then |
| A `DelayedSkeleton`                            | Fades in once the wait has lasted 500ms; a quicker load shows nothing                                                                                            | 150ms      |

```tsx
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@orz/ui/components/dialog";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@orz/ui/components/dropdown-menu";
import { Kbd } from "@orz/ui/components/kbd";
import { NavList, NavListItem, NavListLink } from "@orz/ui/components/nav-list";
import { Pane } from "@orz/ui/components/pane";
import {
  Sheet,
  SheetContent,
  SheetDescription,
  SheetHeader,
  SheetTitle,
  SheetTrigger,
} from "@orz/ui/components/sheet";
import { ToolbarButton } from "@orz/ui/components/toolbar";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@orz/ui/components/tooltip";
import {
  ArchiveIcon,
  EllipsisIcon,
  FileTextIcon,
  ForwardIcon,
  InboxIcon,
  KeyboardIcon,
  LinkIcon,
  PanelLeftIcon,
  SendIcon,
} from "lucide-react";

const KEYS = [
  { key: "E", label: "Archive" },
  { key: "#", label: "Delete" },
  { key: "S", label: "Star" },
  { key: "C", label: "Compose" },
];

const PLACES = [
  { title: "Inbox", Icon: InboxIcon },
  { title: "Sent", Icon: SendIcon },
  { title: "Drafts", Icon: FileTextIcon },
  { title: "Archive", Icon: ArchiveIcon },
];

export function OverlayMotion() {
  return (
    <TooltipProvider>
      <Pane purpose="toolbar" className="flex flex-wrap items-center gap-1">
        {/* A row a hit area tall on touch, so when the bar wraps on a phone
            Keyboard shortcuts stands clear of the buttons above it. */}
        <div className="flex items-center gap-1 touch:min-h-touch">
          <Sheet>
            <Tooltip>
              <TooltipTrigger asChild>
                <SheetTrigger asChild>
                  <ToolbarButton aria-label="Folders">
                    <PanelLeftIcon />
                  </ToolbarButton>
                </SheetTrigger>
              </TooltipTrigger>
              <TooltipContent>Folders</TooltipContent>
            </Tooltip>
            <SheetContent side="left" className="w-64">
              <SheetHeader>
                <SheetTitle>Folders</SheetTitle>
                <SheetDescription className="sr-only">
                  Mail's folders
                </SheetDescription>
              </SheetHeader>
              <nav aria-label="Folders" className="px-2">
                <NavList>
                  {PLACES.map(({ title, Icon }) => (
                    <NavListItem key={title}>
                      <NavListLink current={title === "Inbox"} asChild>
                        <button type="button">
                          <Icon />
                          {title}
                        </button>
                      </NavListLink>
                    </NavListItem>
                  ))}
                </NavList>
              </nav>
            </SheetContent>
          </Sheet>
          <Tooltip>
            <TooltipTrigger asChild>
              <ToolbarButton aria-label="Archive">
                <ArchiveIcon />
              </ToolbarButton>
            </TooltipTrigger>
            <TooltipContent>Archive</TooltipContent>
          </Tooltip>
          <DropdownMenu>
            <Tooltip>
              <TooltipTrigger asChild>
                <DropdownMenuTrigger asChild>
                  <ToolbarButton aria-label="More actions">
                    <EllipsisIcon />
                  </ToolbarButton>
                </DropdownMenuTrigger>
              </TooltipTrigger>
              <TooltipContent>More actions</TooltipContent>
            </Tooltip>
            <DropdownMenuContent align="start" className="w-44">
              <DropdownMenuItem>
                <ForwardIcon /> Forward
              </DropdownMenuItem>
              <DropdownMenuItem>
                <LinkIcon /> Copy link
              </DropdownMenuItem>
            </DropdownMenuContent>
          </DropdownMenu>
        </div>
        <Dialog>
          <DialogTrigger asChild>
            <ToolbarButton size="default">
              <KeyboardIcon data-icon="inline-start" /> Keyboard shortcuts
            </ToolbarButton>
          </DialogTrigger>
          <DialogContent className="sm:max-w-sm">
            <DialogHeader>
              <DialogTitle>Keyboard shortcuts</DialogTitle>
              <DialogDescription className="sr-only">
                The keys Mail answers to
              </DialogDescription>
            </DialogHeader>
            <dl className="grid grid-cols-[1fr_auto] gap-x-6 gap-y-1.5 text-ui">
              {KEYS.map(({ key, label }) => (
                <div key={key} className="contents">
                  <dt>{label}</dt>
                  <dd className="justify-self-end">
                    <Kbd>{key}</Kbd>
                  </dd>
                </div>
              ))}
            </dl>
          </DialogContent>
        </Dialog>
      </Pane>
    </TooltipProvider>
  );
}
```

Mail's thread toolbar. Hover **Archive** for its tooltip, open **More actions** for the menu, **Keyboard shortcuts** for the dialog, and **Folders** for the sheet: each fades in, and none moves further than it takes to show where it came from. The `Skeleton` is in the run above; the auto-hiding header and `revealBlock` need a page that scrolls, and their Storybook pages show them.

Motion of your own stays in the same range: 100 to 150ms for something small appearing, up to 200ms for something that crosses the screen. Something entering eases out (fast, then settling); something leaving eases in. Leaving is quicker than entering, because nobody needs to watch a thing go.

## When not to

- **Not while a finger drives it.** A sheet dragged by a thumb, a header pulled by a scroll, a slider: the element follows the finger with no transition, and animates only to settle once the finger lets go. A transition there trails the finger and feels broken.
- **Not on what the user is reading.** A list that re-sorts, a log that appends, a value that updates in place: change it, do not slide it. Motion draws the eye, and the eye was busy.
- **Not to fill a wait.** A spinner or skeleton says something is coming; it does not make it arrive sooner. A result that comes back within a few hundred milliseconds needs no loading state at all.
- **Not in a loop.** Anything that moves on its own for more than five seconds needs a way to stop it ([WCAG 2.2.2](https://www.w3.org/WAI/WCAG22/Understanding/pause-stop-hide.html)); a `Skeleton` stops by being replaced.

**Do**

Put movement of your own behind `motion-safe:`, as in a chevron that turns with `motion-safe:transition-transform`, so only people who have not asked for less see it.

**Don't**

Animate layout (height, width, position of large areas) or loop an animation for attention. It costs frames on a phone and makes some readers dizzy.

## Reduced motion

People who get motion sick, or who find movement distracting, turn on **Reduce motion** in their system settings, and the browser reports it as `prefers-reduced-motion: reduce`. Honouring it is one of the rules on [Accessibility](/ui/guidelines/content/accessibility/index.md#motion-and-zoom).

- **`@orz/ui`'s overlays already honour it.** A menu, popover, select, hover card, tooltip, dialog or sheet keeps its fade and drops any zoom or slide its style gives it, and the auto-hiding header snaps instead of settling. It comes with `@orz/ui/styles.css`, with nothing to turn on, and covers any markup of your own drawn with the same `animate-in` / `animate-out` classes (`slide-in-from-*`, `zoom-in-*`, `spin-in-*`, `blur-in-*`).
- **Reduced means a fade, not nothing.** Opacity still changes, so something appearing reads as arriving rather than blinking in; translation, scale, rotation, blur and parallax go.
- **Motion you add any other way is yours to gate.** A transform transition, a keyframe of your own or Tailwind's `animate-bounce` goes behind `motion-safe:` (or is undone with `motion-reduce:`); motion driven from script checks `matchMedia("(prefers-reduced-motion: reduce)")` first, as `@orz/ui-content`'s image viewer does before it eases its pane to a new height.
- **A spinner keeps spinning**, because the motion is the status, and a small transition that answers the hand directly, such as a `Switch` thumb crossing its track, stays too.

## On each platform

- **Pointer.** Hover feedback is a colour change with `transition-colors`, never a movement.
- **Touch.** A press shows at once, with no transition to wait for, and nothing moves under the finger before it lifts.

## In the demos

- [Mail](/ui/demos/mail/index.md): Send a reply: the thread scrolls down to it, or jumps there under reduced motion. The folders' Sheet slides in from its edge, and Refresh spins while it checks.
