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

## Description
Shows a key, or keys pressed together or in turn, beside the action they run, so a person can learn the shortcut.

`import { Kbd, KbdGroup } from "@/components/cobalt/ui/kbd"`, source `registry/cobalt/ui/kbd.tsx`, status `ready`.
Foundation: `hand-rolled`. Traps checked: `trap/illustration-announced`, `trap/color-only-status`, `trap/text-measure`.

`trap/illustration-announced` is answered by the spoken name for each key symbol, with the symbol hidden (ARIA). `trap/color-only-status` n/a: a key cap carries no status. `trap/text-measure` by the 5-key limit on a combination (Limits).

### Foundation
Not applicable: hand-rolled.

| Given by the foundation | Added by the team |
|---|---|
| Not applicable: hand-rolled, the HTML `kbd` | A 20px grey cap with a 4px corner, a small mark by the radius scale (D38), in the mono font at `text-xs`; a known symbol such as ⌘ read by its name, "Command", shown in the system tooltip on hover, and drawn in the sans face, since the mono face draws ⌘ and ⇧ at about two thirds of a capital; `label` for any other symbol; `KbdGroup` as a `kbd` of `kbd`s, the way HTML writes a combination, and `kind="sequence"` with the word "then" between keys |

## Examples
A key and a combination beside what they do; press either one to see it answer.

Real uses, 0 call sites (`rg -n "<Kbd\b" app registry --glob '!registry/cobalt/ui/*'`): the part is new. Atlas draws key hints by hand in its palette, its graph dock and its home page (`docs/research/2026-10-17-coverage.md`, section 3).

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/kbd/default.tsx` | default | A key and a combination beside what they do, with the reader's own modifier; press either one to see it answer |
| Not applicable: default.tsx shows keys pressed together | kind=combo | Keys pressed together, one cap per key with nothing between them |
| `docs/system/examples/kbd/kind-sequence.tsx` | kind=sequence | Keys pressed one after another, with then between them; press G, then W |
| `docs/system/examples/kbd/in-field.tsx` | composition:Field | The key that reaches a search field, in its description; press / to try it |

Example: A key and a combination beside what they do, with the reader's own modifier; press either one to see it answer (`docs/system/examples/kbd/default.tsx`)

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

import { Kbd, KbdGroup } from "@/components/cobalt/ui/kbd";

export default function Example() {
  const [mac, setMac] = useState(false);
  const [heard, setHeard] = useState("Nothing pressed yet.");
  useEffect(() => {
    // Command on a Mac, Control elsewhere; the hint shows only the reader's own.
    setMac(/Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent));
    const onKey = (event: KeyboardEvent) => {
      if (event.key === "Escape") setHeard("The panel would close now.");
      if (event.key.toLowerCase() === "s" && event.shiftKey && (event.metaKey || event.ctrlKey)) {
        event.preventDefault();
        setHeard("The search would be saved now.");
      }
    };
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, []);
  return (
    <div className="flex flex-col gap-3 text-sm">
      <p className="flex items-center gap-2">
        Save the search
        <KbdGroup>
          <Kbd>{mac ? "⌘" : "Ctrl"}</Kbd>
          <Kbd>⇧</Kbd>
          <Kbd>S</Kbd>
        </KbdGroup>
      </p>
      <p className="flex items-center gap-2">
        Close the panel <Kbd>Esc</Kbd>
      </p>
      <p role="status" className="text-xs text-muted-foreground">
        {heard}
      </p>
    </div>
  );
}
```

Example: The key that reaches a search field, in its description; press / to try it (`docs/system/examples/kbd/in-field.tsx`)

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

import { Field, FieldDescription, FieldLabel } from "@/components/cobalt/ui/field";
import { Input } from "@/components/cobalt/ui/input";
import { Kbd } from "@/components/cobalt/ui/kbd";

export default function Example() {
  const input = useRef<HTMLInputElement>(null);
  useEffect(() => {
    const onKey = (event: KeyboardEvent) => {
      const target = event.target as HTMLElement | null;
      const typing = target?.closest("input, textarea, select, [contenteditable='true']");
      if (event.key === "/" && !typing && !event.metaKey && !event.ctrlKey) {
        event.preventDefault();
        input.current?.focus();
      }
    };
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, []);
  return (
    <Field className="max-w-sm">
      <FieldLabel>Find a fund</FieldLabel>
      <Input ref={input} type="search" placeholder="Name or CIK number" />
      <FieldDescription className="flex items-center gap-1.5">
        Press <Kbd>/</Kbd> from anywhere on the page to jump here.
      </FieldDescription>
    </Field>
  );
}
```

## Variants

### kind
- `combo`: keys pressed together, such as ⌘ ⇧ S. The default. Used at 0 call sites.
- `sequence`: keys pressed in turn, such as G then W. Used at 0 call sites.

Example: Keys pressed one after another, with then between them; press G, then W (`docs/system/examples/kbd/kind-sequence.tsx`)

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

import { Kbd, KbdGroup } from "@/components/cobalt/ui/kbd";

export default function Example() {
  const [went, setWent] = useState(false);
  useEffect(() => {
    let last = "";
    let at = 0;
    const onKey = (event: KeyboardEvent) => {
      const target = event.target as HTMLElement | null;
      if (target?.closest("input, textarea, select, [contenteditable='true']")) return;
      const key = event.key.toLowerCase();
      if (key === "w" && last === "g" && Date.now() - at < 1000) setWent(true);
      last = key;
      at = Date.now();
    };
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, []);
  return (
    <div className="flex flex-col gap-2 text-sm">
      <p className="flex items-center gap-2">
        Go to the watchlist
        <KbdGroup kind="sequence">
          <Kbd>G</Kbd>
          <Kbd>W</Kbd>
        </KbdGroup>
      </p>
      <p role="status" className="text-xs text-muted-foreground">
        {went ? "The watchlist would open now." : "Press G, then W within a second."}
      </p>
    </div>
  );
}
```

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| text key | a word or a letter, such as "Esc" or "K" | read it | the key's own label | test |
| symbol key | a symbol the set knows, such as ⌘ | read it, or hover for its name | the symbol in the sans face, and its name in the system tooltip and to screen readers | test |

### State precedence
- Text key and symbol key: Not applicable: a cap holds one face.

### Motion
Not applicable: no motion.

## Props

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

| Prop | Type | Default | Purpose |
|---|---|---|---|
| `label?` | `string` |  | The key's spoken name, when its face is a symbol the set does not know. |

Also accepts every native HTML attribute.

### KbdGroup

| Prop | Type | Default | Purpose |
|---|---|---|---|
| `kind?` | `"combo" \| "sequence"` | `"combo"` | `combo` keys pressed together; `sequence` keys pressed in turn. |
| `then?` | `string` | `"then"` | The word between the keys of a sequence. |

Also accepts every native HTML attribute.

`label` on `Kbd` names a symbol the set does not know. `kind` on `KbdGroup` is `combo` or `sequence`, and `then` replaces the word between the keys of a sequence. Every native `kbd` attribute passes through.

## Usage

### When to use
- An action has a keyboard shortcut and its tooltip, or the field it reaches, can show it.
- A help panel lists the shortcuts of a tool.

### When not to use
- The key is code to copy, such as a command. Use CopyButton instead, beside the code.
- The hint names a button on screen. Use Button instead, with its text.

### Rules
- `rule/kbd-only-real-shortcuts`: When showing a `Kbd`, wire the shortcut it names on the same page, such as a `keydown` listener that focuses the search field on "/", instead of a hint for a shortcut that does nothing, because a hint that does nothing teaches the person that hints lie. Evidence: principle heuristic: consistency and standards, a hint is a promise; single use docs/system/examples/kbd/in-field.tsx:14-16. Check: review of every `Kbd` against the page's key handlers.
  - Don't: `<Kbd>/</Kbd>` with no handler for "/"
  - Do: `window.addEventListener("keydown", (e) => e.key === "/" && input.current?.focus())` beside `<Kbd>/</Kbd>`
- `rule/kbd-group-not-plus`: When keys are pressed together, wrap one `Kbd` per key in a `KbdGroup` instead of one cap holding "⌘+K", because a cap per key is how the keyboard looks and how HTML marks up a chord. Evidence: principle platform: the HTML `kbd` nested inside `kbd` marks a key combination; principle heuristic: match between system and the real world. Check: test `kbd.test.tsx` counts 3 `kbd` elements in a 2-key group.
  - Don't: `<Kbd>⌘+K</Kbd>`
  - Do: `<KbdGroup><Kbd>⌘</Kbd><Kbd>K</Kbd></KbdGroup>`
- `rule/kbd-platform-keys`: When a shortcut differs by platform, show the reader's own modifier, ⌘ on a Mac and "Ctrl" elsewhere, instead of both, because a hint with two keyboards in it is read twice to find the one that applies. Evidence: principle platform: macOS uses Command where Windows and Linux use Control; principle heuristic: recognition rather than recall. Check: review.
  - Don't: `<KbdGroup><Kbd>⌘</Kbd><Kbd>Ctrl</Kbd><Kbd>K</Kbd></KbdGroup>`
  - Do: `<KbdGroup><Kbd>{mac ? "⌘" : "Ctrl"}</Kbd><Kbd>K</Kbd></KbdGroup>`

### Content
- `rule/kbd-key-names`: When a key has a name, write it as the key's own label in title case, such as "Esc", "Enter" or "Tab", instead of upper case, because "ESC" in a mono cap reads as a code. Evidence: principle platform: keyboards print "esc" and "enter" in lower or title case; person D11. Check: lint rule/ban-uppercase (scripts/check-system.mjs); review.
  - Don't: `<Kbd>ESC</Kbd>`
  - Do: `<Kbd>Esc</Kbd>`

### Anti-slop
- `rule/kbd-one-hint-per-action`: When an action has a shortcut, show the `Kbd` in 1 place, the tooltip of its button, the description of the field it reaches or the tool's list of shortcuts, and never in a menu row, which shows no shortcut column, instead of beside every mention, because a cap on every line turns a menu into a keyboard diagram. Evidence: principle heuristic: aesthetic and minimalist design; app 2/2 source apps show a key hint once per action, docs/research/2026-10-17-coverage.md section 3. Check: review.
  - Don't: `<Button>Search <Kbd>⌘</Kbd><Kbd>K</Kbd></Button>` and the same caps in the tooltip
  - Do: `<KbdGroup><Kbd>⌘</Kbd><Kbd>K</Kbd></KbdGroup>` in the search field's description only

### Limits
- `rule/kbd-combo-length`: When a combination needs more than 3 keys, shorten the shortcut or leave the hint out instead, because 5 caps take 116px and wrap beside a 160px label in a 288px column. Evidence: measured a 5-key group at 116px and a 3-word sequence at 136px in a 256px list at 320, docs/system/stress/kbd.tsx. Check: probe docs/system/stress/kbd.tsx on the break-ui harness at 320.
  - Don't: `<KbdGroup><Kbd>⌘</Kbd><Kbd>⌥</Kbd><Kbd>⇧</Kbd><Kbd>⌃</Kbd><Kbd>E</Kbd></KbdGroup>`
  - Do: `<KbdGroup><Kbd>⌘</Kbd><Kbd>E</Kbd></KbdGroup>`

## Accessibility
Rests on the HTML `kbd`.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the cap | passes over it; a key hint is not focusable | the next focusable element | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| key, text | none, a `kbd` | its text | none | the key's label in the flow of the text | test |
| key, symbol | none, a `kbd` | a visually hidden name, the symbol `aria-hidden` | none | the name, such as "Command" | test |
| group, sequence | none, a `kbd` | its text | none | each key with "then" between | test |

## Tokens
| Part | State | Token |
|---|---|---|
| key | all | `--muted`, `--muted-foreground`, `--r-xs`, `--mono` |
| group | all | `--muted-foreground`, `--font` |

## Related
- Tooltip: where a key hint for an icon button goes.
- DropdownMenu: its rows show no shortcut; name the key in the trigger's tooltip instead.
- CopyButton: for code to copy, not a key to press.
- Keyboard shortcuts: the app's key bindings and the dialog that lists them.
