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

## Description
Copies a value a person needs somewhere else, such as a link, an id, a key or a command, and answers in place whether it worked.

`import { CopyButton, copyText } from "@/components/cobalt/ui/copy-button"`, source `registry/cobalt/ui/copy-button.tsx`, status `ready`.
Foundation: `Base UI Tooltip over the system Button`. Traps checked: `trap/button-icon-name`, `trap/control-height`, `trap/live-region-double`, `trap/toast-errors`, `trap/focus-ring-shape`, `trap/loading-label-swap`, `trap/reduced-motion-ignored`.

`trap/button-icon-name` is answered by `label` as the icon form's `aria-label` and tooltip (ARIA). `trap/control-height` by the 36px default size (Rules). `trap/live-region-double` by one status line per button that speaks the result, while the name stays fixed (ARIA). `trap/toast-errors` by the failure shown on the button instead of a toast (Rules). `trap/focus-ring-shape` by the Button's own outline (States). `trap/loading-label-swap` n/a: the clipboard answers at once, so there is no pending label; the text form's words never change. `trap/reduced-motion-ignored` by the base styles, which keep the glyph's fade and drop its blur (Motion).

### Foundation
| Given by the foundation | Added by the team |
|---|---|
| Base UI `Tooltip` through the system `Tooltip`; the system `Button`, `IconSwap` and its 250ms glyph swap | `copyText()`, which turns a missing clipboard and a refused write into `false` instead of an error; the copy glyph swaps to a check for `RESULT_HOLD_MS`, 1.5 seconds; a press keeps the icon form's tooltip open, and its words turn to "Copied" while the check shows, then back, and the tooltip eases to each wording's width on the settle spring, centred over the button, so "Copied" never sits in a box sized for a long name and the two wordings are never legible together (`TextSwap fit`, the leaving one fading over the centre) (`TextSwap`); a refused copy opens the tooltip with "Could not copy" for `ERROR_HOLD_MS`, 3 seconds, without a hover; `inset`, so a button inside a box's corner keeps the tooltip 6px clear of the box; one `role="status"` line that reads "Copied" or "Could not copy" once; an icon form named by `label` and a text form whose words never change; `copy` to swap the clipboard for another writer; moved from the docs site's own copy button, which now imports this one (`site/code.tsx`) |

## Examples
A booking number with its copy button; the glyph turns to a check once it is copied.

Real uses, 0 call sites (`rg -n "<CopyButton\b" app registry --glob '!registry/cobalt/ui/*'`): the docs site renders it through `site/code.tsx` for every code block, outside the counted folders. TSA copies in five places and Atlas in five more by hand (`docs/research/2026-10-17-coverage.md`, section 3).

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/copy-button/default.tsx` | default | A booking number and its copy button as one chip; the glyph turns to a check once it is copied |
| `docs/system/examples/copy-button/default.tsx` | variant=ghost | Ghost, the icon form, inside the chip that holds its value |
| `docs/system/examples/copy-button/variant-secondary.tsx` | variant=secondary | Secondary with a label, for a link to share. The label stays as it is and the glyph turns to a check |
| `docs/system/examples/copy-button/copied.tsx` | state:copied | Pressing either button turns its glyph to a check and its tooltip to Copied for 1.5 seconds, and reads Copied once; the other stays as it was |
| `docs/system/examples/copy-button/failed.tsx` | state:failed | When the clipboard refuses, the glyph stays and the tooltip opens with Could not copy for 3 seconds, so the value can be selected by hand |
| `docs/system/examples/copy-button/in-field.tsx` | composition:Field | A command in a read-only field, with its copy button inside the field's end |

Example: A booking number and its copy button as one chip; the glyph turns to a check once it is copied (`docs/system/examples/copy-button/default.tsx`)

```tsx
import { CopyButton } from "@/components/cobalt/ui/copy-button";

export default function Example() {
  return (
    <p className="flex flex-wrap items-center gap-2 text-sm">
      Booking
      {/* The chip is 36px with a 10px corner; the button sits 4px in, so its 6px corner is concentric. */}
      <span className="inline-flex h-(--control) items-center gap-1 rounded-lg bg-muted ps-3 pe-1 font-mono">
        BK-4821
        <CopyButton size="icon-xs" value="BK-4821" label="Copy booking number" />
      </span>
    </p>
  );
}
```

Example: A command in a read-only field, with its copy button inside the field's end (`docs/system/examples/copy-button/in-field.tsx`)

```tsx
import { CopyButton } from "@/components/cobalt/ui/copy-button";
import { Field, FieldDescription, FieldLabel } from "@/components/cobalt/ui/field";
import { InputGroup, InputGroupAddon, InputGroupInput } from "@/components/cobalt/ui/input-group";

const command = "npx shadcn@latest add https://kaanarici.com/r/cobalt/data-table.json";

export default function Example() {
  return (
    <Field className="w-full">
      <FieldLabel>Add the data table</FieldLabel>
      <InputGroup>
        {/* Read-only, so the whole command can be selected and scrolled, never edited. */}
        <InputGroupInput readOnly value={command} className="font-mono" />
        <InputGroupAddon align="end">
          <CopyButton size="icon-xs" value={command} label="Copy command" />
        </InputGroupAddon>
      </InputGroup>
      <FieldDescription>Run it in your project; it adds the component and its tokens.</FieldDescription>
    </Field>
  );
}
```

## Variants

### variant
- `ghost`: the icon form beside the value, in a row or a code block. The default. Used at 0 call sites.
- `secondary`: a labelled button, or an icon button that needs a fill beside a field. Used at 0 call sites.

Example: Ghost, the icon form, inside the chip that holds its value (`docs/system/examples/copy-button/default.tsx`)

```tsx
import { CopyButton } from "@/components/cobalt/ui/copy-button";

export default function Example() {
  return (
    <p className="flex flex-wrap items-center gap-2 text-sm">
      Booking
      {/* The chip is 36px with a 10px corner; the button sits 4px in, so its 6px corner is concentric. */}
      <span className="inline-flex h-(--control) items-center gap-1 rounded-lg bg-muted ps-3 pe-1 font-mono">
        BK-4821
        <CopyButton size="icon-xs" value="BK-4821" label="Copy booking number" />
      </span>
    </p>
  );
}
```

Example: Secondary with a label, for a link to share. The label stays as it is and the glyph turns to a check (`docs/system/examples/copy-button/variant-secondary.tsx`)

```tsx
import { CopyButton } from "@/components/cobalt/ui/copy-button";

export default function Example() {
  return (
    <CopyButton variant="secondary" value="https://example.com/watchlists/large-cap-core">
      Copy link
    </CopyButton>
  );
}
```

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| idle | rendered | press it to copy | the copy glyph; the icon form's name on hover | test |
| copied | the clipboard took the value | press again to copy again | the check glyph for 1.5 seconds, "Copied" read once, and the tooltip, kept open through the press, saying "Copied" while hovered or focused, then its label again (measured on the showcase: the tooltip stays at opacity 1 and 136.8px wide through the press, "Copied" from about 50ms after the click until 1.5 seconds later) | by hand |
| failed | the clipboard is missing or refused the write | press again to retry, or select the value by hand | the tooltip open with "Could not copy" for 3 seconds, read once | by hand |
| hover | pointer over the icon form, under `(hover: hover)` | press it | the tooltip with the label, and the ghost fill | by hand |
| focus-visible | Tab onto it | press it with Enter or Space | the Button's 2px outline on its 10px corner | by hand |
| disabled | `disabled`, such as an empty value | nothing | half opacity, and no tooltip | by hand |

Example: Pressing either button turns its glyph to a check and its tooltip to Copied for 1.5 seconds, and reads Copied once; the other stays as it was (`docs/system/examples/copy-button/copied.tsx`)

```tsx
import { CopyButton } from "@/components/cobalt/ui/copy-button";
import { DataList, DataListItem, DataListLabel, DataListValue } from "@/components/cobalt/ui/data-list";
import { TruncatedText } from "@/lib/cobalt/truncated-text";

const facts = [
  { label: "Fund id", value: "F-0001234567", copy: "Copy fund id" },
  { label: "Webhook", value: "https://example.com/hooks/filings/7f3a2c", copy: "Copy webhook address" },
];

export default function Example() {
  return (
    // Stacked, so a short id and a long address sit the same way: the name over the value and its button.
    <DataList orientation="stacked" className="w-full max-w-sm">
      {facts.map((fact) => (
        <DataListItem key={fact.label}>
          <DataListLabel>{fact.label}</DataListLabel>
          <DataListValue className="flex items-center gap-1">
            <TruncatedText className="font-mono">{fact.value}</TruncatedText>
            <CopyButton value={fact.value} label={fact.copy} />
          </DataListValue>
        </DataListItem>
      ))}
    </DataList>
  );
}
```

Example: When the clipboard refuses, the glyph stays and the tooltip opens with Could not copy for 3 seconds, so the value can be selected by hand (`docs/system/examples/copy-button/failed.tsx`)

```tsx
import { CopyButton } from "@/components/cobalt/ui/copy-button";

// Stands in for a browser that refuses the clipboard, such as a page served without HTTPS.
const refused = () => Promise.resolve(false);

export default function Example() {
  return (
    <div className="flex items-center gap-1 text-sm">
      <span className="text-muted-foreground">Door code</span>
      <code className="font-mono">GARDEN-4821</code>
      <CopyButton value="GARDEN-4821" label="Copy door code" copy={refused} />
    </div>
  );
}
```

### State precedence
- Copied and failed: Not applicable: the clipboard answers one way per press, and a new press replaces the last answer.
- Failed and hover: failed wins. The tooltip shows "Could not copy" until its 3 seconds end, then the label returns.
- Copied and hover: both show. The check in the button, "Copied" in the tooltip.
- Focus-visible and copied or failed: both show. Focus stays on the button through the result.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| idle to copied and back | announce | `IconSwap`, `--t-icon` 250ms on `--curve-in-out` | opacity and filter of each glyph | the fade stays and the blur drops, from the base styles |
| tooltip opens or closes | announce | `popup-motion` at `--t-press`; a press does not close it | opacity | the same fade |
| the tooltip's words change | announce | `TextSwap`: the old wording leaves on 70 percent of `--t-pop`, the new one arrives on `--t-icon` | opacity | the same fade |

## Props

<!-- Props generated from registry/cobalt/ui/copy-button.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `value` | `string` |  | The text that goes on the clipboard. |
| `children?` | `ReactNode` |  | A visible label, such as "Copy link". |
| `copiedLabel?` | `string` | `"Copied"` | Read once, and shown in the tooltip on hover, after a copy that worked. |
| `copy?` | `(value: string) => Promise<boolean>` | `copyText` | Writes the value somewhere and says whether it worked. |
| `failedLabel?` | `string` | `"Could not copy"` | Shown in a tooltip and read once when the clipboard refuses. |
| `inset?` | `number` | `0` | How far the button sits inside the edge of the box that holds it, on the tooltip's side, in pixels, such as 4 for a 28px button in a 36px command line. |
| `label?` | `string` | `"Copy"` | The button's name, read aloud and shown on hover, for the icon form: "Copy link". |
| `onCopy?` | `((event: BaseUIEvent<ClipboardEvent<HTMLButtonElement>>) => void) & ((copied: boolean) => void)` |  | Called with the result, after the clipboard answers. |
| `size?` | `"default" \| "sm" \| "lg" \| "icon" \| "icon-sm" \| "icon-xs" \| "icon-lg"` |  |  |
| `tooltipAlign?` | `"center" \| "start" \| "end"` | `"center"` | Where the tooltip lines up with the button: `end` for a button in a box's trailing corner, so the label stays over the box instead of hanging past its edge. |
| `variant?` | `"default" \| "secondary" \| "ghost" \| "destructive"` | `"ghost"` | `ghost` the icon form beside the value; `secondary` a labelled button. |

Also accepts every native `<button>` attribute; from Base UI: `focusableWhenDisabled`, `nativeButton`, `render`.

`value` is the text that goes on the clipboard. `label` names the icon form. `copiedLabel` and `failedLabel` replace "Copied" and "Could not copy". `children` turn it into the text form. `onCopy` hears the result. `copy` replaces the clipboard writer. `tooltipAlign`, `end` for a button in a box's trailing corner, keeps the label over the box instead of past its edge. `inset` is how far the button sits inside the edge of the box that holds it, on the tooltip's side, in pixels, such as 4 for a 28px button in a 36px command line: the tooltip then keeps its 6px gap from the box's edge, not the button's. `variant`, `size`, `disabled` and the other Button props pass through.

## Usage

### When to use
- A value is meant to be pasted elsewhere: a share link, a booking number, an API key's id, a command.
- A code block or a read-only field holds text a person will copy.

### When not to use
- The person shares a link through the device's own share sheet. Use Button instead, with `navigator.share` and this as the fallback.
- The text is long and meant to be read, such as a paragraph. Use Textarea instead, read-only, so it can be selected.

### Rules
- `rule/copy-button-answer-in-place`: When a copy finishes, let the `CopyButton` answer with its glyph and status line instead of calling `toast()`, because the result of a copy is on the clipboard, not on the page, and the button the person just pressed is where they look. Evidence: person D39; principle heuristic: visibility of system status, the answer is where you acted. Check: review of every `toast` near a copy.
  - Don't: `navigator.clipboard.writeText(link); toast("Link copied")`
  - Do: `<CopyButton value={link} label="Copy share link" />`
- `rule/copy-button-name-the-value`: When using the icon form, set `label` to the verb and the value, such as "Copy booking number", instead of "Copy", when more than 1 copy button shares a view, because the name is all a screen reader and the tooltip give, and five buttons named "Copy" are one name for five things. Evidence: principle wcag: 2.4.6 Headings and labels, a name says what it acts on. Check: review of every `CopyButton` without children.
  - Don't: `<CopyButton value={booking.id} />` beside `<CopyButton value={link} />`
  - Do: `<CopyButton value={booking.id} label="Copy booking number" />`
- `rule/copy-button-show-the-value`: When offering a copy, show the value beside the button, in full or cut with its whole text in `title`, because a person checks what they copy, and a button alone copies something they cannot see. Evidence: principle heuristic: visibility of system status; app 2/2 source apps show the value beside their copy control, docs/research/2026-10-17-coverage.md section 3. Check: review.
  - Don't: `<CopyButton value={secretUrl} label="Copy link" />` alone in a toolbar
  - Do: `<code title={url} className="truncate">{url}</code><CopyButton value={url} label="Copy link" />`

### Content
- Follows `rule/writing-tooltip-is-the-name`.
- `rule/copy-button-text-form-label`: When using the text form, write "Copy" and the object in sentence case, such as "Copy link", in at most 3 words, because the words stay the same through the result and the glyph says it worked. Evidence: principle heuristic: consistency and standards, a button names its verb and object; person D11. Check: review.
  - Don't: `<CopyButton value={link}>Click here to copy the link</CopyButton>`
  - Do: `<CopyButton variant="secondary" value={link}>Copy link</CopyButton>`

### Anti-slop
- `rule/copy-button-no-swap-words`: When the copy works, keep the text form's words and let the glyph turn into a check, instead of swapping "Copy link" to "Copied", because a label that changes its width moves the buttons beside it. Evidence: person D37; principle heuristic: visibility of system status, the glyph is the answer. Check: test `copy-button.test.tsx` renders the text form with no `aria-label`.
  - Don't: `<Button onClick={copy}>{copied ? "Copied" : "Copy link"}</Button>`
  - Do: `<CopyButton variant="secondary" value={link}>Copy link</CopyButton>`

### Limits
Not applicable: the button is one 36px control and never grows; the value beside it is the host's, and its length is the host's limit, checked in docs/system/stress/copy-button.tsx, where a signed link of over 150 characters is cut to one line beside the button at 320.

## Accessibility
Rests on the system `Button` and Base UI `Tooltip`.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the button | focuses it, shows the outline, and opens the icon form's tooltip | the button | by hand |
| Enter | the button | copies the value | stays on the button | by hand |
| Space | the button | copies the value | stays on the button | by hand |
| Escape | the button with its tooltip open | closes the tooltip | stays on the button | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| button, icon form | button | `aria-label` from `label` | none; the name never changes | the label and "button" | test |
| button, text form | button | its text | none | the text and "button" | test |
| status line | status | its text | visually hidden | "Copied" or "Could not copy", once per press | test |
| glyphs | none, `aria-hidden` | none | none | nothing | test |

## Tokens
| Part | State | Token |
|---|---|---|
| button | all | the Button's tokens: `--control`, `--r-lg`, `--muted`, `--secondary` |
| glyph | swap | `--t-icon`, `--curve-in-out`, `--blur-leave` |
| tooltip | open | `--tip`, `--tip-foreground`, `--r-md`, `--t-press` |

## Related
- IconButton: an icon button that does something other than copy.
- Button: a share button that uses the device's share sheet.
- Kbd: a key to press, not text to copy.
