Skip to content

CopyButton

CopyButton puts a piece of text on the clipboard. After a copy it shows a check for two seconds, then goes back to the copy icon. It is icon-only unless you give it a label.

Demos

Basic

Without children the button shows only its icon and is named “Copy” from the locale.

With a label

Children become a visible label beside the icon.

Size

Heights match Button of the same size.

Text from a function

text can be a function; it runs on click, so it can read the latest value. onCopy gets the text once the clipboard accepted it, and onError gets the error when it did not.

Disabled

Accessibility

CopyButton is a native button type="button", so Space and Enter copy. Icon-only, it is named by the locale’s “Copy” (or your aria-label); with a label, the label names it. After a copy its name becomes the locale’s “Copied” for copiedDuration, so a screen reader hears the result. The icons are hidden from assistive tech.

Props

NameTypeDefaultDescriptionVersion
textstring | (() => string)requiredThe text to copy, or a function that returns it at click time.Unreleased
onCopy(text: string) => voidundefinedCalled with the copied text once the clipboard accepted it.Unreleased
onError(error: unknown) => voidundefinedCalled when the clipboard is missing or refuses the text.Unreleased
copiedDurationnumber2000How long, in ms, the copied state lasts.Unreleased
sizesmall | medium | largemediumButton size.Unreleased
iconReactNodecopy iconReplaces the copy icon.Unreleased
copiedIconReactNodecheck iconReplaces the check shown after a copy.Unreleased
childrenReactNodeundefinedA visible label; without it the button is icon-only.Unreleased
aria-labelstringlocale “Copy” when icon-onlyNames the idle button; after a copy it reads the locale’s “Copied”.Unreleased
disabledbooleanfalseDisables the button.Unreleased

CopyButton also accepts native button attributes and a ref. Restyle it through the --r-copy-button-* tokens.