# Motion

> Animation on by default and controlled from the theme: overlays spring out of their triggers, active indicators slide, lists animate, and sections reveal on scroll.

Status: beta. Category: Patterns.

```ts
import { Reveal, Stagger, StaggerItem, AnimatedNumber, PageTransition, Presence } from '@glyphui/ui'
```

Exports: `Reveal`, `Stagger`, `StaggerItem`, `AnimatedNumber`, `PageTransition`, `Presence`, `ActiveIndicator`, `m`, `useMotionSettings`.

## Notes

- StaggerItem orders itself by document order when you leave out index; pass index only to override the cascade.
- Set theme.motion.level to none, subtle or expressive, and theme.motion.spring to { stiffness, damping, mass? } — overlays, indicators and reveals all follow it. Brutalist snaps with no bounce; Playful is expressive.
- GlyphProvider motion="none" overrides the theme for the whole app, e.g. in tests or from a user setting. Visitors who ask their system for reduced motion never get animation.
- The spring is compiled to a CSS linear() easing (--glyph-ease-spring, with --glyph-duration-spring), so plain CSS can use it: ease-spring duration-(--glyph-duration-spring).
- Content never depends on JavaScript to become visible: server HTML, tests and reduced-motion visitors see it immediately; only browsers that can animate it hide it first.
- The motion library loads its animation features lazily on first render; Button and other CSS-only components never import it.
- Blocks animate by default; pass animate={false} to any block to turn its entrance off.

## Reveal props

| Prop | Type | Default | Description |
|---|---|---|---|
| `amount` | `number` |  | Share of the element that must be visible, 0–1. Default 0.15. |
| `asChild` | `boolean` |  |  |
| `delay` | `number` |  | Delay before the entrance starts, in milliseconds. |
| `enabled` | `boolean` |  | Set false to show the content without animating. |
| `once` | `boolean` |  | Animate only the first time it scrolls into view. Default true. |
| `scale` | `boolean` |  | Start slightly smaller, for cards and media. |
| `y` | `string \| number` |  | Distance it rises from, in px or any CSS length. Defaults to the motion level's. |

## Example: Motion levels

The same components at each level. `subtle` is the default; `expressive` travels further and bounces with a soft spring; `none` renders everything instantly.

```tsx
import {
  AnimatedNumber,
  Card,
  Checkbox,
  defineTheme,
  type MotionLevel,
  SegmentedControl,
  Stagger,
  Switch,
  Tabs,
  TabsContent,
  TabsList,
  TabsTrigger,
  Text,
  ThemeScope,
  useGlyph,
} from '@glyphui/ui'
import { useMemo, useState } from 'react'

export default function Example() {
  const { theme } = useGlyph()
  const [level, setLevel] = useState<MotionLevel>('subtle')
  const [replay, setReplay] = useState(0)
  const [seats, setSeats] = useState(12)
  const scoped = useMemo(
    () => defineTheme({ name: `${theme.name}-motion-${level}`, extends: theme, motion: { level } }),
    [theme, level],
  )

  return (
    <div className="grid w-full gap-4">
      <SegmentedControl<MotionLevel>
        aria-label="Motion level"
        className="justify-self-start"
        value={level}
        onValueChange={(next) => {
          setLevel(next)
          setReplay((n) => n + 1)
        }}
        options={[
          { value: 'none', label: 'None' },
          { value: 'subtle', label: 'Subtle' },
          { value: 'expressive', label: 'Expressive' },
        ]}
      />
      <ThemeScope theme={scoped} className="grid gap-5 rounded-lg p-5">
        <div className="flex flex-wrap items-center gap-6">
          <Tabs defaultValue="overview">
            <TabsList>
              <TabsTrigger value="overview">Overview</TabsTrigger>
              <TabsTrigger value="activity">Activity</TabsTrigger>
              <TabsTrigger value="settings">Settings</TabsTrigger>
            </TabsList>
            <TabsContent value="overview" className="sr-only" tabIndex={-1}>
              Overview
            </TabsContent>
            <TabsContent value="activity" className="sr-only" tabIndex={-1}>
              Activity
            </TabsContent>
            <TabsContent value="settings" className="sr-only" tabIndex={-1}>
              Settings
            </TabsContent>
          </Tabs>
          <SegmentedControl
            aria-label="View"
            size="sm"
            defaultValue="list"
            options={[
              { value: 'list', label: 'List' },
              { value: 'board', label: 'Board' },
              { value: 'timeline', label: 'Timeline' },
            ]}
          />
          <Switch aria-label="Notifications" defaultChecked />
          <Checkbox aria-label="Done" />
        </div>
        <Stagger key={replay} className="grid gap-3 sm:grid-cols-3">
          {['Revenue', 'Seats', 'Uptime'].map((label, i) => (
            <Card key={label} padding="sm">
              <Text size="small" tone="muted">
                {label}
              </Text>
              <p className="font-display text-h3">
                {i === 1 ? (
                  <button
                    type="button"
                    className="cursor-pointer"
                    onClick={() => setSeats((n) => n + 7)}
                  >
                    <AnimatedNumber value={seats} />
                  </button>
                ) : i === 0 ? (
                  '$48k'
                ) : (
                  '99.98%'
                )}
              </p>
            </Card>
          ))}
        </Stagger>
      </ThemeScope>
    </div>
  )
}
```

## Example: Reveal and Stagger

Entrances on scroll. Every Glyph block uses them, so landing pages come alive with no code.

```tsx
import { Button, Card, Reveal, Stagger, Text } from '@glyphui/ui'
import { RotateCcw } from 'lucide-react'
import { useState } from 'react'

const features = ['Automatic retries', 'Revenue reports', 'Audit logs', 'Usage billing']

export default function Example() {
  const [run, setRun] = useState(0)
  return (
    <div className="grid w-full gap-6">
      <Button
        variant="outline"
        size="sm"
        leftIcon={<RotateCcw />}
        className="justify-self-start"
        onClick={() => setRun((n) => n + 1)}
      >
        Replay
      </Button>
      <div key={run} className="grid gap-6">
        <Reveal className="grid gap-1">
          <p className="font-display text-h3">Billing that keeps up</p>
          <Text tone="muted">Reveal fades and lifts a block in when it scrolls into view.</Text>
        </Reveal>
        <Stagger as="ul" className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
          {features.map((feature) => (
            <li key={feature}>
              <Card padding="sm" className="h-full">
                <Text weight="medium">{feature}</Text>
              </Card>
            </li>
          ))}
        </Stagger>
      </div>
    </div>
  )
}
```

## Example: AnimatedNumber

```tsx
import { AnimatedNumber, Button, Card, Text } from '@glyphui/ui'
import { useState } from 'react'

export default function Example() {
  const [mrr, setMrr] = useState(48210)
  return (
    <Card className="w-full max-w-sm gap-3">
      <Text size="small" tone="muted">
        Monthly recurring revenue
      </Text>
      <p className="font-display text-h1">
        <AnimatedNumber
          value={mrr}
          locale="en-US"
          format={{ style: 'currency', currency: 'USD', maximumFractionDigits: 0 }}
        />
      </p>
      <div className="flex gap-2">
        <Button size="sm" onClick={() => setMrr((n) => n + 1840)}>
          New customer
        </Button>
        <Button size="sm" variant="outline" onClick={() => setMrr((n) => Math.max(0, n - 920))}>
          Churn
        </Button>
      </div>
    </Card>
  )
}
```

## Example: Page transitions

Wrap your router outlet and key it by path. With React Router: <PageTransition transitionKey={useLocation().pathname}><Outlet /></PageTransition>.

```tsx
import { PageTransition, SegmentedControl, Text } from '@glyphui/ui'
import { useState } from 'react'

const pages = {
  inbox: 'Three unread conversations, one waiting on you.',
  issues: '12 open issues, 3 due this week.',
  reports: 'Revenue is up 11.9% month over month.',
} as const
type Page = keyof typeof pages

export default function Example() {
  const [page, setPage] = useState<Page>('inbox')
  return (
    <div className="grid w-full max-w-md gap-4">
      <SegmentedControl<Page>
        aria-label="Page"
        value={page}
        onValueChange={setPage}
        options={[
          { value: 'inbox', label: 'Inbox' },
          { value: 'issues', label: 'Issues' },
          { value: 'reports', label: 'Reports' },
        ]}
      />
      {/* With a router: transitionKey={useLocation().pathname} around <Outlet />. */}
      <PageTransition
        transitionKey={page}
        className="grid gap-1 rounded-lg border border-border p-5"
      >
        <p className="font-display text-h4 capitalize">{page}</p>
        <Text tone="muted">{pages[page]}</Text>
      </PageTransition>
    </div>
  )
}
```

## Example: Custom enter and exit

Presence and m.* elements from motion, sharing the theme’s spring.

```tsx
import { Button, m, Presence, Text } from '@glyphui/ui'
import { useState } from 'react'

export default function Example() {
  const [open, setOpen] = useState(true)
  return (
    <div className="grid w-full max-w-sm gap-4">
      <Button variant="outline" className="justify-self-start" onClick={() => setOpen((o) => !o)}>
        {open ? 'Dismiss' : 'Show'} banner
      </Button>
      <Presence>
        {open && (
          <m.div
            key="banner"
            initial={{ opacity: 0, y: -8, scale: 0.98 }}
            animate={{ opacity: 1, y: 0, scale: 1 }}
            exit={{ opacity: 0, y: -8, scale: 0.98 }}
            className="rounded-lg border border-border bg-surface p-4 shadow-sm"
          >
            <Text weight="medium">Your trial ends in 3 days</Text>
            <Text size="small" tone="muted">
              Pick a plan to keep your invoices sending.
            </Text>
          </m.div>
        )}
      </Presence>
    </div>
  )
}
```

Live page: https://glyphui.xyz/components/motion
