Skip to content

Action

CopyButton

Copies a value to the clipboard with visual feedback.

import { CopyButton } from '@var-ui/react';

CopyButton

Copies a value to the clipboard with visual feedback. Defaults to an icon + label button; pass a render-prop children function when you need custom chrome.

CopyButton from @var-ui/astro renders the same button and classes with the same labels, enhanced by a small clipboard script. It takes the same props except the render-prop children — custom chrome needs the React component.

Examples

Default

Provide the value written to the clipboard. Labels default to “Copy” / “Copied” / “Error”.

Demo.tsxTSX
import { CopyButton } from '@var-ui/react';

<CopyButton value="npm i @var-ui/react" />

Custom labels

Override copyLabel and copiedLabel (and optionally errorLabel) for context-specific copy.

Demo.tsxTSX
import { CopyButton } from '@var-ui/react';

<CopyButton
  value="npm i @var-ui/react"
  copyLabel="Copy install command"
  copiedLabel="Copied to clipboard"
/>

Reacting to a copy

onCopied(value) runs after a successful write and onCopyError(error, value) when the write fails (permission denied, an insecure context), so you can show your own toast or log the failure. They have their own names because the native onCopy is the element’s clipboard event. useCopy takes the same two options.

TSX
<CopyButton
  value={url}
  onCopied={() => toast.success('Link copied')}
  onCopyError={() => toast.error('Copy failed')}
/>

In Astro, listen for the bubbling var-ui:copied and var-ui:copy-error events (COPIED_EVENT, COPY_ERROR_EVENT from @var-ui/astro); event.detail.value is the text.

Props

Accessibility

The default control is a native button whose aria-label tracks the current state label (copyLabel, copiedLabel, or errorLabel). After a successful copy, the label updates so assistive tech hears the confirmation. Prefer clear verbs over icon-only copy affordances unless you supply an explicit render-prop with its own accessible name.