Skip to main content

Copy button

Requires 2.0

CopyButton ships in @rtorcato/shadcn-ui 2.0.0 and later. On 1.x the import fails with ERR_PACKAGE_PATH_NOT_EXPORTED.

A button that copies value to the clipboard. It is icon-only with a tooltip by default, or labelled with showLabel. After each attempt it shows a copied or failed state for timeout ms, then returns to idle. It wraps the useCopy hook.

import { CopyButton, type CopyButtonProps } from '@rtorcato/shadcn-ui/components/ui-extended/copy-button'

Usage​

<CopyButton value={command} />

<CopyButton value={apiKey} showLabel label="Copy key" />

// Inside a code block
<CopyButton value={command} variant="ghost" size="icon-sm" />

Props​

Also accepts every Button prop except value, children, onClick and the DOM onCopy event, which onCopy below replaces.

PropTypeDefaultDescription
valuestring—Text written to the clipboard.
labelstring'Copy'Idle label: the aria-label and tooltip (icon-only), or the visible text.
copiedLabelstring'Copied'Text shown after a successful copy.
failedLabelstring"Couldn't copy"Text shown after a failed copy.
showLabelbooleanfalseShow the label beside the icon instead of in a tooltip.
timeoutnumber2000ms before returning to idle.
onCopy(ok: boolean) => void—Fires after each attempt with the result.
variantButton variant'outline'Use 'ghost' inside code blocks.
sizeButton size'icon', or 'default' with showLabel

Accessibility​

  • Icon-only, the aria-label stays label in every state. A polite role="status" region announces the copied or failed text, for both variants.
  • With showLabel, the visible text is the accessible name and the button keeps its width as the text changes.
  • States are carried by icon shape and text, not colour.
  • Icon-only buttons keep a 44px touch target at every icon size. Disabled buttons show no tooltip.