Unkey
ComponentsButtons

CopyButton

The CopyButton component provides a simple way to copy text to the clipboard with visual feedback. It extends the base Button component and includes built-in copy functionality with proper accessibility support.

Features

  • Visual feedback with icon change on copy (TaskUnchecked → TaskChecked)
  • Accessible with screen reader support and ARIA labels
  • Automatic reset after 2 seconds
  • Extends ButtonProps for full button customization
  • Analytics support with optional src prop
  • Prevents event propagation to parent elements
  • TypeScript support with proper typing

Props

PropTypeDefaultDescription
valuestringRequiredThe text content to be copied to clipboard
srcstring?undefinedOptional source identifier for analytics tracking
variantButtonVariant"outline"Button variant (outline, ghost, primary, etc.)
classNamestring?undefinedAdditional CSS classes to apply to the button

The component also accepts all standard Button props.

Usage

Basic usage:
uk_1234567890abcdef
Different variants:
With custom styling:

Behavior

  1. Click Handling: On click, the button copies the provided text to the clipboard
  2. Visual Feedback: The icon changes from TaskUnchecked to TaskChecked state
  3. Auto Reset: After 2 seconds, the icon reverts to its original state
  4. Event Prevention: Parent click events are prevented from triggering (e.stopPropagation)
  5. Analytics: If provided, the src prop is passed to analytics for tracking

Accessibility

The component includes comprehensive accessibility features:

  • ARIA Label: aria-label="Copy to clipboard" for screen readers
  • Screen Reader Text: Hidden "Copy" text for additional context
  • Title Attribute: title="Copy to clipboard" for tooltip on hover
  • Focus Management: Proper focus states with focus:ring-0 focus:border-grayA-6
  • Keyboard Support: Full keyboard navigation support inherited from Button component

Styling

The component includes default styling:

  • Fixed dimensions: w-6 h-6 (24x24px)
  • Focus states: focus:ring-0 focus:border-grayA-6
  • Icons: Full-size icons with w-full h-full
  • Default variant: outline for subtle appearance

Best Practices

  • Use the src prop for analytics tracking when copying sensitive data
  • Provide meaningful value content for better user experience
  • Consider the button's context when choosing variants
  • Ensure sufficient contrast for the button in your design
  • Test with screen readers to verify accessibility

On this page