# Motion

> Motion levels, springs, reduced motion and the animation primitives.

Glyph animates by default, and the theme decides how much. Overlays spring out of the element that opened them, active indicators slide between tabs, list items enter and leave, and page sections reveal as they scroll into view. None of it needs code in your app.

## Motion levels

`theme.motion.level` is one of three values:

| Level | What it does |
|---|---|
| `none` | Renders everything instantly. Exit events still fire, so overlays close normally. |
| `subtle` | The default: short, purposeful movement. |
| `expressive` | Travels further and overshoots slightly on a soft spring. |

```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>
  )
}
```

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

The spring behind overlays, indicators and reveals is also part of the theme:

```ts title="src/glyph.theme.ts"
import { defineTheme, presets } from '@glyphui/ui'

export default defineTheme({
  name: 'acme',
  extends: presets.glyph,
  motion: {
    level: 'expressive',
    // Lower damping bounces more; higher stiffness is faster.
    spring: { stiffness: 380, damping: 22 },
    duration: { fast: '120ms', base: '180ms', slow: '280ms' },
  },
})
```

The presets differ here too: brutalist snaps with no bounce, playful is expressive.

## Reduced motion

Visitors who ask their operating system for reduced motion never get animation, whatever the theme says. Content never depends on an animation to become visible: server-rendered HTML, tests and reduced-motion visitors see everything at once.

To turn motion off for everyone, for example from a user setting or in tests, override the theme on the provider:

```tsx title="src/root.tsx"
import { GlyphProvider } from '@glyphui/ui'
import type { ReactNode } from 'react'

export function Root({ calm, children }: { calm: boolean; children: ReactNode }) {
  return <GlyphProvider motion={calm ? 'none' : undefined}>{children}</GlyphProvider>
}
```

## Reveal and Stagger

`Reveal` fades and lifts its child in when it scrolls into view. `Stagger` does the same for each direct child in turn: card grids, feature lists, pricing tiers.

```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>
  )
}
```

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

```tsx title="src/features.tsx"
import { Card, Reveal, Stagger } from '@glyphui/ui'

export function Features({ items }: { items: { title: string; body: string }[] }) {
  return (
    <>
      <Reveal>
        <h2 className="font-display text-h2">Why teams switch</h2>
      </Reveal>
      <Stagger as="ul" className="grid gap-4 sm:grid-cols-3">
        {items.map((item) => (
          <li key={item.title}>
            <Card>
              <h3 className="font-display text-h4">{item.title}</h3>
              <p className="text-small text-muted-foreground">{item.body}</p>
            </Card>
          </li>
        ))}
      </Stagger>
    </>
  )
}
```

Every block in `@glyphui/blocks` already uses them. Pass `animate={false}` to a block to turn its entrance off.

## Animated numbers

`AnimatedNumber` counts to its value on a spring and formats it with `Intl.NumberFormat`. Screen readers get the final value, not every step.

```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>
  )
}
```

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

## Page transitions

Wrap your router's outlet and key it by path. Only the new page animates in; the old one leaves at once, so pages never overlap:

```tsx title="src/layout.tsx"
import { PageTransition } from '@glyphui/ui'
import { Outlet, useLocation } from 'react-router'

export function Layout() {
  const { pathname } = useLocation()
  return (
    <PageTransition transitionKey={pathname}>
      <Outlet />
    </PageTransition>
  )
}
```

The first page renders without an entrance.

## Custom animations

For your own enter and exit animations, use `Presence` and `m.*` elements from the [motion](https://motion.dev) library, re-exported by Glyph. They share the theme's spring and turn off with reduced motion:

```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 example: https://glyphui.xyz/components/motion

In plain CSS, the spring is available as an easing: `ease-spring duration-(--glyph-duration-spring)` in Tailwind, or `var(--glyph-ease-spring)` and `var(--glyph-duration-spring)`.

## Cost

Buttons and other CSS-only components never load the motion library. The library's animation features load after the first render, so they don't delay the page. [`useMotionSettings()`](https://glyphui.xyz/components/motion) tells your own components the current level and spring.

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