GooseUI

Components

Copy Button

New

One button for every place that copies text, in three variants: a square icon button, a button with a label, and a small inline icon for the end of a field. The copy icon fades into a check mark and back, the text for screen readers changes with it, and the label stays the same width. Copying goes through useCopyToClipboard, the icons are inline SVG with no icon library. The button never shows a toast: hook onCopy and onCopyError up to your own.

Browser Support

Async clipboard

Copies text to the clipboard.

Your browser:

Preview

Invite code
GOOSE-7F3K-29QD
https://example.com/invite/7f3k29qd

Installation

npx shadcn@latest add @gooseui/copy-button
pnpm dlx shadcn@latest add @gooseui/copy-button
yarn dlx shadcn@latest add @gooseui/copy-button
bunx --bun shadcn@latest add @gooseui/copy-button

Usage

import { CopyButton } from "@/components/ui/copy-button"

export function Token({ value }: { value: string }) {
  return (
    <div className="flex items-center gap-2">
      <code>{value}</code>
      <CopyButton value={value} />
    </div>
  )
}

Examples

Variants and sizes

variant="icon" for tight spots next to code or tokens, variant="label" when the action needs words, variant="inline" inside a field. The last two sizes are sm and md.

<CopyButton value={value} size="sm" />
<CopyButton value={value} />
<CopyButton value={value} variant="label" size="sm" />
<CopyButton value={value} variant="label" />
<CopyButton value={value} variant="inline" />

Toast through a callback

onCopy fires once after a successful copy and onCopyError once after a failed one. Show a toast or send an analytics event there; the component itself knows nothing about toasts.

<CopyButton
  value={inviteUrl}
  variant="label"
  labels={{ copy: "Copy link", copied: "Link copied" }}
  onCopy={() => toast.success("Link copied")}
  onCopyError={() => toast.error("Couldn't copy")}
/>

Props

PropTypeDefaultDescription
valuestring | (() => string)-Text to copy, or a function that returns it at click time
variant"icon" | "label" | "inline""icon"Square icon button, button with a label, or a small icon for a field
size"sm" | "md""md"Button size
timeoutnumber2000How long the check mark stays, in ms
labels{ copy?: string; copied?: string }{ copy: "Copy", copied: "Copied" }Texts for the label and screen readers. For icon and inline they become aria-label
onCopy(value: string) => void-Called after a successful copy
onCopyError(error: Error) => void-Called when copying failed
classNamestring-Additional CSS classes

Features

  • Accessible. The accessible name switches to “Copied”, and a polite live region announces it.
  • Stable width. Both labels share one grid cell, so the button does not jump.
  • Reduced motion. With prefers-reduced-motion the icons swap without a transition.
  • Works over http. useCopyToClipboard falls back to a hidden textarea when the Clipboard API is not available.