Copy Button
Copies text to the clipboard and shows for a moment that it did.
Usage
import { CopyButton } from 'nyctoui/copy-button'
<CopyButton value="ENG-214" aria-label="Copy issue ID" />
<CopyButton value="ENG-214">
{({ copied }) => (copied ? 'Copied' : 'Copy')}
</CopyButton>Without children it shows a copy icon that turns into a check, on a square button; give it an aria-label. Pass children as a function of { copied } to swap a label or icon yourself. The copied state lasts timeout ms, and a live region says “Copied” to screen readers.
Good for
- An API key or a webhook secret on a developer settings page.
- A code sample in docs, at the corner of the block.
- An invite or share link in a team settings dialog.
- An order or tracking number on a confirmation page.
Examples
Icon
Takes every Button prop. Use ghost inside a field or a toolbar.
Sizes
Label
A function child gets the copy state. “Copied” is wider than “Copy”, so put the button where its width can change, such as the end of a row.
Read at click
Pass value as a function to read the text when it’s pressed, such as what’s in an input now.
In a dropdown
render replaces the button. As a function it also gets the copy state, here to swap the item’s icon. Keep the menu open with closeOnSelect={false}, or the item closes before it can show that it worked.
With a toast
onCopiedChange and onCopyError report the outcome, for feedback shown elsewhere. The clipboard refuses writes on insecure origins and in some embedded frames.
Best practices
- Put it right beside the text it copies, so it’s clear what lands on the clipboard.
- Give an icon-only button an
aria-labelthat says what it copies, such as “Copy API key”. - Pass a function as
valuewhen the text must be read at the moment of the click, like a link that includes the current filters. - Handle
onCopyError, since the clipboard can be blocked, and say so in a toast.
- Use it as the only way to see the value. People should be able to read and select the text too.
- Show a toast as well as the check for every copy. The button’s own change is enough.
API reference
CopyButton
Renders a Button and accepts its props, such as variant, size and sx.
| Prop | Type | Default |
|---|---|---|
valueText to copy, or a function that returns it when pressed. | string | () => string | — |
childrenContent, or a function of the copy state. Defaults to a copy icon that turns into a check. | ReactNode | ({ copied }) => ReactNode | — |
copiedCopied state, when controlled. | boolean | — |
defaultCopiedCopied state on mount, when uncontrolled. | boolean | false |
onCopiedChangeCalled with true after a copy and false when the timeout runs out. | (copied: boolean) => void | — |
onCopyErrorCalled when the clipboard refuses the write. | (error: unknown) => void | — |
timeoutHow long the copied state lasts, in ms. | number | 1500 |
copiedLabelAnnounced to screen readers after a copy. | string | 'Copied' |
iconOnlySquare the button. Pass an aria-label. | boolean | true without children |
renderReplaces the button, e.g. render={<DropdownItem />}. | ReactElement | (props, { copied }) => ReactElement | <Button /> |