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

## Description
Marks a thing with one word or a count that you read and do not press: new, draft, overdue, 12 unread.

`import { Badge, badgeVariants } from "@/components/cobalt/ui/badge"`, source `registry/cobalt/ui/badge.tsx`, status `ready`.
Foundation: `shadcn badge (base-luma, base)`. Traps checked: `trap/numbers-shift`, `trap/color-only-status`, `trap/empty-value`, `trap/link-as-button`, `trap/touch-hover-flash`, `trap/decor-pointer`, `trap/surface-double-edge`, `trap/surface-matches-parent`, `trap/illustration-announced`, `trap/list-dead-gap`.

The box is 22px tall in a 310px column at 390 and never wraps; it is never wider than its container (`max-w-full`), so a label longer than the column, which measured 56 characters (`.design-system/evidence/badge/grow-text-390.json`), ends in an ellipsis and shows the whole text in the system tooltip while it is cut. `trap/numbers-shift` is answered by the tabular figures in the box (Tokens and Content), `trap/color-only-status` by the word (Anti-slop), `trap/empty-value` by rendering nothing for nothing (Rules), `trap/link-as-button` by `render` (Rules), `trap/touch-hover-flash` by the foundation's `(hover: hover)` guard (States), `trap/decor-pointer` by `pointer-events: none` on the icon (Foundation). `trap/surface-double-edge` n/a: the box draws a transparent border and no shadow, so it has no edge. `trap/surface-matches-parent` n/a: a badge is a label, not a panel, and every variant has a fill. `trap/illustration-announced` n/a: an icon inside a badge is decoration beside text and carries `aria-hidden` from the icon set. `trap/list-dead-gap` n/a: a badge is not clickable, so the gap between two is not a hit area.

### Foundation
| Given by the foundation | Added by the team |
|---|---|
| Base UI `useRender` for `render`, so the badge can be the host's link; the `span`, the variant names (three kept), the icon slot (`[&>svg]:size-3`, `pointer-events: none`), `data-slot` and `data-variant` from the render state | One box shared with Tag (D32): `min-h-5.5`, `px-1.75`, `text-xs`, `font-medium`; `tabular-nums` so a count keeps its width; `--r-sm`, the tag's corner (D32); hover fills from `--primary-hover` and `--muted-hover` only when the badge is a link (`[a]:hover`), so a plain badge does not change under the pointer; `max-w-full`, and a text label wrapped in a span that is cut with an ellipsis, given `dir="auto"` so it is cut at its own end, and shown whole in the system tooltip while it is cut (`TruncatedText`), so a translated status never leaves its row; the trim beside an inline glyph on the logical side (`ps`, `pe`), so it follows the text direction; the destructive variant as red text on a light red background (D13); the shadcn `outline`, `ghost` and `link` variants dropped, since a ghost badge is plain text and a link badge is a link; `duration-fade` on colour changes |

## Examples
The accent, the grey fill and the red text.

Real uses, 0 call sites (`rg -n "<Badge\b" app docs/system/examples --glob '!docs/system/examples/badge/*'`): the showcase chrome marks nothing new yet, and the examples folder does not count.

Uses inside the registry, which other components build on: none. The registry's skill names the badge as the mark for a count or a short label (`registry/cobalt/SKILL.md:274`).

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/badge/default.tsx` | default | The accent, the grey fill and the red text |
| Not applicable: default.tsx shows the accent fill beside the other two | variant=default | The accent fill, for the one mark that needs attention |
| `docs/system/examples/badge/variant-secondary.tsx` | variant=secondary | The grey fill, for a fact that needs no attention |
| `docs/system/examples/badge/variant-destructive.tsx` | variant=destructive | Red text on a light red background, for something gone wrong |
| Not applicable: hover only exists on a badge rendered as a link, which no example shows | state:hover | The fill one step darker |
| Not applicable: focus-visible only exists on a badge rendered as a link, which no example shows | state:focus-visible | The host link's focus outline |
| `docs/system/examples/badge/in-card.tsx` | composition:Card | A count beside a card's title |
| `docs/system/examples/badge/in-item.tsx` | composition:Item | Counts beside the views in a sidebar, digits only, and no badge on a view with nothing new |

Example: The accent, the grey fill and the red text (`docs/system/examples/badge/default.tsx`)

```tsx
import { Badge } from "@/components/cobalt/ui/badge";

export default function Example() {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <Badge>New</Badge>
      <Badge variant="secondary">Draft</Badge>
      <Badge variant="destructive">Overdue</Badge>
    </div>
  );
}
```

Example: A count beside a card's title (`docs/system/examples/badge/in-card.tsx`)

```tsx
import { Badge } from "@/components/cobalt/ui/badge";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/cobalt/ui/card";

export default function Example() {
  return (
    <Card className="max-w-sm">
      <CardHeader>
        <CardTitle className="flex items-center gap-2">
          Reading list <Badge variant="secondary">3</Badge>
        </CardTitle>
        <CardDescription>Three items are unread.</CardDescription>
      </CardHeader>
      <CardContent>Updated this morning.</CardContent>
    </Card>
  );
}
```

Example: Counts beside the views in a sidebar, digits only, and no badge on a view with nothing new (`docs/system/examples/badge/in-item.tsx`)

```tsx
import { useState } from "react";

import { Badge } from "@/components/cobalt/ui/badge";
import { Item, ItemActions, ItemContent, ItemGroup, ItemTitle } from "@/components/cobalt/ui/item";

const views = [
  { id: "watchlist", name: "Watchlist", unread: 12 },
  { id: "new-filings", name: "New filings", unread: 3 },
  { id: "saved-searches", name: "Saved searches", unread: 0 },
];

export default function Example() {
  const [current, setCurrent] = useState("watchlist");
  return (
    <nav aria-label="Views" className="max-w-xs">
      <ItemGroup render={<ul />}>
        {views.map((view) => (
          <li key={view.id}>
            <Item
              render={
                <a
                  href={`#${view.id}`}
                  aria-current={view.id === current ? "true" : undefined}
                  onClick={() => setCurrent(view.id)}
                />
              }
            >
              <ItemContent>
                <ItemTitle>{view.name}</ItemTitle>
              </ItemContent>
              {view.unread > 0 && (
                <ItemActions>
                  <Badge variant="secondary">{view.unread}</Badge>
                </ItemActions>
              )}
            </Item>
          </li>
        ))}
      </ItemGroup>
    </nav>
  );
}
```

## Variants

### variant
- `default`: the one mark on a view that needs attention, the accent fill. Used at 0 call sites.
- `secondary`: a fact about the thing, such as "Draft", or a count, the grey fill. Used at 0 call sites.
- `destructive`: something gone wrong, such as "Overdue", red text on a light red background. Used at 0 call sites.

Example: The grey fill, for a fact that needs no attention (`docs/system/examples/badge/variant-secondary.tsx`)

```tsx
import { Badge } from "@/components/cobalt/ui/badge";

export default function Example() {
  return (
    <div className="flex items-center gap-2">
      <Badge variant="secondary">Draft</Badge>
      <Badge variant="secondary">Archived</Badge>
    </div>
  );
}
```

Example: Red text on a light red background, for something gone wrong (`docs/system/examples/badge/variant-destructive.tsx`)

```tsx
import { Badge } from "@/components/cobalt/ui/badge";

export default function Example() {
  return <Badge variant="destructive">Overdue</Badge>;
}
```

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| default | render | read it | the label or count in the box | test |
| cut | the label is wider than the badge's container | hover or long-press for the whole label in `title` | the ellipsis at the end of the label | test |
| hover | pointer over a badge rendered as a link, under `(hover: hover)` | follow the link | the pointer cursor and a fill one step darker | by hand |
| focus-visible | keyboard focus on a badge rendered as a link | follow the link with Enter | the host link's outline | by hand |

### State precedence
- Default and hover: Not applicable: a plain badge takes no hover, since every hover class is scoped to `[a]`, so only a badge rendered as a link has the pair.
- Hover and focus-visible: both show. The outline is drawn by the link and the fill by the badge, so neither hides the other.
- Cut and every other state: both show. The ellipsis is on the label and the fill and outline on the box.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| pointer over and out, on a badge rendered as a link | announce | `--t-fade` on `--curve` | background-color, color | the same colour change, with no movement to drop |

## Props

<!-- Props generated from registry/cobalt/ui/badge.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `variant?` | `"default" \| "secondary" \| "destructive"` | `"default"` | `default` the one mark on a view that needs attention; `secondary` a fact about the thing; `destructive` something gone wrong. |

Also accepts every native `<span>` attribute; from Base UI: `render`.

Every native `span` attribute passes through: `title`, `aria-*`, `id`. A string label that is cut shows itself whole in the system tooltip; `title` is not set. `render` swaps the element, for a badge that is a link. `className` is merged last and is for placement. `badgeVariants` is exported so a host can paint another element, such as a table cell, in the same box.

## Usage

### When to use
- A thing has a state you read at a glance, and that state is the exception in its list: new, draft, overdue.
- A heading or a row carries a count, such as the unread items in a list.

### When not to use
- The label is a category or a topic that should be the same colour everywhere it appears. Use Tag instead, which takes its hue from the key.
- The label is a filter that is on or off. Use Pill instead.
- The label is an action. Use Button instead.
- The label marks someone's presence on a picture. Use Avatar with its badge part instead.
- Every row in a list would carry the same word, such as "Active". Use a Tag in a table column instead, or leave the usual state unmarked and badge only the rows that differ.

### Rules
- `rule/badge-renders-the-link`: When a badge navigates, render it through `render` with the host's link element, instead of wrapping a `Badge` in a link or giving it `onClick`, because a badge is a `span`, and a span that acts has no role, no key handling and no tab stop. Evidence: principle platform: one interactive element per action, a `span` with a handler is reachable by pointer only; principle wcag: 2.1.1 Keyboard, the link is the element the keyboard reaches. Check: lint trap/link-as-button (scripts/check-system.mjs); test `badge.test.tsx` renders a link badge and expects one anchor and no span.
  - Don't: `<a href="/details"><Badge>Details</Badge></a>`
  - Do: `<Badge variant="secondary" render={<a href="/details" />}>Details</Badge>`
- `rule/badge-nothing-for-nothing`: When a count is 0 or the label is empty, render no `Badge` at all instead of an empty box or a "0", because an empty box is a mark with no meaning and a zero count says there is nothing to see, which the absence of a badge already says. Evidence: principle heuristic: aesthetic and minimalist design, every mark competes with the one that matters; principle wcag: 1.3.1 Info and relationships, a screen reader announces an empty span as nothing, so the box and the announcement disagree. Check: review of every badge whose label comes from data.
  - Don't: `<Badge variant="secondary">{unread}</Badge>`
  - Do: `{unread > 0 && <Badge variant="secondary">{unread}</Badge>}`
- `rule/badge-one-accent-per-view`: When a view holds more than one badge, give `variant="default"` to at most 1 of them and `variant="secondary"` to the rest, because the accent fill is the colour of the one thing that needs attention (D13), and two of them leave nothing to attend to. Evidence: person D13; principle heuristic: visibility of system status, one mark says where to look. Check: review of every view in the showcase.
  - Don't: `<Badge>New</Badge> <Badge>Updated</Badge> <Badge>Popular</Badge>`
  - Do: `<Badge>New</Badge> <Badge variant="secondary">Updated</Badge> <Badge variant="secondary">Popular</Badge>`
- `rule/badge-exception-only`: When rows in a list share a status, badge only the rows whose status differs from the usual one, such as "Terminated" among active funds, instead of a badge on every row, because a mark on every row is no mark at all and pushes the title aside on a phone. Evidence: measured 14 of 16 screener rows badged "Active", the badge cut to "Ac" at 390, docs/research/2026-10-16-audit.md; principle heuristic: visibility of system status, one mark says where to look. Check: review of every list that renders a badge per row.
  - Don't: `{rows.map((r) => <Badge variant="secondary">{r.status}</Badge>)}`
  - Do: `{rows.map((r) => r.status === "Terminated" && <Badge variant="secondary">Terminated</Badge>)}`
- `rule/badge-row-keeps-the-name`: When a badge sits beside a name in a row, let the row wrap with the name at least 160px wide, `flex flex-wrap gap-x-3 gap-y-1` on the row and `min-w-40 flex-1` on the name, instead of `min-w-0 flex-1 truncate` on the name beside the badge, because a badge keeps its whole label, so a status of 27 characters such as "Registrierung zurückgezogen" takes 180px of a 240px row and cuts the fund's name to "Kons…", and the row then names nothing. Evidence: measured the name cut to "A." beside "Registration withdrawn 12 days ago" at 320px and whole once the row wraps, docs/research/2026-10-16-break-ui-display.md; principle heuristic: recognition rather than recall, the row is found by its name. Check: probe docs/system/stress/badge.tsx on the break-ui harness, worst case at 320px; review of every row that holds a badge.
  - Don't: `<li className="flex items-center gap-3"><span className="min-w-0 flex-1 truncate">{fund.name}</span><Badge variant="destructive">{fund.status}</Badge></li>`
  - Do: `<li className="flex flex-wrap items-center gap-x-3 gap-y-1"><span className="min-w-40 flex-1">{fund.name}</span><Badge variant="destructive">{fund.status}</Badge></li>`
- `rule/badge-no-layout-override`: When a badge needs another fill, height or corner, add a variant instead of passing them through `className`, and keep `className` for placement such as `ml-auto` or `shrink-0`, because a one-off class breaks the box the badge shares with Tag (D32) and skips the link hover the variant carries. Evidence: person D32; principle heuristic: consistency and standards, one component carries every state. Check: lint rule/component-override (scripts/check-system.mjs).
  - Don't: `<Badge className="h-7 rounded-full bg-good px-3">Verified</Badge>`
  - Do: `<Badge variant="secondary" className="ml-auto">Verified</Badge>`

### Content
- Follows `rule/writing-no-invented-numbers`.
- `rule/badge-label-one-word`: When a badge names a state, write 1 word in sentence case, such as "Draft" or "Overdue", and at most 2 for a compound such as "In review", instead of a sentence, because the box is a mark read beside a title and a sentence in it reads as a second title. Evidence: person D11; principle heuristic: recognition rather than recall, a one-word state is recognised, a sentence is read. Check: lint rule/ban-uppercase (scripts/check-system.mjs); review of the badge slot in `docs/system/copy-inventory.tsv`.
  - Don't: `<Badge variant="secondary">THIS ITEM IS STILL A DRAFT</Badge>`
  - Do: `<Badge variant="secondary">Draft</Badge>`
- `rule/badge-count-is-digits`: When a badge carries a count, pass the digits alone, such as "12", with the noun in the text beside it, and pass a count of 1000 or more through `compact()` from `@/lib/cobalt/format`, because the tabular figures line up only when the box holds digits, and a six-digit count outgrows the mark. Evidence: principle heuristic: match between system and the real world, "12" beside "Unread" reads as the list's own count; principle platform: tabular figures, `font-variant-numeric: tabular-nums`, keep digits from shifting as they change. Check: lint copy/template on the badge slot (scripts/copy-check.mjs); review.
  - Don't: `<Badge variant="secondary">{count} unread items</Badge>`
  - Do: `Unread <Badge variant="secondary">{compact(count)}</Badge>`

### Anti-slop
- `rule/badge-word-carries-status`: When a badge reports a status, put the status in the word, such as "Overdue" with `variant="destructive"`, instead of a coloured badge with a neutral label or a coloured dot, because colour 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: lint rule/palette-use and rule/named-color (scripts/check-system.mjs); review.
  - Don't: `<Badge className="bg-bad text-white">Invoice</Badge>`
  - Do: `<Badge variant="destructive">Overdue</Badge>`
- `rule/badge-no-red-fill`: When a badge reports something gone wrong, use `variant="destructive"`, which is red text on a light red background, instead of a red or green fill from the palette, because good and bad are colours for data and destructive is written in red, with no red fill (D13). Evidence: person D13; person D26. Check: lint rule/palette-use (scripts/check-system.mjs).
  - Don't: `<Badge className="bg-red-600 text-white">Failed</Badge>`
  - Do: `<Badge variant="destructive">Failed</Badge>`
- `rule/badge-no-icon`: When a badge has a label, leave the icon off, and keep the icon slot for a badge whose icon is the whole meaning, such as a lock before "Private", because an icon in a 22px box leaves 12px for it and the label becomes the longer thing you have to read twice. Evidence: principle heuristic: aesthetic and minimalist design, one glyph beside every label makes none findable; measured 22px box with a 12px icon slot, .design-system/evidence/badge/grow-text-390.json. Check: review.
  - Don't: `<Badge><CheckIcon /> Verified</Badge> <Badge><ClockIcon /> Pending</Badge>`
  - Do: `<Badge variant="secondary">Verified</Badge> <Badge variant="secondary">Pending</Badge>`

### Limits
- `rule/badge-label-length`: When a label would run past 55 characters at 390px, shorten it to the state's name or use plain text instead, because the badge never wraps, so at 56 characters it is cut with an ellipsis at a 310px column and the exception it marks is read whole only in the tooltip. Evidence: measured overflow of the column at 56 characters and of the viewport at 63 in a 310px column at 390, .design-system/evidence/badge/grow-text-390.json. The cut, with the whole label in the tooltip, measured at 320px, docs/research/2026-10-16-break-ui-display.md. Check: probe `.design-system/scripts/grow-display.mjs` on the showcase at 390.
  - Don't: `<Badge variant="secondary">Waiting for the finance team to approve the quarterly budget revision</Badge>`
  - Do: `<Badge variant="secondary">Awaiting approval</Badge>`

## Accessibility
Rests on a `span`, or on the host's link through `render`.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before a badge rendered as a link | focuses the link and shows the host's outline; a plain badge is skipped | the link | by hand |
| Enter | a badge rendered as a link | follows the link | the destination | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| root, plain | none, a `span` | its text, read in the flow of the text around it | none; a badge has no disabled, selected or pressed state | the label or count as plain text, in place | test |
| root, rendered as a link | link | its text | `aria-current` from the host when it points at the current page | the text and "link" on focus | by hand |

## Tokens
| Part | State | Token |
|---|---|---|
| root | all | `--r-sm`, `--t-fade`, `--curve` |
| default | rest | `--primary`, `--primary-foreground` |
| default | hover, as a link | `--primary-hover` |
| secondary | rest | `--secondary`, `--secondary-foreground` |
| secondary | hover, as a link | `--muted-hover` |
| destructive | rest | `--destructive` at 10 percent, `--destructive` |

## Related
- Tag: a label coloured by what it names, the same hue everywhere.
- Pill: a label that is on or off.
- AvatarBadge: a mark on an avatar's corner.
- Button: a label that acts.
- TextLink: a word that navigates.
- TextSwap: changes a short status line in place, such as an autosave line.
- Stat: one figure that is the point, with its change.
