# Clipboard

Copies a value such as an API key, a link or a command.

Copy a value: an API key, a link, a command. Copy a value. The trigger is the code block's Copy button: the Button with two states in one cell (Copy, then Copied, cross-fading and tinted green while the copied state lasts). variant button is that button alone; variant field puts an icon-only Copy button inside the end of a read-only Input that shows the value (an API key, a link). The change is announced through a status region. Behaviour comes from the Zag clipboard machine, through Ark UI.

Kind: component · Category: Actions · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `clipboard` machine, through Ark UI

## Add

```bash
npx fndn add clipboard
```

It writes the folder `clipboard/` into the project's ui folder (`index.ts`, `clipboard.tsx`, `Clipboard.vue`, `Clipboard.svelte`, `clipboard.recipe.ts`), and what it is built from: [Button](/components/button.md), [Input](/components/input.md), [Tooltip](/components/tooltip.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { Clipboard } from "@/components/ui/clipboard";
```

```vue
import { Clipboard } from "@/components/ui/clipboard";
```

```svelte
import { Clipboard } from "$lib/components/ui/clipboard";
```

## Parts

In the DOM the root carries `data-slot="clipboard"` and each part `data-slot="clipboard-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `clipboard` |  | data-copied while the copied state lasts (from the engine) |
| `label` | `label` | `clipboard-label` | optional | field only: names the field |
| `control` | `div` | `clipboard-control` | optional | field only: holds the input and the trigger |
| `input` | `input` | `clipboard-input` | optional | field only: the real Input, read-only, marked data-clipboard="input"; its end padding keeps the trigger's room (28px xs, 30px sm, 34px md, 36px lg), so a long value is cut before the button |
| `trigger` | `button` | `clipboard-trigger` |  | The real Button (ghost, xs, muted), marked data-clipboard="trigger", swapped while copied: copy and check icons share one cell and cross-fade; green while copied. button: rounded, with the two labels (Copy, Copied) in one cell; as wide as the label it shows. field: square, icon only, in the real Tooltip, 4px inside the field's end (1px at sm). aria-label stays Copy; disabled when the value is empty. |
| `status` | `span` | `clipboard-status` |  | Visually hidden role=status: says Copied while the copied state lasts |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` |  | The text placed on the clipboard (and shown in the field). |
| `variant` | `"button" \| "field"` | `"button"` | button: the code block's Copy button. field: a read-only Input showing the value, with an icon-only Copy button inside its end. |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | field only: the Input's sizes (`lib/control.ts`): xs 24px, sm 28px, md 32px, lg 36px. |
| `label` | `string` |  | field only: visible label; otherwise pass aria-label. |
| `labels` | `object` |  | { copy, copied }: the button's labels and the announcement. Default Copy / Copied. |
| `timeout` | `number` | `1500` | How long Copied shows, in ms (the code block's). |
| `disabled` | `boolean` | `false` |  |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `copy` | (text: string) once the text is on the clipboard |

## States

`hover`, `press`, `focus-visible`, `copied`, `disabled`

## Keyboard and accessibility

Role: button.

Keyboard:

- Tab reaches the field (read-only, its text selectable) and the button
- Space / Enter on the button copy

Notes:

- The button's name stays Copy (its two stacked labels are aria-hidden); the status region announces Copied.
- The field's icon-only button is named by its Tooltip and its aria-label.
- A read-only field keeps its text selectable, so the value can also be copied by hand.

## Motion

- the two states cross-fade: opacity and a 2px blur over duration.base (the Button's swapped)
- colour: green while copied, over duration.fast
- press: the Button's (motion.press-scale over duration.press, easing.out-strong)
- a copy from the keyboard swaps the same way: the change is the feedback

## Tokens

`color.field`, `color.surface`, `color.hover`, `color.hover-2`, `color.ink`, `color.ink-3`, `color.green`, `color.accent`, `color.accent-tint`, `shadow.hairline`, `radius.control`, `radius.chip`, `text.xs`, `text.sm`, `duration.fast`, `duration.base`, `duration.press`, `easing.out-strong`, `motion.press-scale`

## Dependencies

- [Button](/components/button.md)
- [Input](/components/input.md)
- [Tooltip](/components/tooltip.md)

## Examples

### Copy an API key

React · `api-key.tsx`

```tsx
import { Clipboard } from "@/components/ui/clipboard";

/* field: the value stays readable and selectable; the button copies it */
export default function ApiKey() {
  return <Clipboard variant="field" label="Live API key" value="nw_live_4fK9qL2mX8vR7tB3" className="w-80" />;
}
```

Vue · `api-key.vue`

```vue
<script setup lang="ts">
import { Clipboard } from "@/components/ui/clipboard";
</script>

<!-- field: the value stays readable and selectable; the button copies it -->
<template>
  <Clipboard variant="field" label="Live API key" value="nw_live_4fK9qL2mX8vR7tB3" class="w-80" />
</template>
```

Svelte · `api-key.svelte`

```svelte
<script lang="ts">
  import { Clipboard } from "$lib/components/ui/clipboard";
</script>

<!-- field: the value stays readable and selectable; the button copies it -->
<Clipboard variant="field" label="Live API key" value="nw_live_4fK9qL2mX8vR7tB3" class="w-80" />
```

### Copy a command

React · `checkout-command.tsx`

```tsx
import { Clipboard } from "@/components/ui/clipboard";

/* button: Copy beside a one-line command; it turns to Copied for a moment */
const command = "git checkout feat/usage-alerts";

export default function CheckoutCommand() {
  return (
    <div className="flex w-80 items-center justify-between gap-3 rounded-control bg-inset py-1 ps-3 pe-1">
      <code className="min-w-0 truncate font-mono text-xs text-ink-2" title={command}>{command}</code>
      <Clipboard value={command} />
    </div>
  );
}
```

Vue · `checkout-command.vue`

```vue
<script setup lang="ts">
import { Clipboard } from "@/components/ui/clipboard";

/* button: Copy beside a one-line command; it turns to Copied for a moment */
const command = "git checkout feat/usage-alerts";
</script>

<template>
  <div class="flex w-80 items-center justify-between gap-3 rounded-control bg-inset py-1 ps-3 pe-1">
    <code class="min-w-0 truncate font-mono text-xs text-ink-2" :title="command">{{ command }}</code>
    <Clipboard :value="command" />
  </div>
</template>
```

Svelte · `checkout-command.svelte`

```svelte
<script lang="ts">
  import { Clipboard } from "$lib/components/ui/clipboard";

  /* button: Copy beside a one-line command; it turns to Copied for a moment */
  const command = "git checkout feat/usage-alerts";
</script>

<div class="flex w-80 items-center justify-between gap-3 rounded-control bg-inset py-1 ps-3 pe-1">
  <code class="min-w-0 truncate font-mono text-xs text-ink-2" title={command}>{command}</code>
  <Clipboard value={command} />
</div>
```

### Copy a deploy ID (own labels)

React · `deploy-id.tsx`

```tsx
import { Clipboard } from "@/components/ui/clipboard";

/* labels name what is copied; Copy and Copied keep one width */
export default function DeployId() {
  return (
    <div className="flex items-center gap-2 text-sm">
      <span className="text-ink-2">Deploy</span>
      <code className="font-mono text-xs text-ink">dpl_7f3a9c</code>
      <Clipboard value="dpl_7f3a9c" labels={{ copy: "Copy ID", copied: "ID copied" }} />
    </div>
  );
}
```

Vue · `deploy-id.vue`

```vue
<script setup lang="ts">
import { Clipboard } from "@/components/ui/clipboard";
</script>

<!-- labels name what is copied; Copy and Copied keep one width -->
<template>
  <div class="flex items-center gap-2 text-sm">
    <span class="text-ink-2">Deploy</span>
    <code class="font-mono text-xs text-ink">dpl_7f3a9c</code>
    <Clipboard value="dpl_7f3a9c" :labels="{ copy: 'Copy ID', copied: 'ID copied' }" />
  </div>
</template>
```

Svelte · `deploy-id.svelte`

```svelte
<script lang="ts">
  import { Clipboard } from "$lib/components/ui/clipboard";
</script>

<!-- labels name what is copied; Copy and Copied keep one width -->
<div class="flex items-center gap-2 text-sm">
  <span class="text-ink-2">Deploy</span>
  <code class="font-mono text-xs text-ink">dpl_7f3a9c</code>
  <Clipboard value="dpl_7f3a9c" labels={{ copy: "Copy ID", copied: "ID copied" }} />
</div>
```

### Invite link (small)

React · `invite-link.tsx`

```tsx
import { Clipboard } from "@/components/ui/clipboard";

/* sm (28px) field, for a share popover or a dense settings row */
export default function InviteLink() {
  return (
    <div className="flex w-80 flex-col gap-1.5">
      <Clipboard variant="field" size="sm" label="Invite link" value="https://northwind.example/join/8kd2q71x" />
      <p className="text-xs text-ink-2">Anyone with the link joins Northwind as a viewer until Nov 21, 2026.</p>
    </div>
  );
}
```

Vue · `invite-link.vue`

```vue
<script setup lang="ts">
import { Clipboard } from "@/components/ui/clipboard";
</script>

<!-- sm (28px) field, for a share popover or a dense settings row -->
<template>
  <div class="flex w-80 flex-col gap-1.5">
    <Clipboard variant="field" size="sm" label="Invite link" value="https://northwind.example/join/8kd2q71x" />
    <p class="text-xs text-ink-2">Anyone with the link joins Northwind as a viewer until Nov 21, 2026.</p>
  </div>
</template>
```

Svelte · `invite-link.svelte`

```svelte
<script lang="ts">
  import { Clipboard } from "$lib/components/ui/clipboard";
</script>

<!-- sm (28px) field, for a share popover or a dense settings row -->
<div class="flex w-80 flex-col gap-1.5">
  <Clipboard variant="field" size="sm" label="Invite link" value="https://northwind.example/join/8kd2q71x" />
  <p class="text-xs text-ink-2">Anyone with the link joins Northwind as a viewer until Nov 21, 2026.</p>
</div>
```

### Revoked key (disabled)

React · `revoked-key.tsx`

```tsx
import { Clipboard } from "@/components/ui/clipboard";

/* disabled: a revoked key stays visible for reference but cannot be copied */
export default function RevokedKey() {
  return (
    <div className="flex w-80 flex-col gap-1.5">
      <Clipboard variant="field" label="Previous API key" value="nw_live_9Qx3Lm7Ty2Pd8KcW" disabled />
      <p className="text-xs text-ink-2">Revoked on Nov 14, 2026 by Maya Chen.</p>
    </div>
  );
}
```

Vue · `revoked-key.vue`

```vue
<script setup lang="ts">
import { Clipboard } from "@/components/ui/clipboard";
</script>

<!-- disabled: a revoked key stays visible for reference but cannot be copied -->
<template>
  <div class="flex w-80 flex-col gap-1.5">
    <Clipboard variant="field" label="Previous API key" value="nw_live_9Qx3Lm7Ty2Pd8KcW" disabled />
    <p class="text-xs text-ink-2">Revoked on Nov 14, 2026 by Maya Chen.</p>
  </div>
</template>
```

Svelte · `revoked-key.svelte`

```svelte
<script lang="ts">
  import { Clipboard } from "$lib/components/ui/clipboard";
</script>

<!-- disabled: a revoked key stays visible for reference but cannot be copied -->
<div class="flex w-80 flex-col gap-1.5">
  <Clipboard variant="field" label="Previous API key" value="nw_live_9Qx3Lm7Ty2Pd8KcW" disabled />
  <p class="text-xs text-ink-2">Revoked on Nov 14, 2026 by Maya Chen.</p>
</div>
```

## Recipe

`clipboard.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* clipboard — copy a value: the code block's Copy button, on its own or at the end of a read-only
 * field that shows the value (an API key, a link). Behaviour comes from the Zag clipboard machine
 * (data-copied while the copied state lasts).
 *
 * The trigger is the real Button with two states in one cell (the Button's swapped prop): Copy
 * then Copied, the icons and labels cross-fading, tinted green while copied, as the code block's.
 *   button  the code block's Copy button: ghost, xs, rounded, muted, with its label
 *   field   the real Input (read-only) with an icon-only Copy button 4px inside its end; the
 *           Input keeps room for it (24px button + 4px + 6px), so a long value is cut before it
 * The change is announced through a visually hidden status region. */

export const clipboard = tv({
  slots: {
    base: "inline-flex flex-col gap-1.5 min-w-0 max-w-full",
    label: "text-sm font-medium text-ink",
    control: "relative flex min-w-0",
    input: "pe-[34px]",
    trigger: "",
    status: "sr-only",
  },
  variants: {
    variant: {
      button: {},
      field: {
        base: "flex",
        trigger: "absolute inset-y-0 end-1 my-auto",
      },
    },
    /* the Copied state: green, as the code block's */
    copied: {
      true: { trigger: "text-green!" },
    },
    /* the Input's sizes (lib/control.ts); the button keeps its inset from the field's edge */
    size: {
      xs: {},
      sm: {},
      md: {},
      lg: {},
    },
  },
  compoundVariants: [
    { variant: "field", size: "xs", class: { input: "pe-[28px]", trigger: "end-0" } },
    { variant: "field", size: "sm", class: { input: "pe-[30px]", trigger: "end-0.5" } },
    { variant: "field", size: "lg", class: { input: "pe-[36px]", trigger: "end-1.5" } },
  ],
  defaultVariants: { variant: "button", size: "md" },
});

export type ClipboardRecipeProps = VariantProps<typeof clipboard>;
```
