Copy button
One copy button owns the write, the feedback and the failure, and it copies the real value, not the text on screen.
The problem
navigator.clipboard.writeText(value) is one line, so every screen grows its own copy button. Count them in a mature app and you’ll find a dozen that disagree: different icons, some flash a check and some don’t, the check lasts one second here and two there, and half of them fail silently.
And most copy whatever is on screen, which is formatted for reading, not for pasting.
What I concluded
One component owns the whole exchange. A call site passes what to copy and what to call it.
<CopyButton value={order.placedAt} label="date" />
async function copy() {
try {
await navigator.clipboard.writeText(value);
} catch {
toast.error("Couldn't copy to clipboard");
return; // silence would read as success
}
setCopied(true); // the check stands in briefly
clearTimeout(timer.current);
timer.current = setTimeout(() => setCopied(false), 1500);
}
What goes on the clipboard is the value, shaped for wherever it gets pasted:
| On screen | On the clipboard |
|---|---|
| 3 days ago | 2026-11-14T09:12:00.000Z |
| gift, fragile, rush | one tag per line, a column in a sheet |
| $1,204.50 | 1204.5 |
A relative date that copies as “3 days ago” is wrong by tomorrow.
Rules
- Failure is visible. A blocked clipboard says so instead of flashing a check it didn’t earn.
- Announce what was copied. A live region next to the button says “Copied date”, not just “Copied”.
- Don’t take focus. Preventing the default on mousedown keeps focus where it was, so a surrounding editor doesn’t light up. The click still fires.
- Always visible, except dense rows. Hover doesn’t exist on touch. In a property list it shows on hover and focus, hidden with opacity so nothing shifts.
- Copy link is just the address. When the URL is the state, it copies
location.href.
What you get
One wording, one timing, one failure path, and one place to fix it. The copy line was never the hard part. The feedback, the failure and the payload are, and those are what get skipped when the button is rewritten in place.
The demo copies a date and some tags, and lets you block the clipboard to see the failure. Related: Toasts, URL state.