Copy button
A button that copies a value, says Copied, and keeps its width.
Demo
Usage
import { CopyButton } from '@tuttn/slant'
// The copy icon and "Copy", then a check and "Copied" for 1.6 seconds.
<CopyButton value={token} />
// Icon only: the label names it. Any Button variant, tone and size.
<CopyButton value={run.id} iconOnly label="Copy run ID" size="sm" variant="ghost" />
// Read the text at the moment of the press, and hear about each copy.
<CopyButton getValue={() => editor.getValue()} onCopied={() => track('copy')}>Copy prompt</CopyButton>
// Your own words for both states; the button is as wide as the wider.
<CopyButton value={url} copiedChildren="Link copied">Copy link</CopyButton>
// Where the clipboard refuses, select the text that shows the value instead,
// so the reader can copy it themselves.
const field = useRef<HTMLInputElement>(null)
<Input ref={field} readOnly value={token} aria-label="Token" />
<CopyButton value={token} fallback={field} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | — | The text to copy. Give this or `getValue`: one of the two is required. |
getValue | () => string | — | Reads the text to copy at the moment of the press, for a value that is costly to keep in props (an editor's content). Give this or `value`: one of the two is required. It wins over `value`. |
children | ReactNode | 'Copy' | The button's text, after the copy icon. From the root's `messages.copy`. |
copiedChildren | ReactNode | 'Copied' | The text shown for a moment after a copy, in the same space: the button keeps the width of the wider of the two. From the root's `messages.copied`. |
iconOnly | boolean | false | Shows only the icon, in a square button. Give it a `label`, such as "Copy run ID". |
label | string | — | The accessible name. With `iconOnly` it names the button, "Copy" (the root's `messages.copy`) by default. With visible text the text names it, and a `label` should start with that text ("Copy" for "Copy run ID"), as voice control finds the button by what it shows. While the button says Copied, its name is the copied text (or `messages.copied`). |
fallback | RefObject<HTMLElement | null> | — | An element whose text is selected when the clipboard refuses the copy, so the reader can copy it themselves: the input or code that shows the value. Where the page has no clipboard at all, the button stays pressable with one, and a press selects its text. |
onCopied | (text: string) => void | — | Called with the text after it reaches the clipboard. |
onClick | (event: MouseEvent<HTMLButtonElement>) => void | — | Called on every press before the copy; preventing its default skips the copy. |
variant | 'primary' | 'secondary' | 'ghost' | 'secondary', or the ButtonGroup's | Emphasis: a solid fill in the tone's colour, a raised surface, or no background. |
tone | Tone | — | Semantic colour. `primary` fills with it, so `tone="danger" variant="primary"` is a destructive button; `secondary` and `ghost` take it as their text and border colour. With `accent` and `neutral` they keep their usual look. |
size | 'sm' | 'md' | 'lg' | 'md', or the ButtonGroup's | Height on the control scale: 28, 34 or 40px at regular density. `lg` also sets larger text and padding. |
loading | boolean | — | Shows a small Spinner before the label, in the button's text colour, and sets aria-busy and aria-disabled. The button keeps focus, so a screen reader stays on it, but clicks are ignored until loading ends. |
fullWidth | boolean | — | Stretches the button to the width of its container. |
…rest | ButtonProps | — | Every Button prop but asChild and icon (variant, tone, size, loading, fullWidth, disabled) and every button attribute. They, the ref, className and style reach the <button>. |
Keyboard
| Tab | moves to the button |
| Enter / Space | copies the value; focus stays on the button |
Accessibility
- A native
<button type="button">. With text, the text on show is its name: “Copy”, then “Copied”. Both labels are in the button at once, the hidden onevisibility: hiddenandaria-hidden, so the name never holds both. - Icon only,
labelnames it (“Copy”,messages.copy, by default), and while it shows the check its name is “Copied” (messages.copied), so the name always matches what is on show. - A copy is announced through the root’s polite announcer: “Copied”, or, when the clipboard refuses, that copying failed, and with a
fallbackthat the text is selected instead. - Where the page has no clipboard (served over plain http, say), a tooltip, which is also its description, says so. Without a
fallbackthe button isaria-disabled, keeps its focus and ignores presses, and the tooltip says “Copying isn’t available here” (messages.copyUnavailable). With one it stays pressable, a press selects the fallback’s text and announces that, and the tooltip says so (messages.copyUnavailableSelect). - The check and its success colour are never the only sign: the name changes too, and with text, the text. On a primary fill the check keeps the button’s text colour.
Value or getValue
Give one of the two. value is the text itself; if it changes while the button says Copied, the button goes back to Copy, so it never claims to hold what it copied a moment ago. getValue is called on each press, for text that is costly to keep in props or lives in an editor, and wins over value. onClick runs first, and preventing its default skips the copy.
Keeping its width
Both labels share one grid cell, so the button is always as wide as the wider of the two and nothing beside it moves when it says Copied. The text starts at the icon, so the shorter label leaves its space at the end. Copied lasts 1.6 seconds, and data-copied is present meanwhile for your own styles.
When copying fails
A refused copy (a denied permission) changes nothing on the button. With fallback, a ref to the element that shows the value, that element's text is selected (an input's or textarea's value, or any element's contents) and the announcement says so. A password input can't be copied from, so Secret input passes its input as the fallback only while the value shows.
No clipboard at all
On a page served without HTTPS the browser has no clipboard, which the button learns once it mounts (on the server it assumes one). Without a fallback it then can't do anything: it is aria-disabled, and its tooltip and description say copying isn't available here. With a fallback it stays pressable: a press runs onClick, selects the fallback's text, announces that the text is selected for the reader to copy, and the tooltip says so up front.