CopyButton

Copies a string to the clipboard.

Overview

A value the page already shows that a reader will paste elsewhere.

Clipboard

Do
import { CopyButton } from "@hackcove/ui";

<CopyButton text="hc_live_9f2c41" label="Copy token" copiedLabel="Token copied" />
CopyButton writes text to the clipboard and swaps its label to copiedLabel.
Don't
import { Button } from "@hackcove/ui";

<Button
  variant="ghost"
  size="sm"
  onClick={() => {
    void navigator.clipboard.writeText("hc_live_9f2c41");
  }}
>
  Copy token
</Button>
A hand-rolled copy Button gives no feedback and does not survive a refused clipboard.

Usage

  • text is what is copied; the label swaps to copiedLabel once it is on the clipboard.
  • Name what is copied in label and copiedLabel ("Copy token", "Token copied") when the page has more than one.
  • A command or token in its own box is a CopyLine, which carries this button.
  • Do not write the clipboard call by hand in a Button.

Accessibility

  • A native <button> with type="button" and a focus ring from the component.
  • The label text is the accessible name and changes to copiedLabel after the copy.
  • A refused or missing clipboard leaves the label unchanged; nothing is announced.

API

CopyButton

Writes text to the clipboard and swaps the label to copiedLabel. Defaults to type="button". A refused or missing clipboard leaves the label on label. Every other button prop, disabled included, goes on the <button>.


copiedLabel
Type
string
Default
copied

label
Type
string
Default
copy

text required
Type
string