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

## Description
Names a category or a topic in a colour that follows the name, so the same key wears the same tint on every page and every visit, and leads to its topic when given a link.

`import { Tag, TagRow, tagHueClassName, type TagProps, type TagRowProps } from "@/components/cobalt/ui/tag"`, source `registry/cobalt/ui/tag.tsx`, status `ready-with-gaps`.
Foundation: `hand-rolled`. Traps checked: `trap/color-only-status`, `trap/link-as-button`, `trap/touch-hover-flash`, `trap/empty-value`, `trap/list-dead-gap`, `trap/numbers-shift`, `trap/decor-pointer`, `trap/surface-double-edge`, `trap/surface-matches-parent`, `trap/illustration-announced`.

The box is 22px tall and the small size 20px in a 310px column at 390; the label is cut with an ellipsis at 56 characters (`.design-system/evidence/tag/grow-text-390.json`). Text on fill measures 7.57:1 in light and 9.03:1 in dark as rendered, and the scheme's lowest hue gives 6.66:1 in light (hue 200) and 8.95:1 in dark (hue 25) (`.design-system/evidence/tag/contrast.json`); the gap is that the page renders every hue as hue 0 today, see the report. `trap/color-only-status` is answered by the text (Anti-slop), `trap/link-as-button` by `href` through `RenderLink` (Rules), `trap/touch-hover-flash` by the hover fill scoped to `href` and guarded by `(hover: hover)` (States), `trap/empty-value` by the data table's "None" for a row with no tags (Rules), `trap/list-dead-gap` by `touch-target` on a linked tag, which grows the hit area to 44px on a coarse pointer so the gaps between tags belong to them (Accessibility). `trap/numbers-shift` n/a: a tag holds a key, never a count; a count goes in a Badge. `trap/decor-pointer` n/a: the tag has no decorative layer. `trap/surface-double-edge` and `trap/surface-matches-parent` n/a: the fill is a tint on the page with no border and no shadow, and a tag is a label, not a panel. `trap/illustration-announced` n/a: the tag holds text only.

### Foundation
| Given by the foundation | Added by the team |
|---|---|
| Not applicable: hand-rolled. The link shape is the system's own `LinkProps`, `RenderLink` and `defaultLink` from `registry/cobalt/lib/link.tsx` (D19) | The badge's box (D32) with the `--r-sm` corner (G-03) and a `sm` step under it; the hue scheme, `hueStyle` writing `--h` or `--c: 0` for grey, and the `--hue-bg`, `--hue-fg` and `--hue-bg-hover` tokens that flip for dark; `max-w-full` with the label in a `truncate` span (`TruncatedText`) and the whole label in the system tooltip while it is cut; `href` rendering one anchor through `renderLink`, with `touch-target` and the hover fill only then |

## Examples
A report's topics as links, the hues and the small size, a long tag cut short, and a table's tags column.

Real uses, 0 call sites (`rg -n "<Tag\b" app docs/system/examples --glob '!docs/system/examples/tag/*'`): the showcase chrome lists no topics 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 tags column of the data table, one tag per value keyed by its own text, `registry/cobalt/ui/data-table.tsx:488`.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/tag/default.tsx` | default | A report's topics, each a link to the topic's other reports, in the tint the topic has on every page |
| `docs/system/examples/tag/hue-number.tsx` | hue=number | The same key gets the same tint on every page |
| `docs/system/examples/tag/hue-null.tsx` | hue=null | Grey, for a key with no colour of its own |
| Not applicable: default.tsx and hue-number.tsx are the default size | size=default | The badge's box, 22px tall |
| `docs/system/examples/tag/size-sm.tsx` | size=sm | One step under the badge's box, for a dense row |
| Not applicable: default.tsx shows tags as links to their topic | href=string | Links to their topic, a step darker under the pointer |
| `docs/system/examples/tag/state-truncated.tsx` | state:truncated | Cut with an ellipsis; the full label is the title |
| `docs/system/examples/tag/state-overflow.tsx` | state:overflow | Tags past the row become a grey +N naming them |
| Not applicable: hover only exists on a linked tag, reached with the pointer on default.tsx | state:hover | The fill one step darker |
| Not applicable: focus-visible only exists on a linked tag, reached with the keyboard on default.tsx | state:focus-visible | The host link's outline |
| `docs/system/examples/tag/in-data-table.tsx` | composition:DataTable | The tags column keeps each cell to one line and counts the rest, with bug pinned to red and None for a row with no tags |
| Not applicable: the tiles show each hue and size | matrix:hue,size | Both sizes, keyed and grey |

Example: A report's topics, each a link to the topic's other reports, in the tint the topic has on every page (`docs/system/examples/tag/default.tsx`)

```tsx
import { Tag } from "@/components/cobalt/ui/tag";
import { hueFor } from "@/lib/cobalt/hues";

const topics = ["private credit", "europe", "fees", "advisers"];

export default function Example() {
  return (
    <div className="flex max-w-md flex-col gap-2">
      <p className="text-sm font-medium">Private credit launches in Europe, third quarter</p>
      <div className="flex flex-wrap gap-1.5">
        {topics.map((topic) => (
          <Tag key={topic} hue={hueFor(topic)} href={`#topic-${topic.replace(/ /g, "-")}`}>
            {topic}
          </Tag>
        ))}
      </div>
    </div>
  );
}
```

Example: The tags column keeps each cell to one line and counts the rest, with bug pinned to red and None for a row with no tags (`docs/system/examples/tag/in-data-table.tsx`)

```tsx
import { type Column, DataTable } from "@/components/cobalt/ui/data-table";
import { TagRow } from "@/components/cobalt/ui/tag";
import { counted } from "@/lib/cobalt/format";

type Issue = { id: number; title: string; tags: string[] | null };

const issues: Issue[] = [
  { id: 1, title: "Export fails on large files", tags: ["bug", "export"] },
  { id: 2, title: "Dark theme for the report view", tags: ["design", "theme"] },
  { id: 3, title: "Invoice totals round up", tags: ["billing", "bug", "invoices", "rounding", "export"] },
  { id: 4, title: "Rename the saved views menu", tags: null },
];

// "bug" means something, so it is pinned to red; every other key hashes to a hue of its own.
const hues = { bug: 25 };

const columns: Column<Issue>[] = [
  { key: "title", label: "Title", kind: "text" },
  // The kind sizes the column and writes the phone's text; the cell only adds the pinned hue.
  { key: "tags", label: "Tags", kind: "tags", width: 180, cell: (row) => <TagRow tags={row.tags ?? []} hues={hues} /> },
];

export default function Example() {
  return (
    <DataTable
      label="Issues"
      labels={{ rowCount: (count) => counted(count, { one: "issue", other: "issues" }) }}
      rows={issues}
      columns={columns}
      getRowId={(row) => row.id}
    />
  );
}
```

## Variants

### hue
- `number`: a hue on the OKLCH wheel from `hueFor(key)`, hashed from the key or pinned in a map, so a key is the same colour everywhere. Used at 1 call site, the data table's tags column.
- `null`: grey, zero chroma, the colour that means none, for "archived" or a key with no meaning. Used at 0 call sites; the default when `hue` is left out.

Example: The same key gets the same tint on every page (`docs/system/examples/tag/hue-number.tsx`)

```tsx
import { Tag } from "@/components/cobalt/ui/tag";
import { hueFor } from "@/lib/cobalt/hues";

const topics = ["design", "billing", "search", "mobile"];

export default function Example() {
  return (
    <div className="flex flex-wrap gap-1.5">
      {topics.map((topic) => (
        <Tag key={topic} hue={hueFor(topic)}>
          {topic}
        </Tag>
      ))}
    </div>
  );
}
```

Example: Grey, for a key with no colour of its own (`docs/system/examples/tag/hue-null.tsx`)

```tsx
import { Tag } from "@/components/cobalt/ui/tag";

export default function Example() {
  return (
    <div className="flex flex-wrap gap-1.5">
      <Tag hue={null}>archived</Tag>
      <Tag>untagged</Tag>
    </div>
  );
}
```

### size
- `default`: the badge's box, 22px tall with `text-xs`. Used at 1 call site, the data table's tags column.
- `sm`: 20px tall, one step under, for a row that is already dense. Used at 0 call sites.

Example: One step under the badge's box, for a dense row (`docs/system/examples/tag/size-sm.tsx`)

```tsx
import { Tag } from "@/components/cobalt/ui/tag";
import { hueFor } from "@/lib/cobalt/hues";

export default function Example() {
  return (
    <div className="flex items-center gap-1.5 text-sm">
      Quarterly report
      <Tag hue={hueFor("finance")} size="sm">
        finance
      </Tag>
    </div>
  );
}
```

### href
- `string`: the tag is one anchor to its topic, through `renderLink`, with the hover fill and a 44px touch target. Used at 0 call sites.

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| default | render | read it | the key as text in a tinted box | test |
| truncated | the label is wider than the container | hover for the whole label in the tooltip | the ellipsis at the end of the text, which is the start on a right-to-left label | screenshot |
| overflow | a `TagRow` holds more tags than its width | hover the count for the hidden keys in the tooltip; a screen reader hears them after the shown tags | a grey `+N` tag at the end, in tabular figures; when one tag and the count do not fit, that tag is cut. Fitted before the first paint: an overflowing row first shows as many tags as one reading of it says fit, each tag's width and the count's from the tags' own font against the row's content box, the count reckoned 2px narrow so the reading never shows fewer than fit; then, while the row as drawn still overflows, one fewer a pass, as it dropped one tag a pass before. Each pass shows fewer tags than the last, so it ends by the first tag (`registry/cobalt/ui/tag.tsx:183`); only a row grown wider than where tags last gave way starts again from every tag, so a row whose width follows its tags never swings between the two. One ResizeObserver serves every row, and the count's tooltip mounts on the first hover or focus. Measured the same tags shown as at 04e3058 at every width from 120 to 480px in 0.25px steps, with no side padding and with 8px, four sets of tags, 11,528 rows, React and Preact (`.design-system/evidence/perf/tag-sweep.txt`) | screenshot |
| hover | pointer over a linked tag, under `(hover: hover)` | follow the link | pointer only, the fill one step darker | by hand |
| focus-visible | keyboard focus on a linked tag | follow the link with Enter | the host link's outline | by hand |
| grey | `hue={null}` or no `hue` | read it | the box in grey, the text the same | test |

Example: Cut with an ellipsis; the full label is the title (`docs/system/examples/tag/state-truncated.tsx`)

```tsx
import { Tag } from "@/components/cobalt/ui/tag";
import { hueFor } from "@/lib/cobalt/hues";

export default function Example() {
  return (
    <div className="w-40">
      <Tag hue={hueFor("research")}>Research and development, long term</Tag>
    </div>
  );
}
```

Example: Tags past the row become a grey +N naming them (`docs/system/examples/tag/state-overflow.tsx`)

```tsx
import { TagRow } from "@/components/cobalt/ui/tag";

const topics = ["design", "billing", "search", "mobile", "accounts", "export", "theme"];

export default function Example() {
  return (
    <div className="w-60">
      <TagRow tags={topics} />
    </div>
  );
}
```

### State precedence
- Default and hover: Not applicable: a plain tag takes no hover, since the hover class is added only with `href`, so only a linked tag has the pair.
- Truncated and hover: both show. The darker fill and the ellipsis are on different parts, and the `title` is shown by the browser after its own delay.
- Hover and focus-visible: both show. The outline is drawn by the link and the fill by the tag.
- Overflow and truncated: both show only when one tag is left; before that, a tag is dropped into the count rather than cut.
- Grey and every other state: both show. Grey is a hue value, so a grey tag truncates, hovers and focuses the same.

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

## Props

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

| Prop | Type | Default | Purpose |
|---|---|---|---|
| `children` | `ReactNode` |  |  |
| `className?` | `string` |  |  |
| `href?` | `string` |  | Renders the tag as a link. |
| `hue?` | `number` |  | A hue on the OKLCH wheel from `hueFor`, or `null` or nothing for grey. |
| `renderLink?` | `RenderLink` | `defaultLink` | Swaps the plain anchor for a router link. |
| `size?` | `"default" \| "sm"` | `"default"` | `default` the badge's box; `sm` 20px tall. |

### TagRow

| Prop | Type | Default | Purpose |
|---|---|---|---|
| `tags` | `readonly string[]` |  | The keys, in order. |
| `className?` | `string` |  |  |
| `hues?` | `Record<string, number>` |  | Pins keys to chosen hues, or to `null` for grey, as `hueFor` takes them. |
| `moreLabel?` | `(count: number) => string` | `(n) => `${n} more`` | What a screen reader hears before the hidden keys. |
| `size?` | `"default" \| "sm"` | `"default"` | `default` the badge's box; `sm` 20px tall. |

`children` is the label; when it is a string it is also the `title`, so a cut label still has its whole name. `hue` takes a number from `hueFor` or `null`. `href` turns the tag into one anchor and `renderLink` swaps the plain anchor for a router link (D19). `size` is `default` or `sm`. `className` is merged last and is for placement, such as `max-w-40`. `tagHueClassName` is exported so another part, such as a swatch, can wear the same fill and ink from `--h`. `TagRow` takes `tags` (the keys, in order), `hues` (the map `hueFor` takes), `size`, `moreLabel` (a function of the count that a screen reader hears before the hidden keys, "9 more" by default) and `className`; it renders nothing for no tags, so the host shows "None".

## Usage

### When to use
- A thing belongs to a category or a topic that appears on more than one screen, and you find it by colour before reading it.
- A list filters or groups by the same keys, and the tag leads to the filtered list.

### When not to use
- The label is a state you read, such as "Draft", or a count. Use Badge instead, which has the same box without a hue.
- The label is a filter that is on or off. Use Pill instead.
- The label is an action with an effect other than navigation. Use Button instead.

### Rules
- `rule/tag-key-is-the-text`: When a tag is coloured by `hueFor(text)`, pass the same string to `hueFor` and as `children`, instead of hashing one form and showing another, because the hash of "Design" and "design" differ, so one topic wears two colours. Evidence: single use `registry/cobalt/ui/tag.tsx:217-221`, the tags column passes `tag` to both, and it generalises because every hashed key is case-sensitive; principle heuristic: consistency and standards, one key is one colour. Check: review of every `hueFor` call beside a `Tag`.
  - Don't: `<Tag hue={hueFor(topic.toLowerCase())}>{topic}</Tag>`
  - Do: `<Tag hue={hueFor(topic)}>{topic}</Tag>`
- `rule/tag-pin-what-means-something`: When a key carries a meaning, such as a status, pin its hue in the map passed to `hueFor(key, map)` and pin "none" keys to `null`, instead of letting every key hash, because a hashed hue is stable but arbitrary, and two statuses can hash to the same hue 1 time in 360. Evidence: principle heuristic: match between system and the real world, a blocked state in red and an open one in green follow the page's data colours; single use `registry/cobalt/ui/data-table.tsx:488`, the tags column hashes freely because topics carry no meaning, which generalises because only a key with a meaning needs a pinned hue. Check: review of the map beside every `hueFor` for status keys.
  - Don't: `<Tag hue={hueFor(status)}>{status}</Tag>`
  - Do: `<Tag hue={hueFor(status, { open: 150, blocked: 25, archived: null })}>{status}</Tag>`
- `rule/tag-href-not-onclick`: When a tag leads somewhere, pass `href` and, in a routed app, `renderLink`, instead of `onClick` on the tag or a `Tag` inside an anchor, because the tag renders one anchor from `href` and a `span` with a handler has no role, no tab stop and no key handling. Evidence: principle platform: a link is an `a` with `href`, which the keyboard, the middle click and the screen reader all know; person D19. Check: lint trap/link-as-button (scripts/check-system.mjs); test `tag.test.tsx` renders `href` and expects one anchor.
  - Don't: `<a href="/topics/design"><Tag hue={hueFor("design")}>design</Tag></a>`
  - Do: `<Tag hue={hueFor("design")} href="/topics/design" renderLink={routerLink}>design</Tag>`
- `rule/tag-none-is-text`: When a record has no tags, render the word "None" in `text-muted-foreground`, as the data table does, instead of an empty cell or a grey tag that says nothing, because an empty cell reads as a value still loading and a screen reader skips it. Evidence: single use `registry/cobalt/ui/data-table.tsx:467`, the table's `none` label for a blank value, which generalises because every list of tags can be empty; principle wcag: 1.3.1 Info and relationships, an empty value needs text. Check: lint trap/empty-value (scripts/check-system.mjs); review.
  - Don't: `{tags.length === 0 ? <Tag hue={null}> </Tag> : tags.map((t) => <Tag key={t} hue={hueFor(t)}>{t}</Tag>)}`
  - Do: `{tags.length === 0 ? <span className="text-muted-foreground">None</span> : tags.map((t) => <Tag key={t} hue={hueFor(t)}>{t}</Tag>)}`

### Content
- Follows `rule/writing-link-object`.
- `rule/tag-label-is-the-key`: When a tag is shown, write the key as the data holds it, such as "billing", with no prefix such as "Tag:" or "Topic:", and no trailing count, because the tint already says it is a tag and the hash follows the exact text. Evidence: single use `registry/cobalt/ui/data-table.tsx:488`, the tags column renders the raw value, which generalises because the hash follows the exact text; principle heuristic: aesthetic and minimalist design, the box is the label's frame. Check: lint copy/template on the tag slot (scripts/copy-check.mjs); review.
  - Don't: `<Tag hue={hueFor("billing")}>Topic: billing (3)</Tag>`
  - Do: `<Tag hue={hueFor("billing")}>billing</Tag>`

### Anti-slop
- `rule/tag-hue-not-palette`: When a tag needs a colour, pass `hue` from `hueFor` instead of palette classes such as `bg-blue-100 text-blue-800` in `className`, because the hue tokens mix the lightness and chroma once for light and dark, at 6.66:1 or better, and a palette class fixes one theme and one key by hand. Evidence: person D13; measured 6.66:1 lowest text on fill in light and 8.95:1 in dark across 8 hues, .design-system/evidence/tag/contrast.json. Check: lint rule/palette-use and rule/component-override (scripts/check-system.mjs).
  - Don't: `<Tag className="bg-blue-100 text-blue-800">design</Tag>`
  - Do: `<Tag hue={hueFor("design")}>design</Tag>`
- `rule/tag-text-carries-status`: When a tag names a status, the word carries it, such as "blocked", and the hue repeats it, instead of a hue on a neutral word or a coloured dot, because colour alone is lost to a screen reader and to about 1 in 12 men. Evidence: principle wcag: 1.4.1 Use of color, colour never the only means; person D13. Check: review of every pinned hue map.
  - Don't: `<Tag hue={25}>Invoice 4411</Tag>`
  - Do: `<Tag hue={hueFor("blocked", hues)}>blocked</Tag>`
- `rule/tag-not-a-count`: When a number needs a mark, use a Badge with tabular figures instead of a Tag, because a tag's hue is a key and a number is not one, and the badge's figures line up where the tag's text does not. Evidence: person D32; principle heuristic: consistency and standards, one box for counts and one for keys. Check: review.
  - Don't: `<Tag hue={hueFor("unread")}>12</Tag>`
  - Do: `<Badge variant="outline">12</Badge>`

### Limits
- `rule/tag-label-length`: When a label would run past 55 characters at 390px, shorten the key or give the tag `className="max-w-40"` so it cuts early, because the tag never wraps and at 56 characters it is cut with an ellipsis at a 310px column, leaving the whole label in the tooltip only. Evidence: measured truncation at 56 characters in a 310px column at 390, .design-system/evidence/tag/grow-text-390.json. Check: probe `.design-system/scripts/grow-display.mjs` on the showcase at 390.
  - Don't: `<Tag hue={hueFor(topic)}>Research and development projects for the long term, every region</Tag>`
  - Do: `<Tag hue={hueFor(topic)}>research</Tag>`
- `rule/tag-row-one-line`: When a list row or a cell holds a set of tags, render them with `TagRow`, which keeps them on one line and counts the rest in a grey "+N" that names them, instead of a wrapping row or one capped with `h-5.5 overflow-hidden`, because a wrapping row pushes every row below it, and a capped one hid 11 of a fund's 12 tags with nothing to say so. Evidence: measured 1 of 12 tags shown and 11 hidden without a count in a 180px tags cell, and "equity +11" with `TagRow`, docs/research/2026-10-16-break-ui-display.md; single use `registry/cobalt/ui/data-table.tsx:488`, the tags cell counts its hidden tags the same way. Check: probe docs/system/stress/tag.tsx on the break-ui harness, worst case; review of every row of tags.
  - Don't: `<div className="flex h-5.5 flex-wrap gap-1 overflow-hidden">{tags.map((t) => <Tag key={t} hue={hueFor(t)}>{t}</Tag>)}</div>`
  - Do: `<TagRow tags={tags} />`

## Accessibility
Rests on a `span`, or on one anchor from `renderLink` when `href` is set.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before a linked tag | focuses the anchor and shows the host's outline; a plain tag is skipped | the anchor | by hand |
| Enter | a linked tag | 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, in the flow of the text around it; the tooltip repeats it for the pointer while it is cut | none | the key as plain text, in place | test |
| row | none, a `span` | Not applicable: its tags are read in order | none | the shown keys, then "9 more" and the hidden keys from a visually hidden span; the `+N` itself is `aria-hidden` | test |
| root, linked | link | its text; `title` repeats it | `aria-current` from the host when it points at the current page | the key and "link" on focus | by hand |
| hit area, linked, coarse pointer | none | Not applicable: the `touch-target` pseudo-element is `pointer-events` only, no content | 44px from `--touch` | nothing | by hand |

## Tokens
| Part | State | Token |
|---|---|---|
| root | all | `--r-sm`, `--hue-bg`, `--hue-fg` |
| root, linked | hover | `--hue-bg-hover`, `--t-press`, `--curve` |
| hit area, linked | coarse pointer | `--touch` |
| row count | all | `--r-sm`, `--hue-bg` and `--hue-fg` at zero chroma, the grey tag |

## Related
- Badge: a state or a count in the same box, without a hue.
- Pill: a filter that is on or off.
- DataTable: renders a tags column with this component, counting the tags that do not fit.
- Avatar: a mark for someone, where a tag marks a thing.
- Flag: a country, by its flag beside its name.
