# Copy Button

An icon button that copies a value, with copied and failed feedback.

Page: https://sui.draco.dev/docs/components/copy-button

### Example: copy-button-demo

```tsx
"use client";
import { CopyButton } from "@workspace/ui/components/copy-button";
import { clipboardLabelsZhCN } from "@workspace/ui/hooks/use-clipboard";
import type { ExampleProps } from "../types";

const command = "bunx --bun shadcn@latest add @sui/copy-button";

export default function Example({ locale }: ExampleProps) {
  const chinese = locale === "zh-CN";
  return (
    <div className="flex w-full max-w-md items-center gap-2 rounded-xl bg-muted px-3 py-2 font-mono text-sm">
      <span className="min-w-0 flex-1 truncate">{command}</span>
      <CopyButton
        value={command}
        labels={chinese ? clipboardLabelsZhCN : undefined}
      />
    </div>
  );
}
```

## Installation

```bash
npx shadcn@latest add @sui/copy-button
```

With pnpm, Yarn or Bun, use `pnpm dlx`, `yarn dlx` or `bunx --bun` in place of `npx`, and `pnpm add`, `yarn add` or `bun add` in place of `npm install`.

Install with the shadcn CLI or use the shared `@workspace/ui` package. Follow the [installation guide](/docs/installation) to configure the registry, load styles, and choose import aliases.

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

## Usage

```tsx
<CopyButton value={command} />
```

The icon morphs to a check mark after the clipboard write succeeds, or to a cross when it fails, and the tooltip and a polite live region say the same. The button is disabled while a write is pending. It is a [Tooltip Button](/docs/components/tooltip-button), so `variant`, `size`, `disabled` and `className` pass through; it defaults to a small ghost icon button. [Code Viewer](/docs/viewers/code-viewer), [Diff Viewer](/docs/viewers/diff-viewer) and the [Code Editor](/docs/components/code-editor) `copy()` plugin use it.

## Copy field

A read-only copy field is an [Input Group](/docs/components/input-group) composition, as in shadcn: `render` turns the button into an `InputGroupButton`.

### Example: copy-button-input-group

```tsx
"use client";
import { CopyButton } from "@workspace/ui/components/copy-button";
import {
  InputGroup,
  InputGroupAddon,
  InputGroupButton,
  InputGroupInput,
} from "@workspace/ui/components/input-group";
import { clipboardLabelsZhCN } from "@workspace/ui/hooks/use-clipboard";
import type { ExampleProps } from "../types";

const command = "bun add @workspace/ui";

export default function Example({ locale }: ExampleProps) {
  const zh = locale === "zh-CN";
  return (
    <InputGroup className="w-full max-w-md font-mono">
      <InputGroupInput
        readOnly
        value={command}
        aria-label={zh ? "安装命令" : "Install command"}
      />
      <InputGroupAddon align="inline-end">
        <CopyButton
          value={command}
          render={<InputGroupButton size="icon-xs" />}
          labels={zh ? clipboardLabelsZhCN : undefined}
        />
      </InputGroupAddon>
    </InputGroup>
  );
}
```

## Labels

`labels` overrides `copy`, `pending`, `copied` and `copyFailed`. Pass `clipboardLabelsZhCN` from `@workspace/ui/hooks/use-clipboard` for Chinese. `CopyText` shares the same labels.

## API reference

| Prop | Type | Default / behavior |
| --- | --- | --- |
| `value` | `string` | Required text to copy. |
| `labels` | `ClipboardLabels` | English labels. |
| `render` | `ReactElement` | Renders another button, such as `<InputGroupButton />`. |
| `disabled`, `resetDelay`, `onCopy`, `onCopyError` | Clipboard options | As in `CopyText`. |
| `variant`, `size` | Button props | `"ghost"`, `"icon-sm"`. |
| `feedback` | `FeedbackOption` | Follows `FeedbackProvider`; haptics when a copy succeeds or fails. |

The module exports `CopyButtonProps`. The button carries `data-copy-status` (`idle`, `pending`, `copied` or `error`) for `data-[copy-status=copied]:` styles.
