Skip to content

Clipboard

A component that allows users to quickly copy text content to their clipboard with visual feedback.

Features

  • ✅ One-click copy to clipboard
  • ✅ Visual feedback with data attributes
  • ✅ Optional input field for displaying content
  • ✅ Accessible ARIA attributes

Installation

js
import Alpine from 'alpinejs'
import clipboard from 'alpine-headless-ui/clipboard'

Alpine.plugin(clipboard)
Alpine.start()

Examples

Basic Clipboard

With Input Field

With Icon Indicator

Custom Timeout

Code Snippet

JavaScript

API Reference

Component Props

PropTypeDefaultDescription
valuestring''The text to copy to clipboard
timeoutnumber3000Duration in milliseconds before resetting copied state

Parts

PartDescription
x-clipboard:labelLabel for the clipboard content
x-clipboard:controlContainer for input and trigger button
x-clipboard:triggerButton that triggers the copy action
x-clipboard:inputInput field displaying the text to be copied
x-clipboard:indicatorOptional indicator shown when content is copied

Data Attributes

AttributeDescription
data-scopeAlways set to clipboard
data-partIdentifies the part (root, label, control, trigger, input, indicator)
data-copiedPresent when text has been copied

Events

EventDetailDescription
copy{ value: string, copied: boolean }Fired when text is successfully copied
copy-error{ error: Error }Fired when clipboard copy fails

Accessing State

You can access the clipboard API using $clipboard:

html
<div x-clipboard>
  <div x-data>
    <p x-text="$clipboard.copied ? 'Copied!' : 'Not copied'"></p>
  </div>
  <button x-clipboard:trigger>
  <!-- Parts -->
</div>

Accessing State

You can access the clipboard API using $clipboard:

html
<div x-clipboard="{ value: 'Hello' }">
  <div x-data>
    <p x-show="$clipboard.copied">Text copied!</p>
    <p x-text="$clipboard.value"></p>
  </div>

  <button x-clipboard:trigger>Copy</button>
</div>

Available properties:

  • value - The text to copy
  • copied - Whether text was recently copied
  • timeout - Timeout duration in ms
  • setValue(value) - Update the text value
  • copy() - Trigger copy action programmatically

Accessibility

Best Practices

  • Use descriptive labels for clipboard triggers
  • Provide visual feedback when content is copied
  • Consider using ARIA live regions for screen reader announcements
  • Ensure keyboard accessibility for trigger buttons

Example with ARIA Live Region

html
<div x-clipboard="{ value: 'Text to copy' }">
  <button x-clipboard:trigger>Copy</button>

  <div role="status" aria-live="polite" aria-atomic="true">
    <span x-show="$clipboard.copied">Copied to clipboard</span>
  </div>
</div>

Browser Support

The clipboard component uses the modern Clipboard API. It requires:

  • HTTPS (or localhost for development)
  • Modern browsers with Clipboard API support
  • User permission for clipboard access (automatic in most browsers)

Fallback

For older browsers, consider providing alternative copy methods or displaying the text for manual copying.

See Also

  • Popover - Show helpful hints or feedback for clipboard buttons
  • Collapsible - Show/hide additional information

Built with Alpine.js and inspired by Zag.js