Components

React component for shadcn/ui

Copy Button

Copy text to the clipboard with visual, sound, and haptic feedback.

Loading…

Copy Button is a React copy-to-clipboard button for shadcn/ui that confirms each copy with an animated icon, a UI sound, and a haptic tap. Use it next to code snippets, install commands, API keys, and share links.

Features

  • Accepts text as a value or a function resolved when the button is clicked.
  • Animates idle, success, and error icons before returning to idle.
  • Plays matching UI sounds and haptics for successful and failed copies.
  • Supports custom state icons and copy outcome callbacks.

Installation

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

Usage

import { CopyButton } from "@/components/copy-button";
<CopyButton size="default" variant="outline" text="npm install tentui">
  Copy command
</CopyButton>

Pass a function when the value should be resolved at click time.

<CopyButton text={() => window.location.href} aria-label="Copy page URL" />

Accessibility

CopyButton preserves the underlying button's keyboard and focus behavior and uses Copy as its default accessible label. Set aria-label to describe a more specific action when needed.

Success and error feedback is visual. Announce the result separately when confirmation needs to be available to assistive technology.

API reference

PropType

FAQ

How do I install the Copy Button with shadcn?

Run npx shadcn@latest add @tentui/copy-button after adding the @tentui registry to components.json (installation guide), or run npx shadcn@latest add https://tentui.com/r/copy-button.json without it. The CLI copies copy-button.tsx, its icon-swap helper, and the use-copy-to-clipboard hook into your project, adds the shadcn button, and installs motion, lucide-react, web-haptics, and @rexa-developer/tiks.

Can I copy a value that's only known at click time?

Yes. Pass a function to text, such as () => window.location.href, and it runs on click; it can also return a promise. Async text is written through a ClipboardItem promise so Safari still treats the copy as part of the click.

Why does the Copy Button show an error instead of copying?

Browsers only expose the Clipboard API in secure contexts, so copying fails on plain HTTP pages other than localhost, and it also fails if a text function throws. The button then shows the error icon and calls onCopyError with the error.

Is the Copy Button accessible?

It is a native button with keyboard and focus behavior intact, and its default accessible name is "Copy". Set aria-label when the button has its own visible text or copies something specific, and announce the result yourself if screen-reader users need it, because the success and error states are visual.

How do I change the icons and style?

Pass idleIcon, doneIcon, and errorIcon to replace the Lucide icons, and any shadcn Button prop such as variant, size ("icon" by default), or className. The icon returns to idle 1.5 seconds after each copy.

Is the Copy Button free to use?

Yes. The Copy Button is free and source-available: the shadcn CLI copies its source into your project, and you can use it in unlimited personal and client projects under the TentUI License. The license doesn't allow redistributing or reselling the component itself.

All buttons and toggles
All guides