- Docs
- Components
- Actions
Copy button
Copies a value. The icon turns to a check, "Copied" is announced, and focus stays.
- Import
- @qeetrix/react/experimental
- Status
- ExperimentalSince 0.0.4
- Renders
<button>Server-rendered- Tested
- WCAG 2.2 AAaxe on every pull request
01
Import
// Experimental: from the package's experimental entry, until it's stable.
import { CopyButton } from "@qeetrix/react/experimental";02
Props
And every native attribute of the element it renders.
| Prop | Type | Default | What it does |
|---|---|---|---|
value | string | () => string | Promise<string> | required | The text, or a function that makes it, even asynchronously. |
label | string | "Copy" | What it copies: its name and its tooltip. |
children | ReactNode | — | A visible label: then it's a Button, label first. |
variant | "ghost" | "soft" | "solid" | "ghost" | As Icon button's. |
tone | "brand" | "danger" | "neutral" | "neutral" | Neutral for a utility, as most are. |
size | "sm" | "md" | "lg" | "md" | Height from the density tokens. |
tooltipSide | "top" | "bottom" | "left" | "right" | "inline-start" | "inline-end" | "top" | The side its tooltip opens on. |
onCopied | (value: string) => void | — | Called once the text is on the clipboard. |
onCopyError | (error: unknown) => void | — | Called when copying fails, e.g. with no clipboard access. |
resetAfter | number | 2000 | How long "Copied" shows, in milliseconds. |
messages | Partial<{ copy, copied, failed }> | — | This button's words, over QeetrixProvider's. |
disabled | boolean | false | Sets aria-disabled; it stays focusable. |
03
Parts
Each part carries a data-slot, for tests and placement.
| data-slot | Element | What it is |
|---|---|---|
icon-button | <button> | The root, icon-only. data-state: idle, copied or failed. button with a label. |
button-icon | <svg> | Copy, check or alert. |
— | <span role="status"> | Visually hidden: announces "Copied" or "Couldn't copy". |
04
A value made when it's copied
A function, even async: fetched or made at the moment of the copy.
<CopyButton
value={async () => (await createInvite({ role: "member" })).url}
label="Copy an invite link"
onCopied={() => track("invite_copied")}
>
Copy invite link
</CopyButton>05
In another language
Its words come from QeetrixProvider; messages overrides one button's.
import { QeetrixProvider } from "@qeetrix/react";
import { CopyButton } from "@qeetrix/react/experimental";
import { hi } from "@qeetrix/react/locales/hi";
<QeetrixProvider locale="hi-IN" messages={hi}>
<CopyButton value="usr_01J8K2M4PQ" />
</QeetrixProvider>