# CopyButton

Copies a text value to the clipboard with an in-place icon cross-fade and a polite status announcement. Icon-only by default, or labelled alongside the icon.

**Dependencies:** npm: 2 direct, 3 total; registry: 2 direct, 2 total.

## Usage

### When to use

- Let users copy a command, token, URL, or short snippet in one press.
- Show in-place confirmation on the button with a polite screen-reader status announcement.

### When not to use

- Display a multi-line code snippet with syntax highlighting and line numbers. Use CodeBlock.
- Trigger a general action that does not write to the clipboard. Use Button.

## Variants

### size

Height from size-element-*: sm 28px, md 32px, lg 36px, xl 44px with a mouse; larger on touch. Icons are 16px up to lg, 20px in xl.

Values: `sm`, `md`, `lg`, `xl`. Default: `md`.

### variant

ghost for quiet toolbar or inline actions, secondary for a raised bordered button, soft for muted accent fill.

Values: `ghost`, `secondary`, `soft`. Default: `ghost`.

## Examples

### Icon only

Compact square copy button for inline tokens, commands, or table rows.

```tsx
<div className="flex items-center gap-sm">
  <code className="font-mono text-sm text-text-primary">npx werewolf-ui add copy-button</code>
  <CopyButton value="npx werewolf-ui add copy-button" label="Copy command" />
</div>
```

### With label

Set iconOnly={false} to show the label beside the cross-fading icon.

```tsx
<CopyButton variant="secondary" iconOnly={false} value="https://www.werewolf-ui.dev" label="Copy link" copiedLabel="Link copied" />
```

## Do and don't

### Do

- Pass the exact plain-text value to copy, such as an API token, CLI command, or share URL.
- Pair an icon-only CopyButton with a Tooltip or clear label so its purpose is unmistakable.

### Don't

- Don't use CopyButton for general actions that do not write to the clipboard; use Button.
- Don't omit a descriptive label when multiple copy buttons sit on the same screen.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `value` (required) | `string` |  |  | Plain text written to the clipboard when pressed. |
| `label` | `string` |  | `Copy` | Idle accessible name when iconOnly, and visible button text when iconOnly is false. |
| `copiedLabel` | `string` |  | `Copied` | Accessible name, visible text when labelled, and polite status announcement after copying. |
| `size` | `enum` | `sm`, `md`, `lg`, `xl` | `md` | Height from size-element-*: sm 28px, md 32px, lg 36px, xl 44px with a mouse; larger on touch. Icons are 16px up to lg, 20px in xl. |
| `variant` | `enum` | `ghost`, `secondary`, `soft` | `ghost` | ghost for quiet toolbar or inline actions, secondary for a raised bordered button, soft for muted accent fill. |
| `iconOnly` | `boolean` |  | `true` | Square icon-only button by default. Set to false to display label beside the icon. |
| `timeout` | `number` |  | `2000` | Duration in milliseconds before the copied confirmation resets to idle. |
| `disabled` | `boolean` |  | `false` | Disables presses and applies neutral disabled styling. |
| `onCopy` | `(value: string) => void` |  |  | Called with the copied string after clipboard write succeeds. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | The button element. |
| `icon` | The cross-fading copy and check icons inside the button, 16px in sm, md and lg, 20px in xl. The check mark uses color-text-success. |
| `hitArea` | Transparent ::before centered on the root, never smaller than size-target-min (24px on mouse, 44px on touch). |

## Accessibility

Role: `button`

| Keys | Action |
| --- | --- |
| `Tab` | Moves focus to the button and shows the focus ring. |
| `Enter, Space` | Copies the value to the clipboard and announces the copied label. |

- Defaults to type="button" so it never submits a form by accident.
- Sets aria-label to label (default "Copy") when idle and copiedLabel (default "Copied") after copying.
- A polite role="status" live region announces copiedLabel when the copy succeeds.

## Install

```bash
npx werewolf-ui add https://www.werewolf-ui.dev/r/copy-button.json
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.
