How it works
Uses navigator.clipboard.writeText, and falls back to a hidden textarea with execCommand('copy') when the page is not secure or permission is denied, restoring focus and the selection afterwards. The copy, check and alert icons share one cell and cross-fade with scale and blur. In labelled mode an invisible sizer holds the longest label, so the button keeps its size across states. Repeated clicks restart the timer and unmounting clears it. Filled variants (solid and ink) turn green after a copy; the icon-only bubble is an ink pill with a caret.
Use it for
- Beside order, tracking or reference numbers.
- Invite codes, coupon codes and other short text people paste elsewhere.
- Code blocks, masked API keys and share links.
Look elsewhere when
- Rich text or images: the clipboard call here handles plain text only.
- Content the person cannot see: show it first, then offer to copy it.
Usage
import { CopyButton } from './CopyButton.jsx'
// Icon only: an ink pill bubble confirms the copy
<CopyButton text={inviteCode} variant="outline" aria-label="Copy invite code" />
// Ink button with a label, inside a read-only link field
<CopyButton text={inviteUrl} variant="ink" showLabel label="Copy link" />
// Read the value at click time
<CopyButton text={() => inputRef.current?.value ?? ''} size="sm" aria-label="Copy" />Props
| Prop | Type | Default | What it does |
|---|---|---|---|
text | string | (() => string) | — | Text to copy. Pass a function to read the value at click time. |
resetDelay | number | 1600 | How long the result stays before the button returns, in ms. |
showLabel | boolean | false | Shows text beside the icon. The width is fixed to the longest label, so the layout never shifts. |
tooltip | boolean | true | Icon-only mode: shows a small bubble above the button after copying. |
label | string | 'Copy' | Idle label, also the accessible name in icon-only mode. Override with aria-label for something specific. |
copiedLabel | string | 'Copied' | Text shown and announced after a successful copy. |
failedLabel | string | 'Copy failed' | Text shown and announced when copying fails. |
variant | 'ghost' | 'outline' | 'solid' | 'ink' | 'ghost' | Ghost sits beside text; outline is a raised surface with a hairline ring; solid (accent) and ink (text colour, inverts in dark) are filled and turn green after a copy. |
size | 'sm' | 'md' | 'md' | Height 28px or 36px; radius 10px or 12px. |
onCopy | (text) => void | — | Called after a successful copy. |
onError | (error) => void | — | Called when copying fails. |
...rest | ButtonHTMLAttributes | — | Other attributes, such as aria-label and disabled, go to the <button>. |
Accessibility
- A polite live region next to the button announces “Copied” or “Copy failed”.
- Icon-only buttons have an aria-label; in labelled mode the inactive labels are hidden from assistive technology.
- The fallback returns focus to the button and restores the text selection.
- Reduced motion removes scale, blur and the drawn check, keeping the fade.
Motion
Icon out: 120ms ease-in (scale 0.4, blur 4px). Icon in: opacity 240ms ease-out, scale 360ms spring. Check stroke: 320ms ease-out after 60ms. Label resize: 240ms standard. Bubble in: 160ms / 240ms spring; out: 120ms ease-in. Press scale 0.94 (0.97 with a label).
Add it to your project
Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.
CopyButton.jsxcopy-button.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.



