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>Usage
textis what is copied; the label swaps tocopiedLabelonce it is on the clipboard.- Name what is copied in
labelandcopiedLabel("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>withtype="button"and a focus ring from the component. - The label text is the accessible name and changes to
copiedLabelafter 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