CopyField and CopyGroup
CopyField is a ready-made, unstyled copy row: a label, a value and a button, with reveal on hover, focus and touch, screen-reader announcements and the no-flicker display status built in. Compose it from these parts:
Try it
Section titled “Try it”Only one "Copied" at a time
Rows inside a <CopyGroup> share a coordinator, so a contact card never shows three green ticks at once.
<CopyGroup>
<CopyField.Root value={name} label="Name">…</CopyField.Root>
<CopyField.Root value={email} label="Email">…</CopyField.Root>
</CopyGroup>
| Part | Renders | Notes |
|---|---|---|
CopyField.Root |
div |
Props: value, label, copyOptions, messages, alwaysVisible, announce, pendingDelayMs, minPendingMs. Forwards ref and all div props |
CopyField.Label |
span |
Defaults to label |
CopyField.Value |
span |
Defaults to the text value; pass children for anything else |
CopyField.Trigger |
button type="button" |
Children can be a node or ({ status, displayStatus, state, revealed, canRetry }) => node |
useCopyField() |
— | Full context, for building your own parts |
Styling hooks on Root and Trigger: data-display-state="idle | copying | copied | error"
(style with this one), data-state (the raw state), and data-revealed while the trigger
should be visible.
Translations
Section titled “Translations”<CopyField.Root label="ईमेल" value={email} messages={{ copied: "कॉपी हो गया", error: (e) => t(`copy.errors.${e.type}`), triggerLabel: (label) => `${label} कॉपी करें`, }}/>Only one “Copied” at a time
Section titled “Only one “Copied” at a time”Wrap a table or list in <CopyGroup>. A new successful copy returns the previously copied
row to idle, because the clipboard no longer holds its value. A failed copy releases nothing.
import { CopyField, CopyGroup } from "react-smart-copy";
<CopyGroup> {rows.map((row) => ( <CopyField.Root key={row.id} value={row.email} label="Email"> <CopyField.Value /> <CopyField.Trigger /> </CopyField.Root> ))}</CopyGroup>;It works for useCopy() too. Opt one field out with copyOptions={{ coordinator: null }}.
To share one group across React roots or with vanilla code, create it yourself:
const coordinator = createCopyCoordinator(), then <CopyGroup coordinator={coordinator}>
and createCopyMachine({ coordinator }).