Skip to content

Early previewhamur is not on npm yet. The components are being built in the open, follow along on GitHub.

UIComponentsReact

Clipboard

A button that copies a text, such as a link, a command or a code, with an optional field showing it.

Built on the headless Clipboard.See its API in Docs

Rendered by React 19.3.1

clipboard.tsx

Installation

npx shadcn@4.23.0 add https://hamur.dev/r/react/clipboard.json

It also adds utils, button. The app needs its setup first: Installation.

Usage

Give Clipboard the value to copy: a string, or a function called once per press that returns the text or a promise of it. Put a ClipboardLabel, a ClipboardInput (the value, read-only) and a ClipboardTrigger around a ClipboardIndicator inside it; ClipboardValue shows the value as text instead of a field. A press writes the value, the parts take data-copied or data-failed for timeout milliseconds (2000 by default), and a status region inside the Root says "Copied" or "Copy failed". The Trigger's name stays "Copy" and the Label in every status. The writing, the statuses, onStatusChange, labels and disabled come from the headless Clipboard.

import {
  Clipboard,
  ClipboardIndicator,
  ClipboardInput,
  ClipboardLabel,
  ClipboardTrigger,
  ClipboardValue,
} from '@/components/ui/clipboard'

Examples

Default

A share link in a read-only field with a labelled icon button that copies it.

Its preview and code are at the top of the page.

The Input has the Input's box, read-only, and cuts a long value with an ellipsis; focusing it selects the whole value, so a keyboard copy works too. The Trigger is the Button's outline icon, 36 by 36 CSS pixels (WCAG 2.5.8); variant and size change it, for a Trigger with text as well. The Indicator is hidden from assistive technology and shows a copy icon, a check while copied and a cross in the destructive colour while failed; the result is also announced, so it is never told by the icon alone (WCAG 1.4.1, 4.1.3). A browser can refuse the write, for example on a page without a secure context or without the permission: the Trigger then shows the failed state, and the Input still lets the user select and copy the value. This item takes Base UI's component ID as its slug and one name per Base UI part.

API reference

The styled parts pass every prop to the headless part they wrap and add classes. The props, data attributes and keyboard are on Clipboard's API reference.