Copy Button

A standalone copy-to-clipboard button that copies a string or a lazily computed value on click, showing animated copied and error states. It supports ghost, outline, and solid variants, icon-only or labeled modes, and a floating tooltip for icon-only buttons.

Copy Button
LIVE · running in a sandboxed iframe
Installed with plain shadcn add · no workarounds · theme neutral (none shipped)
See how it was built

Installation

pnpm dlx shadcn@latest add @reactframe/copy-button

Requires @reactframe in the registries of your components.json (shadcn adds official directory namespaces automatically).

Usage

usage.tsx
import { CopyButton } from "@/components/ui/copy-button"export function Example() {  return <CopyButton value="npm install @reactframe/copy-button" label="Copy" />}
  • Adding a copy-to-clipboard action to code blocks, API keys, invite links, or share URLs
  • Needing a compact icon-only copy control with a hover tooltip inside toolbars or table rows
  • Copying a value that must be computed on click, such as a fetched token or generated snippet, via an async value function
  • Showing clear, animated success or failure feedback after a copy attempt

Examples

Icon-only copy button in a toolbar

Omitting label renders an icon-only button with an aria-label and a hover tooltip.

icon-only-copy-button-in-a-toolbar.tsx
import { CopyButton } from "@/components/ui/copy-button"export function ToolbarCopy({ code }: { code: string }) {  return <CopyButton value={code} size="sm" variant="ghost" theme="dark" />}

Solid labeled button with a lazy async value

The value function runs on click, so the text is generated only when needed. Errors from the function or the clipboard trigger the error state.

solid-labeled-button-with-a-lazy-async-value.tsx
import { CopyButton } from "@/components/ui/copy-button"export function InviteCopy({ getInviteUrl }: { getInviteUrl: () => Promise<string> }) {  return (    <CopyButton      value={getInviteUrl}      label="Copy invite link"      copiedLabel="Link copied"      errorLabel="Copy failed"      variant="solid"      size="lg"      theme="light"      onCopy={(v) => console.log("copied", v)}      onError={(e) => console.error(e)}    />  )}

Outline button with a custom accent ring

Outline variant with an accent color applied to the focus ring and a longer reset delay.

outline-button-with-a-custom-accent-ring.tsx
import { CopyButton } from "@/components/ui/copy-button"export function ApiKeyCopy({ apiKey }: { apiKey: string }) {  return (    <CopyButton      value={apiKey}      label="Copy key"      variant="outline"      accentColor="#6366F1"      resetDelay={2400}      className="font-mono"    />  )}

API reference

PropTypeDefaultDescription
valuestring | (() => string | Promise<string>)—The text to copy, or a function (sync or async) that produces it lazily, evaluated on click.
labelstring—Text shown next to the icon. Omit it for an icon-only button.
copiedLabelstring"Copied"Text shown in the copied state.
errorLabelstring"Failed to copy"Text shown in the error state.
variant"ghost" | "outline" | "solid""ghost"Visual style of the button.
size"sm" | "md" | "lg""md"Height, padding, font size, and icon size of the button.
theme"dark" | "light""dark"Color palette used for text, borders, tooltip, and success/error colors.
accentColorstring—CSS color applied to the focus ring (--tw-ring-color). Falls back to the palette text color.
resetDelaynumber1600How long the copied or error state is shown before reverting to idle, in milliseconds.
showTooltipbooleantrueWhether icon-only buttons show a floating tooltip on hover or focus.
onCopy(value: string) => void—Called with the resolved text after a successful copy.
onError(error: unknown) => void—Called when the value function throws or rejects, or when the clipboard write fails.
disabledbooleanfalseDisables the button and prevents copying. The tooltip is hidden while disabled.
classNamestring—Additional Tailwind classes merged onto the button element.

Accessibility

  • Icon-only buttons receive an aria-label that matches the current tooltip text ("Copy", the copied label, or the error label), so screen readers announce the state.
  • Labeled buttons render visible text, which serves as the accessible name; the aria-label is not set in that case.
  • The state icon is decorative and marked aria-hidden="true".
  • The button is a native button element with type="button" and uses the native disabled attribute.
  • Focus styling uses focus-visible:ring-2 with the accentColor applied to the ring, and the tooltip is shown on focus as well as hover.
  • The tooltip uses role="tooltip" and is pointer-events-none so it does not block interaction.
  • Copy is ignored while a copied or error state is active, which prevents repeated triggers during feedback.
  • The copied and error states are conveyed through text changes and icon shape, not color alone, so they remain distinguishable without color perception.

Docs written by anthropic:claude-haiku-5-5 from the registry source.

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "Copy Button" component (reactframe/copy-button) from its shadcn registry.1. Install it with: npx shadcn@latest add @reactframe/copy-button2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: value, label, copiedLabel, errorLabel, variant, size, theme, accentColor, resetDelay, showTooltip, onCopy, onError, disabled, className.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { CopyButton } from "@/components/ui/copy-button"export function Example() {  return <CopyButton value="npm install @reactframe/copy-button" label="Copy" />}```

Files & dependencies

  • registry/new-york/copy-button/copy-button.tsx
dependenciesmotionclsxtailwind-merge

Looks similar, elsewhere