# Copy Button

Copies a value; its label swaps for a pen-drawn tick and "Copied" without changing width.

Docs: https://ballpoint.st9wd.com/docs/copy-button

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

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

<CopyButton value="hello@example.com">Copy email</CopyButton>
```

Props:

- `value`: `string | (() => string)`. What to copy, or a function that returns it when clicked.
- `children`: `ReactNode` (default `"Copy"`). The label. Icon sizes show the copy icon instead and use this as the button's accessible name.
- `copiedLabel`: `ReactNode` (default `"Copied"`). Shown, after a drawn tick, once it's copied.
- `timeout`: `number` (default `1600`). ms before it goes back to the label.
- `onCopy`: `(value: string) => void`. Called once the value is on the clipboard.
- `variant, size`: `Button's` (default `"outline"`). Any Button variant and size; icon sizes swap the copy icon for the tick.

Pen settings: `draw`, `roughness`, `passes`, `radius`, `corners`, `fill`, `shadow`, `weight`, `speed` (see Pen settings).

## Example

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

export default function CopyButtonDemo() {
  return (
    <div className="flex flex-col items-center gap-6">
      <CopyButton value="hello@example.com" seed="copy-email">
        Copy email
      </CopyButton>
      <div className="flex items-center gap-2 font-mono text-sm text-ink-2">
        <code>npx shadcn@latest add @ballpoint/copy-button</code>
        <CopyButton value="npx shadcn@latest add @ballpoint/copy-button" variant="ghost" size="icon-sm" seed="copy-command">
          Copy command
        </CopyButton>
      </div>
    </div>
  );
}
```

## Variants and sizes

Any button variant, a custom copied label, and the icon size.

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

export default function CopyButtonStates() {
  return (
    <div className="flex flex-wrap items-center gap-5">
      <CopyButton value="ballpoint.st9wd.com" variant="default" seed="cbs-default">
        Copy URL
      </CopyButton>
      <CopyButton value="ballpoint.st9wd.com" variant="outline" size="sm" copiedLabel="Got it" seed="cbs-sm">
        Copy, small
      </CopyButton>
      <CopyButton value="ballpoint.st9wd.com" variant="ghost" seed="cbs-ghost">
        Ghost
      </CopyButton>
      <CopyButton value="ballpoint.st9wd.com" size="icon" seed="cbs-icon">
        Copy URL
      </CopyButton>
    </div>
  );
}
```

## Read at click time

Pass a function and the value is read when it's clicked.

```tsx
"use client";

import { useState } from "react";
import { CopyButton } from "@/components/ui/copy-button";
import { Input } from "@/components/ui/input";

// Pass a function and the value is read at the moment of the click.
export default function CopyButtonDynamic() {
  const [text, setText] = useState("Dear Ada,");
  const [copied, setCopied] = useState<string>();
  return (
    <div className="flex w-full max-w-md flex-col gap-4">
      <div className="flex items-center gap-3">
        <Input aria-label="Text to copy" value={text} onChange={(e) => setText(e.target.value)} className="flex-1" seed="cbd-input" />
        <CopyButton value={() => text} onCopy={setCopied} seed="cbd-copy">
          Copy
        </CopyButton>
      </div>
      {copied && <p className="text-sm text-ink-3">Last copied: &quot;{copied}&quot;</p>}
    </div>
  );
}
```

## Beside a command

An icon button at the end of a line of code.

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

const command = "pnpm dlx shadcn@latest add @ballpoint/card";

export default function CopyButtonSnippet() {
  return (
    <Card seed="cbs-card" className="w-full max-w-xl flex-row items-center justify-between gap-4 px-4 py-2.5">
      <code className="min-w-0 overflow-x-auto font-mono text-sm whitespace-nowrap text-ink-2">
        <span className="text-ink-3 select-none">$ </span>
        {command}
      </code>
      <CopyButton value={command} variant="ghost" size="icon-sm" seed="cbs-copy">
        Copy command
      </CopyButton>
    </Card>
  );
}
```

## Beside a field

A read-only link with a labelled copy button next to it.

```tsx
import { CopyButton } from "@/components/ui/copy-button";
import { Field, FieldDescription, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

export default function CopyButtonInput() {
  return (
    <Field className="w-full max-w-sm">
      <FieldLabel htmlFor="cbi-link">Invite link</FieldLabel>
      <div className="flex items-center gap-2">
        <Input id="cbi-link" readOnly defaultValue="https://example.com/join/4fK9q" seed="cbi-link" className="min-w-0 flex-1" />
        <CopyButton value="https://example.com/join/4fK9q" copiedLabel="Copied" seed="cbi-copy">
          Copy
        </CopyButton>
      </div>
      <FieldDescription>Anyone with it can join. It stops working in a week.</FieldDescription>
    </Field>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `radius`: `number | "full"` (default `0`). Corner radius in px; "full" draws a pill.
- `corners`: `"crossed" | "joined"` (default `"crossed"`). Square corners: sides pulled separately past each other, or the box drawn in one motion.
- `fill`: `"shade" | "hatch" | "scribble" | "flat"`. How an area is coloured in. Solid parts default to shade, light ones to hatch.
- `shadow`: `"hatch" | "solid" | "none"` (default `"hatch"`). What a lifted box leaves on the paper. "none" turns the lift off too.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
