Copy button
Copy button
Copies on the press: the icon blurs into a self-drawing check, the label morphs to Copied, and a failed copy shakes and says so.
Dependencies
Interaction states
- idle — The copy icon, and "Copy" when labelled.
- copied — On the press: the icon blurs out (scale 0.6, 3px) while an emerald check draws itself in 360ms; the label morphs to "Copied" and the pill eases wider. Back to idle after 2s.
- error — If the clipboard refuses: red tint, an alert icon, "Failed" and a 380ms shake.
Installation
Add this component with your preferred package manager.
pnpm dlx shadcn@4.21.5 add @cavemio/copy-buttonFirst install? Configure @cavemio once
Run npx shadcn@4.21.5 init if shadcn is not set up, then merge this entry into the registries field in your existing components.json. Keep your other settings and registries.
{
"registries": {
"@cavemio": "https://registry.cavemio.dev/r/cavemio-{name}.json"
}
}Where it’s used
Code blocks
Top-right copy button in syntax-highlighted code specimens.
Share links & invite codes
One-tap copy for URLs and referral tokens.
API keys & secrets
Fast copying of sensitive tokens with visual confirmation.
What it handles
- Checkmark morph — The copy icon crossfades smoothly into a green checkmark on successful copy.
- Instant feedback — The copied state lands on the press itself; the clipboard write catches up, and if it is refused the button says Failed and shakes.
- Label morph — Icon only, or labelled: "Copy" morphs into "Copied" through TextMorph and the pill eases to its new width.
- Self-reverting timer — Returns to idle after timeoutMs (2000ms); pressing again while copied copies again and restarts it.
- Accessible announcement — A status region outside the button announces "Copied" (or "Failed"); the icon-only button keeps "Copy" as its name.
Usage
Interactive local fixture. Data and async results are simulated in the preview; the caller supplies application services.
import { CopyButton } from "@/components/copy-button";
<CopyButton text="npm i @thin line/button" />
<CopyButton text="https://thin lineui.com" showLabel />Props
| Prop | Type | Description |
|---|---|---|
| text | string | – · The string to copy. |
| label | string | "Copy" · Label while idle; also the icon-only button's accessible name. |
| copiedLabel | string | "Copied" · Label once copied. |
| errorLabel | string | "Failed" · Label if the clipboard refuses. |
| showLabel | boolean | false · Show the label beside the icon. |
| timeoutMs | number | 2000 · How long the copied state stays. |
| variant | "secondary" | "outline" | "ghost" | "ghost" · Visual appearance. |
| size | "xs" | "sm" | "md" | "sm" · 24, 28 or 36px tall. |
| onCopySuccess / onCopyError | (text) => void / (error) => void | – · Called once the clipboard answers. |
Accessibility & motion
- Uses navigator.clipboard, with a selection fallback for older browsers and plain-http pages. - The copied state is optimistic: it shows on the press, not after the clipboard promise. - Reduced motion: icons and labels change in place, no shake.
pnpm dlx shadcn@4.21.5 add @cavemio/copy-button