@k8ordo/ui

CopyButton

A button that copies text to the clipboard and shows that it did.

Import

ts
import { CopyButton } from '@k8ordo/ui';

Usage

When pressed, its icon turns into a check for two seconds, and a screen reader announces “Copied” through a live region beside it. If the write fails, the icon becomes an error icon. The button keeps its name.

tsx
<CopyButton value="pnpm add @k8ordo/ui" label="Copy command" size="sm" />

Icon only

With iconOnly it becomes a transparent IconButton. label becomes its tooltip and accessible name.

tsx
<CopyButton value={code} label="Copy code" iconOnly size="sm" />
<CopyButton value={code} label="Copy code" iconOnly />
<CopyButton value={code} label="Copy code" iconOnly size="lg" />

Text built on click

value can also be a function, and it may return a Promise. It is called on the click, and the promise goes to a ClipboardItem as it is. The write starts inside the click even when the text comes later, so it works in Safari, which refuses a write that begins after an await.

tsx
'use client';

<CopyButton label="Copy link" value={() => window.location.href} />

// A promise is fine too: the write still starts inside the click
<CopyButton
  label="Copy as Markdown"
  value={async () => (await fetch(`/blog/${slug}.md`)).text()}
/>

Props

value
Type: string|(() => string | Promise<string>)
Default: -
disabled?
Type: boolean
Default: false
iconOnly?
Type: boolean
Default: false
label?
Type: string
Default: -
size?
Type: 'sm'|'md'|'lg'
Default: 'md'
PropTypeDefault
valuestring|(() => string | Promise<string>)-
disabled?booleanfalse
iconOnly?booleanfalse
label?string-
size?'sm'|'md'|'lg''md'
k8ordo

React libraries that use Baseline features without holding back.