<!-- generated by scripts/gen-docs.mjs from docs/system/spinner.md. Edit the source and rerun. -->
# Spinner

## Description
Says that something is being read, in the slot where the answer will land, for people who see and for people who listen.

`import { Spinner } from "@/components/cobalt/ui/spinner"`, source `registry/cobalt/ui/spinner.tsx`, status `ready`.
Foundation: `shadcn spinner (base-luma, base)`. Traps checked: `trap/loader-small-dots`, `trap/reduced-motion-ignored`, `trap/loop-offscreen`, `trap/motion-linear`, `trap/live-region-double`, `trap/loading-layout-shift`, `trap/loading-label-swap`, `trap/color-only-status`, `trap/demo-on-timers` (n/a: no example steps an animation on a timer; the waits stand in for a server), `trap/illustration-announced` (n/a: the glyph is `aria-hidden` and the label is text).

### Foundation
| Given by the foundation | Added by the team |
|---|---|
| The loader glyph with `role="status"`, an `aria-label` of "Loading" and `animate-spin` at `size-4` | A `span` slot the glyph fills with `size-full`, so `className="size-6"` on the slot scales it (D27); the label as `sr-only` text in the slot instead of `aria-label`, so it can name the wait, read in place; no live role by default, since a region mounted with its words is often skipped and a host that waits usually says so itself; `announce` for the rare spinner that is the only sign of a wait, an empty `role="status"` that takes the label 100ms after it mounts; `aria-hidden` on the glyph; `motion-reduce:animate-none`, so the ring stands still for people who asked for less motion |

## Examples
A search with the ring alone where the results go, and a card with a status line beside it. Each holds the results' height while it waits, so the answer moves nothing below it (the search moved the page 8px twice at 390 before), and the docs frame's Replay remounts each to watch its first search again; nothing in the examples is there only to replay them.

Real uses, 0 call sites (`rg -n "<Spinner\b" app docs/system/examples --glob '!docs/system/examples/spinner/*'`): the showcase chrome renders no Spinner of its own yet, and the examples folder does not count.

Uses inside the registry, which other components build on (planned as product uses once the first screen lands on the system):
- The settling button's pending state, inside an `aria-hidden` cell while the button's own status line speaks, `registry/cobalt/ui/state-button.tsx:130`.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/spinner/default.tsx` | default | A search whose answer has no known shape, with the 24px ring alone where the results go once the wait passes 250ms, named for what it waits on; the box holds the results' height |
| Not applicable: spinning is the rest state, shown in default.tsx under `(prefers-reduced-motion: no-preference)` | state:spinning | The 1s turn |
| Not applicable: still is reached with the OS setting on any example | state:still | The same ring, not turning |
| `docs/system/examples/spinner/in-card.tsx` | composition:Card | A card searching, with one status line in the results' place until they arrive |

Example: A search whose answer has no known shape, with the 24px ring alone where the results go once the wait passes 250ms, named for what it waits on; the box holds the results' height (`docs/system/examples/spinner/default.tsx`)

```tsx
import { useCallback, useEffect, useRef, useState } from "react";

import { Empty, EmptyAction, EmptyContent, EmptyDescription, EmptyHeader, EmptyTitle } from "@/components/cobalt/ui/empty";
import { Item, ItemContent, ItemDescription, ItemGroup, ItemTitle } from "@/components/cobalt/ui/item";
import { Spinner } from "@/components/cobalt/ui/spinner";
import { useSlowWait } from "@/lib/cobalt/use-slow-wait";

const filings = [
  { filer: "Harbour Capital", form: "13F for the second quarter" },
  { filer: "Northgate Partners", form: "Form ADV amendment" },
  { filer: "Calder Investment Management", form: "13F for the second quarter" },
];

// Stands in for your API call. It answers after 1200ms, and fails while the browser is offline or the
// address has ?network=offline.
const request = () =>
  new Promise<typeof filings>((resolve, reject) =>
    setTimeout(() => {
      const offline = !navigator.onLine || new URLSearchParams(location.search).get("network") === "offline";
      if (offline) reject(new Error("offline"));
      else resolve(filings);
    }, 1200),
  );

export default function Example() {
  const [found, setFound] = useState<typeof filings | null>(null);
  const [failed, setFailed] = useState(false);
  const latest = useRef(0);
  const search = useCallback(() => {
    const id = ++latest.current;
    setFailed(false);
    request().then(
      (next) => {
        if (id === latest.current) setFound(next);
      },
      () => {
        if (id === latest.current) setFailed(true);
      },
    );
  }, []);
  useEffect(search, [search]);
  const searching = found === null && !failed;
  // A quick answer goes straight to the results, with no ring at all.
  const slow = useSlowWait(searching);
  return (
    <section aria-labelledby="filings-search" className="flex w-full max-w-md flex-col gap-3">
      <h3 id="filings-search" className="text-base font-semibold">
        Filings this week
      </h3>
      {/* As tall as the three results, so the ring and the results take the same room. */}
      <div className="grid min-h-44 items-start">
        {slow ? (
          // Nothing else here says that the search is running, so the ring announces its label.
          <Spinner announce label="Searching filings" className="size-6 self-center justify-self-center" />
        ) : failed ? (
          <Empty reason="error">
            <EmptyHeader>
              <EmptyTitle>The search did not finish</EmptyTitle>
              <EmptyDescription>Check your connection and try again.</EmptyDescription>
            </EmptyHeader>
            <EmptyContent>
              <EmptyAction onClick={search}>Try again</EmptyAction>
            </EmptyContent>
          </Empty>
        ) : (
          <ItemGroup render={<ul aria-label="Filings this week" />} className="-mx-2.5">
            {found?.map((filing) => (
              <Item key={filing.filer} render={<li />}>
                <ItemContent>
                  <ItemTitle>{filing.filer}</ItemTitle>
                  <ItemDescription>{filing.form}</ItemDescription>
                </ItemContent>
              </Item>
            ))}
          </ItemGroup>
        )}
      </div>
    </section>
  );
}
```

Example: A card searching, with one status line in the results' place until they arrive (`docs/system/examples/spinner/in-card.tsx`)

```tsx
import { useCallback, useEffect, useRef, useState } from "react";

import { Card, CardContent, CardHeader, CardTitle } from "@/components/cobalt/ui/card";
import { Empty, EmptyAction, EmptyContent, EmptyDescription, EmptyHeader, EmptyTitle } from "@/components/cobalt/ui/empty";
import { Spinner } from "@/components/cobalt/ui/spinner";
import { useSlowWait } from "@/lib/cobalt/use-slow-wait";

const events = ["Autumn reading night, Thursday", "Harbour walk, Saturday", "Cellar tasting, Sunday"];

// Stands in for your API call. It answers after 1200ms, and fails while the browser is offline or the
// address has ?network=offline.
const request = () =>
  new Promise<string[]>((resolve, reject) =>
    setTimeout(() => {
      const offline = !navigator.onLine || new URLSearchParams(location.search).get("network") === "offline";
      if (offline) reject(new Error("offline"));
      else resolve(events);
    }, 1200),
  );

export default function Example() {
  const [found, setFound] = useState<string[] | null>(null);
  const [failed, setFailed] = useState(false);
  const latest = useRef(0);
  const search = useCallback(() => {
    const id = ++latest.current;
    setFailed(false);
    request().then(
      (next) => {
        if (id === latest.current) setFound(next);
      },
      () => {
        if (id === latest.current) setFailed(true);
      },
    );
  }, []);
  useEffect(search, [search]);
  const searching = found === null && !failed;
  // The status line waits 250ms, so a quick answer goes straight to the results.
  const slow = useSlowWait(searching);
  return (
    <Card className="w-full max-w-sm">
      <CardHeader>
        <CardTitle>Events this week</CardTitle>
      </CardHeader>
      {/* As tall as the three events, so the status line and the events take the same room. */}
      <CardContent className="min-h-17">
        {/* Mounted before the search starts, so a screen reader hears the words when they arrive. */}
        <p role="status" className="sr-only">
          {slow ? "Searching events" : ""}
        </p>
        {slow ? (
          <p className="flex items-center gap-2 text-sm text-muted-foreground">
            <Spinner aria-hidden="true" />
            Searching events
          </p>
        ) : failed ? (
          <Empty reason="error" align="start">
            <EmptyHeader>
              <EmptyTitle>The search did not finish</EmptyTitle>
              <EmptyDescription>Check your connection and try again.</EmptyDescription>
            </EmptyHeader>
            <EmptyContent>
              <EmptyAction onClick={search}>Try again</EmptyAction>
            </EmptyContent>
          </Empty>
        ) : (
          <ul className="flex flex-col gap-1 text-sm">
            {found?.map((event) => (
              <li key={event}>{event}</li>
            ))}
          </ul>
        )}
      </CardContent>
    </Card>
  );
}
```

## Variants
Not applicable: Spinner has no variant axis. The ring is 16px by default and takes `className="size-6"` for a 24px slot (D27); `label` names it and `announce` makes it a live region; neither is a look.

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| spinning | render under `(prefers-reduced-motion: no-preference)` | wait | the 2px ring turning; the `sr-only` label read in place, or announced once with `announce` | test |
| still | render under `(prefers-reduced-motion: reduce)` | wait | the same ring, open at one side and not turning; the same label | by hand |

### State precedence
- Spinning and still: still wins. The media query drops the animation and the ring holds its frame.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| mount, while the answer loads | announce | `animate-spin`, Tailwind's 1s linear turn, the system's one spinner loop; a loop is where linear timing is allowed | transform, one rotation | `motion-reduce:animate-none`: the ring holds still and the label stays; measured 0 animations under `reduce` and 1 under `no-preference`, .design-system/evidence/spinner/motion-1280.json |

The browser throttles the loop in a hidden tab; nothing in the component pauses it when it is scrolled away, and a spinner seldom outlives one screen. NEEDS REVIEW.

## Props

<!-- Props generated from registry/cobalt/ui/spinner.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `announce?` | `boolean` | `false` | Announce the label once, politely, when nothing else on the page says that it is waiting. |
| `label?` | `string` | `"Loading"` | What a screen reader says for the spinner. |

Also accepts every native `<span>` attribute.

`label` is what a screen reader says, "Loading" by default. `announce` (false) renders the slot as an empty `role="status"` and puts the label in it 100ms after mount, so the region exists before its words change. Every `span` attribute passes to the slot: `className` for the size and placement, `aria-hidden` when a host announces the wait itself. `ref` reaches the `span`.

## Usage

### When to use
- A wait longer than the slow-wait delay whose answer has no known shape, such as a search still running.
- A control waiting on its own action, through StateButton, which places the ring in the control's box.

### When not to use
- A button is waiting on its own press. Use StateButton instead, which keeps the box and the name.
- The shape of the answer is known. Use Skeleton instead.
- The amount done is known. Use Progress instead.
- The answer is known to be none. Use Empty instead.

### Rules
- `rule/spinner-inside-the-box`: When a spinner reports a control's wait, render it through `StateButton`, which puts the ring inside the control's box and keeps the idle label in the tree, instead of appending it beside the label or swapping the label for it, because a ring added beside a label grows the box and a swapped label renames the control mid-action. Evidence: person D2; single use `registry/cobalt/ui/state-button.tsx:160-162`, the ring takes the label's place inside the same box while the label stays. Check: lint trap/loading-label-swap (scripts/check-system.mjs); test on StateButton (`button.test.tsx`).
  - Don't: `<Button disabled={saving}>{saving && <Spinner />} Save</Button>`
  - Do: `<StateButton status={saving ? "pending" : "idle"} pendingLabel="Saving">Save</StateButton>`
- `rule/spinner-after-the-slow-wait`: When the wait may end within 250ms, gate the spinner with `useSlowWait(waiting)` instead of showing it on the first frame, because a ring that shows for 50ms reads as a flicker, and once shown it holds for 400ms. Evidence: person D25; single use `registry/cobalt/ui/data-table.tsx:1582`, the table admits to a wait only after the slow-wait delay. Check: review of every `Spinner` behind a fetch.
  - Don't: `{loading && <Spinner />}`
  - Do: `const slow = useSlowWait(loading); return slow ? <Spinner label="Loading results" /> : null;`
- `rule/spinner-one-announcement`: When the host already announces the wait, such as `StateButton`'s status line, a status line beside the ring or a table's `aria-busy`, leave the spinner without `announce`, and pass `announce` only when the ring is the one sign of the wait, instead of making every spinner a live region, because two live regions read the same wait twice and a region mounted with its words is often skipped. Evidence: principle wcag: 4.1.3 Status messages, one announcement per change; single use `registry/cobalt/ui/state-button.tsx:217`, the ring sits in an `aria-hidden` cell and the button's own status line speaks; measured on the spinner page, 0 of 3 spinners are live regions after the change, 3 of 3 were `role="status"` before (Playwright, 2026-10-17). Check: test `spinner.test.tsx` "is text read in place, not a live region".
  - Don't: `<p role="status">Saving <Spinner announce /></p>`
  - Do: `<p role="status">Saving <Spinner aria-hidden="true" /></p>`
- `rule/spinner-size-by-class`: When a spinner needs a bigger ring, pass `className="size-6"` instead of a `size` prop or a width on the glyph, because the glyph fills its slot and one class rule sizes every glyph in the system. Evidence: person D27. Check: lint `Spinner` has no `size` prop, so `pnpm typecheck` fails it.
  - Don't: `<Spinner size={24} />`
  - Do: `<Spinner className="size-6" />`

### Content
- Follows `rule/writing-status-verb-ing`.
- `rule/spinner-label-names-the-wait`: When the spinner stands alone in a region, set `label` to the -ing verb with its object, such as "Loading results", at most 20 characters, instead of the default "Loading" where more than 1 region can wait, because a screen reader hears "Loading" with nothing to say which region. Evidence: principle wcag: 4.1.3 Status messages, the message names the change; principle heuristic: visibility of system status. Check: lint copy/length on the status slot (scripts/copy-check.mjs).
  - Don't: `<Spinner label="Please wait while we fetch your results" />`
  - Do: `<Spinner label="Loading results" />`

### Anti-slop
- `rule/spinner-no-dots`: When a loader under 20px is wanted, use `Spinner`, the 2px ring, instead of three bouncing dots or a hand-drawn svg, because dots blur into a smudge at 16px and the ring is the one loop the system has. Evidence: measured the ring at 16px with a 2 stroke on a 24 viewBox, .design-system/evidence/spinner/motion-1280.json; principle heuristic: recognition rather than recall, one loader shape everywhere. Check: review.
  - Don't: `<span className="flex gap-1"><i className="size-1 animate-bounce rounded-full bg-foreground" /><i className="size-1 animate-bounce rounded-full bg-foreground" /></span>`
  - Do: `<Spinner />`

### Limits
Not applicable: the ring is one glyph at the size of its slot, 16px or 24px as drawn, and no count of spinners per screen has been grown to a break; `rule/spinner-one-announcement` covers more than one in a region.

## Accessibility
Rests on a `span` with an `sr-only` label, and with `announce` on a `role="status"` that is mounted empty and filled 100ms later. Contrast, as drawn (.design-system/evidence/spinner/motion-1280.json): the ring in `currentColor` on the card 19.8:1 light and 17.18:1 dark, over the 3:1 of WCAG 1.4.11 Non-text contrast.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the spinner | skips it, it is not focusable | the next control | test |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| root | none, a `span` | the `sr-only` label | none | the label, read in place; nothing on mount | test |
| root, with `announce` | status | the `sr-only` label, filled 100ms after mount | `aria-live="polite"` implied by the role | the label, once, after mount; nothing on unmount | test |
| glyph | none, `aria-hidden="true"` | nothing | none | nothing | test |

## Tokens
| Part | State | Token |
|---|---|---|
| glyph | all | `--foreground` through `currentColor`; the slot is `size-4` |

## Related
- StateButton: a button that reports its own action in place.
- Skeleton: a wait whose shape is known.
- Progress: a wait whose amount is known.
- Empty: the answer, when it is none.
- Toaster: reports what happened away from the user's attention.
- Meter: how much of a fixed allowance is used, such as seats or requests.
