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

## Description
Tells the person something about the page they are on before they go on: a limit they reached, data that is behind, a load that failed and how to retry it.

`import { Alert, AlertContent, AlertTitle, AlertDescription, AlertAction } from "@/components/cobalt/ui/alert"`, source `registry/cobalt/ui/alert.tsx`, status `ready`.
Foundation: `hand-rolled`. Traps checked: `trap/color-only-status`, `trap/live-region-double`, `trap/toast-errors`, `trap/surface-double-edge`, `trap/surface-matches-parent`, `trap/illustration-announced`, `trap/focus-ring-shape`, `trap/control-height`.

`trap/color-only-status` is answered by the title's words (Anti-slop). `trap/live-region-double` by no live role of its own: the host gives `role="alert"` to an alert it inserts after an event, and nothing else (ARIA). `trap/toast-errors` by the rule that a failure in place stays in place (Rules). `trap/surface-double-edge` n/a: the alert draws a tint and no border and no shadow. `trap/surface-matches-parent` is answered by the tint, which differs from the card and the page in both themes (Tokens). `trap/illustration-announced` n/a: there is no icon. `trap/focus-ring-shape` is answered by the action's own 10px corner, 6px in from the alert's 16px corner, so the ring is never clipped (States). `trap/control-height` by the 36px default button in `AlertAction` (Rules).

### Foundation
Not applicable: hand-rolled.

| Given by the foundation | Added by the team |
|---|---|
| Not applicable: hand-rolled, a `div` with a role | Three variants as tints (`--primary-soft`, `--warning-soft` with `--warning` ink, `--destructive` at 10 percent, 20 in dark); the title's ink follows the variant; no icon and no side bar (the person's request, 2026-10-17); no live role by default, since a notice present from the first paint is read in place and a polite region mounted with its words is often skipped, and `role` from the host for one inserted later; the action 6px in from the 16px corner so a 10px button is concentric; the content keeps a 224px basis and the action drops under it on a narrower column, its label lined up with the text at 18px |

## Examples
A notice with a title and a line on what it means.

Real uses, 0 call sites (`rg -n "<Alert\b" app registry --glob '!registry/cobalt/ui/*'`): the part is new. The source apps hand-roll it: Atlas's failure line with Retry and its account band, and TSA's offline notice (`docs/research/2026-10-17-coverage.md`, section 3).

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/alert/default.tsx` | default | A notice with a title and a line on what it means |
| Not applicable: default.tsx is the info tint, the default | variant=info | Info, the accent tint, for something worth knowing that is not a problem |
| `docs/system/examples/alert/variant-warning.tsx` | variant=warning | Warning, the amber tint, for a limit you are close to |
| `docs/system/examples/alert/variant-destructive.tsx` | variant=destructive | Destructive, red ink on a red wash, for something that went wrong |
| `docs/system/examples/alert/state-with-action.tsx` | state:with-action | A load that failed, with Retry on the alert; when it works the holdings take its place |
| `docs/system/examples/alert/in-card.tsx` | composition:Card | A warning at the top of a card, above the facts it is about |

Example: A notice with a title and a line on what it means (`docs/system/examples/alert/default.tsx`)

```tsx
import { Alert, AlertContent, AlertDescription, AlertTitle } from "@/components/cobalt/ui/alert";

export default function Example() {
  return (
    <Alert className="max-w-lg">
      <AlertContent>
        <AlertTitle>Filings for October are still arriving</AlertTitle>
        <AlertDescription>Funds have until Nov 14 to file, so totals for the month will change.</AlertDescription>
      </AlertContent>
    </Alert>
  );
}
```

Example: A warning at the top of a card, above the facts it is about (`docs/system/examples/alert/in-card.tsx`)

```tsx
import { Alert, AlertContent, AlertDescription, AlertTitle } from "@/components/cobalt/ui/alert";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/cobalt/ui/card";

export default function Example() {
  return (
    <Card className="w-full max-w-md">
      <CardHeader>
        <CardTitle>Calder Global Equity</CardTitle>
        <CardDescription>Calder &amp; Rowe, last filed Jun 30.</CardDescription>
      </CardHeader>
      <CardContent className="flex flex-col gap-4">
        <Alert variant="warning">
          <AlertContent>
            <AlertTitle>Filing is 17 days late</AlertTitle>
            <AlertDescription>The figures below are from the June filing.</AlertDescription>
          </AlertContent>
        </Alert>
        <p className="text-sm text-muted-foreground">Net assets $1.2B across 412 holdings.</p>
      </CardContent>
    </Card>
  );
}
```

## Variants

### variant
- `info`: something worth knowing that is not a problem, such as data still arriving. The default. Used at 0 call sites.
- `warning`: a limit the person is close to, or data that may be stale. Used at 0 call sites.
- `destructive`: something went wrong, such as a load or a payment that failed. Used at 0 call sites.

Example: Warning, the amber tint, for a limit you are close to (`docs/system/examples/alert/variant-warning.tsx`)

```tsx
import { Alert, AlertContent, AlertDescription, AlertTitle } from "@/components/cobalt/ui/alert";

export default function Example() {
  return (
    <Alert variant="warning" className="max-w-lg">
      <AlertContent>
        <AlertTitle>48 of 50 saved searches used</AlertTitle>
        <AlertDescription>Delete a search you no longer run to save a new one.</AlertDescription>
      </AlertContent>
    </Alert>
  );
}
```

Example: Destructive, red ink on a red wash, for something that went wrong (`docs/system/examples/alert/variant-destructive.tsx`)

```tsx
import { Alert, AlertContent, AlertDescription, AlertTitle } from "@/components/cobalt/ui/alert";

export default function Example() {
  return (
    <Alert variant="destructive" className="max-w-lg">
      <AlertContent>
        <AlertTitle>The June filing could not be read</AlertTitle>
        <AlertDescription>Its holdings are left out of the totals until Harbour Capital files it again.</AlertDescription>
      </AlertContent>
    </Alert>
  );
}
```

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| static | rendered with the page | read it | the title's words | test |
| announced | inserted after an event, such as a failed load, with `role="alert"` from the host | read it; a screen reader hears it once, as it is inserted | the role the host passes; a polite notice goes inside a `role="status"` region the page keeps mounted | test |
| with-action | an `AlertAction` child | press the action, such as Retry | a 36px button 6px in from the top and end corners | screenshot |
| wrapped | the column is narrower than the 224px text basis plus the action | the same | the action under the text, its label at the text's 18px start | screenshot |
| focus-visible | Tab onto the action | press it with Enter or Space | the button's own outline, 6px inside the alert, unclipped | by hand |

Example: A load that failed, with Retry on the alert; when it works the holdings take its place (`docs/system/examples/alert/state-with-action.tsx`)

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

import { Alert, AlertAction, AlertContent, AlertDescription, AlertTitle } from "@/components/cobalt/ui/alert";
import { StateButton } from "@/components/cobalt/ui/state-button";
import { useAction } from "@/lib/cobalt/use-action";
import { cn } from "@/lib/cobalt/utils";

const holdings = ["Northgate Treasury 2031", "Calder Global Equity", "Harbour Short Duration"];

export default function Example() {
  const [loaded, setLoaded] = useState(false);
  const alert = useRef<HTMLDivElement>(null);
  const list = useRef<HTMLUListElement>(null);
  // Whether focus was on Retry when the holdings arrived, so it can follow them.
  const followFocus = useRef(false);
  const retry = useAction(
    () =>
      new Promise<void>((resolve) => {
        setTimeout(() => {
          followFocus.current = alert.current?.contains(document.activeElement) ?? false;
          setLoaded(true);
          resolve();
        }, 900);
      }),
  );
  useEffect(() => {
    if (loaded && followFocus.current) list.current?.focus();
  }, [loaded]);
  return (
    // The failure and the holdings share one cell, so the box is as tall as the taller of the two
    // and nothing under it moves when one takes the other's place. The alert goes at once and the
    // holdings fade in where it was, so the two are never drawn over each other.
    <div className="grid w-full max-w-lg *:col-start-1 *:row-start-1">
      {/* The failure arrives after a load, so it is an alert, announced as it is inserted. */}
      <Alert
        ref={alert}
        variant="destructive"
        role="alert"
        inert={loaded}
        className={cn("self-start", loaded && "opacity-0")}
      >
        <AlertContent>
          <AlertTitle>Holdings did not load</AlertTitle>
          <AlertDescription>The server did not answer. Your filters are kept.</AlertDescription>
        </AlertContent>
        <AlertAction>
          <StateButton
            variant="secondary"
            status={retry.status}
            pendingLabel="Retrying"
            onClick={() => void retry.run()}
          >
            Retry
          </StateButton>
        </AlertAction>
      </Alert>
      <ul
        ref={list}
        aria-label="Holdings"
        tabIndex={-1}
        inert={!loaded}
        className={cn(
          "flex flex-col gap-1 self-start text-sm transition-opacity duration-crossfade ease-sheet",
          !loaded && "opacity-0",
        )}
      >
        {holdings.map((h) => (
          <li key={h}>{h}</li>
        ))}
      </ul>
    </div>
  );
}
```

### State precedence
- Static and announced: Not applicable: whether a notice is announced depends on when it is inserted, not on a state the alert holds.
- With-action and wrapped: both show. The action keeps its 36px height and its corner; only its place moves.
- Focus-visible and wrapped: both show. The ring is drawn by the button wherever it sits.
- With-action and resolved: the result wins, in the alert's place. When Retry works, the holdings take the alert's cell and the box keeps its height, so nothing below moves, and focus follows from Retry to the result (`rule/alert-result-holds-its-place`).

### Motion
Not applicable: no motion. An alert appears and goes with the content around it; a host that animates its arrival uses `Grow` or `Swap`.

## Props

<!-- Props generated from registry/cobalt/ui/alert.tsx with TypeScript by scripts/props-table.mjs -->
### Alert

| Prop | Type | Default | Purpose |
|---|---|---|---|
| `variant?` | `"destructive" \| "info" \| "warning"` | `"info"` | `info` something worth knowing that is not a problem; `warning` a limit the person is close to; `destructive` something went wrong. |

Also accepts every native `<div>` attribute.

### AlertContent

No props of its own.

Also accepts every native `<div>` attribute.

### AlertTitle

No props of its own.

Also accepts every native `<div>` attribute.

### AlertDescription

No props of its own.

Also accepts every native `<div>` attribute.

### AlertAction

No props of its own.

Also accepts every native `<div>` attribute.

`variant` picks the tint. `role` is unset by default; pass `role="alert"` for a failure inserted after an event, as `docs/system/examples/alert/state-with-action.tsx:39` does. Every native `div` attribute passes through, and `style` is merged after the warning's hue. `className` is for placement.

## Usage

### When to use
- A load or an action failed in a place the person is looking at, and they can retry it there.
- The page's data is incomplete, late or limited, and the person should know before they read it.
- The person is close to a limit, such as saved searches or seats.

### When not to use
- The message is the result of an action whose place the person has left. Use Toaster instead.
- The list or page has nothing to show at all. Use Empty instead.
- The message is about one field. Use Field instead, with its error.
- The action cannot be undone and needs a yes. Use AlertDialog instead.

### Rules
- `rule/alert-failure-stays-in-place`: When a load fails in a region the person is reading, show an `Alert variant="destructive"` with Retry in that region instead of a `toast()`, because a toast leaves after 4 seconds and the empty region is left with no reason and no way back. Evidence: app 2/2 source apps show a failed load in place with a retry, Atlas's failure line and TSA's load problem notice, docs/research/2026-10-17-coverage.md section 3; person D39. Check: review of every `toast.error` call next to a list or a record.
  - Don't: `toast.error("Holdings did not load")`
  - Do: `<Alert variant="destructive"><AlertContent><AlertTitle>Holdings did not load</AlertTitle></AlertContent><AlertAction><StateButton status={retry.status} onClick={() => void retry.run()}>Retry</StateButton></AlertAction></Alert>`
- `rule/alert-one-action`: When an alert needs an answer, give it 1 button in `AlertAction`, at the default 36px height, `variant="secondary"`, and put a second path in the description as a link instead of a second button, because two buttons in a notice compete with the page's own primary action. Evidence: person D39; principle heuristic: aesthetic and minimalist design, one next step per notice. Check: review of every `AlertAction`.
  - Don't: `<AlertAction><Button>Upgrade</Button><Button variant="ghost">Dismiss</Button></AlertAction>`
  - Do: `<AlertAction><Button variant="secondary">Manage searches</Button></AlertAction>`
- `rule/alert-server-action-states`: When the alert's action reaches a server, such as Retry, render it as a `StateButton` driven by `useAction`, because the person needs to see the retry is running where they pressed it, and a plain `Button` gives no answer for the 900ms the request takes. Evidence: person D39; single use docs/system/examples/alert/state-with-action.tsx:48. Check: review of every `AlertAction` whose handler is async.
  - Don't: `<AlertAction><Button onClick={reload}>Retry</Button></AlertAction>`
  - Do: `<AlertAction><StateButton status={retry.status} pendingLabel="Retrying" onClick={() => void retry.run()}>Retry</StateButton></AlertAction>`
- `rule/alert-result-holds-its-place`: When the alert's action succeeds and its result replaces the alert, such as the holdings after Retry, stack the alert and the result in one grid cell (`grid *:col-start-1 *:row-start-1`), hide the alert with `inert` and fade the result in, and move focus to the result when it was on the action, instead of rendering one or the other, because the cell keeps the taller of the two, so the page under it does not jump, and a focused button that unmounts drops focus to the page. Evidence: measured the example's box dropping 58px in one frame at 1440 and focus on `body` before, and 0px with focus on the holdings after, `.design-system/evidence/alert/retry-holds-its-place.json`; single use docs/system/examples/alert/state-with-action.tsx:34. Check: review of every alert whose action removes it.
  - Don't: `if (loaded) return <ul>{holdings}</ul>; return <Alert>...</Alert>`
  - Do: `<div className="grid *:col-start-1 *:row-start-1"><Alert inert={loaded} className={cn(loaded && "opacity-0")}>...</Alert><ul inert={!loaded} tabIndex={-1}>{holdings}</ul></div>`
- `rule/alert-no-layout-override`: When an alert needs another fill or corner, add a variant instead of passing `bg-` or `rounded-` through `className`, and keep `className` for placement such as `max-w-lg`, because the 16px corner and the 6px action inset are what keep the button's corner concentric. Evidence: person D38; measured the action 6px from the top and end with a 10px corner inside the 16px alert at 390, docs/system/stress/alert.tsx. Check: lint rule/component-override (scripts/check-system.mjs).
  - Don't: `<Alert className="rounded-md bg-muted">`
  - Do: `<Alert variant="info" className="max-w-lg">`

### Content
- Follows `rule/writing-no-exclamation`.
- Follows `rule/writing-no-em-dash`.
- `rule/alert-title-says-what-happened`: When writing `AlertTitle`, state what happened in at most 8 words, such as "Holdings did not load", and put why and what to do in `AlertDescription`, because the title is what a screen reader hears first and what a person scanning the page reads. Evidence: principle heuristic: help users recognize, diagnose and recover from errors, the title names the problem; app 2/2 source apps lead a failure with what failed, Atlas's failure line and TSA's load problem notice, docs/research/2026-10-17-coverage.md section 3. Check: review of the alert slots in `docs/system/copy-inventory.tsv`.
  - Don't: `<AlertTitle>Error</AlertTitle>`
  - Do: `<AlertTitle>Payment declined</AlertTitle><AlertDescription>The card ending 4417 was declined by its bank.</AlertDescription>`

### Anti-slop
- `rule/alert-words-carry-status`: When an alert is a warning or a failure, say so in the words of `AlertTitle` instead of relying on the tint, an icon or a coloured bar, because the tint is read by about 1 in 12 men differently and by a screen reader not at all. Evidence: principle wcag: 1.4.1 Use of color, colour never the only means; person D13. Check: review; test `alert.test.tsx` renders no `svg` and no side border.
  - Don't: `<Alert variant="destructive" className="border-s-4"><AlertIcon /> Something went wrong</Alert>`
  - Do: `<Alert variant="destructive"><AlertContent><AlertTitle>Holdings did not load</AlertTitle></AlertContent></Alert>`
- `rule/alert-one-per-region`: When a region has several things to report, show 1 alert with the most serious one instead of a stack of alerts, because a stack of tinted boxes pushes the content they are about below the fold. Evidence: principle heuristic: aesthetic and minimalist design, every notice competes with the content; person D37. Check: review of every view that renders more than one `Alert`.
  - Don't: `{problems.map((p) => <Alert key={p}>{p}</Alert>)}`
  - Do: `<Alert variant="destructive"><AlertContent><AlertTitle>{problems.length} filings did not load</AlertTitle></AlertContent></Alert>`

### Limits
- `rule/alert-action-label-length`: When the action's label runs past 20 characters, shorten it to the verb and its object, such as "Update card", instead of a longer label, because past 20 characters the action no longer fits beside the 224px text at 390 and drops under it, which makes the alert 48px taller. Evidence: measured "Zahlungsmethode aktualisieren" drops under the text and the alert grows from 70px to 118px in a 310px column at 390, docs/system/stress/alert.tsx. Check: probe docs/system/stress/alert.tsx on the break-ui harness at 390.
  - Don't: `<Button variant="secondary">Update your payment method now</Button>`
  - Do: `<Button variant="secondary">Update card</Button>`

## Accessibility
Rests on a `div` with no live role: a notice there from the first paint is content, read in place. The host passes `role="alert"` for one it inserts after an event; a polite notice goes into a `role="status"` region that was mounted before it, the pattern in better-accessibility's screen-readers reference.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the alert | skips the text, which is not focusable, and reaches the action if there is one | the action's button | by hand |
| Enter | the action | runs it | stays on the button, which shows its own pending and result | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| root | none, a `div` | its text | none | nothing on insert; read in place with the page | test |
| root, with `role="alert"` from the host | alert | its text | none | the title and description once, as it is inserted | test |
| action | button | its label | `aria-disabled` while a `StateButton` is pending | its label, then the pending and result words | by hand |

## Tokens
| Part | State | Token |
|---|---|---|
| root | all | `--r-2xl`, `--foreground` |
| root | info | `--primary-soft` |
| root | warning | `--warning-soft` |
| root | destructive | `--destructive` at 10 percent, 20 in dark |
| title | warning | `--warning` |
| title | destructive | `--destructive` |
| description | all | `--muted-foreground` |

## Related
- Toaster: the result of something that happened where the person no longer is.
- Empty: a list or page with nothing in it, or that failed to load as a whole.
- Field: an error about one field.
- AlertDialog: a question before something that cannot be undone.
- Message: one message: yours or theirs, its bubble in its run, a quote, its time on demand and whether it was sent.
- Banner: a line at the foot of the window for news or a lost connection, until it is resolved.
- Inbox: the bell in a top bar that opens the person's notifications.
