Enhancement
CopyToClipboard
Copy text to the clipboard from a button
- Import
@parallelogram-js/core/components/CopyToClipboard- Selector
[data-copytoclipboard]
Playground
Booking reference
Your reference is HB-2026-0417
<p>Your reference is <code id="booking-reference">HB-2026-0417</code></p>
<button type="button" data-copytoclipboard data-copytoclipboard-target="#booking-reference">
<span data-copytoclipboard-label>Copy reference</span>
</button>
Use the example to see the events it sends.
Your reference is HB-2026-0417
About
Copies the value or text of the target element, the literal text attribute, or the button's own text. The button stays enabled and focused, the result is announced to screen readers, and data-copytoclipboard-state says whether it worked. When the button contains a [data-copytoclipboard-label] element, only its text is swapped for the message.
Usage
Register the component and the framework loads it the first time a page contains [data-copytoclipboard].
import { Parallelogram } from '@parallelogram-js/core';
const app = Parallelogram.create();
app.components.add('[data-copytoclipboard]', () => import('@parallelogram-js/core/components/CopyToClipboard'));
app.run();
The framework is optional. CopyToClipboard.enhanceAll() mounts the component on every matching element by itself, for a page that wants one enhancement rather than a framework:
import CopyToClipboard from '@parallelogram-js/core/components/CopyToClipboard';
CopyToClipboard.enhanceAll();
It takes a selector and the component's options, and returns the instance it mounted them with. What it leaves out is what the framework adds: loading a component only on the pages that use it, mounting again after a page swap, and ordering with dependsOn.
Attributes
| Name | Type | Default | Description |
|---|---|---|---|
data-copytoclipboard | flag | none | Marks the button On a button. |
data-copytoclipboard-target | selector | none | The element whose value or text is copied |
data-copytoclipboard-text | string | none | Text to copy instead of a target |
data-copytoclipboard-success-message | string | Copied! | Shown and announced after copying For every instance, CopyToClipboard.defaults.successMessage. |
data-copytoclipboard-error-message | string | Copy failed | Shown and announced when copying fails For every instance, CopyToClipboard.defaults.errorMessage. |
data-copytoclipboard-success-duration | number | 2000 | Milliseconds before the label returns For every instance, CopyToClipboard.defaults.successDuration. |
data-copytoclipboard-success-class | string | copy-success | Class on the button after copying For every instance, CopyToClipboard.defaults.successClass. |
data-copytoclipboard-error-class | string | copy-error | Class on the button when copying fails For every instance, CopyToClipboard.defaults.errorClass. |
data-copytoclipboard-label | flag | none | The text to swap for the message On an element inside the button. |
data-copytoclipboard-stateset by the component | copied | failed | none | The result of the last copy, until the label returns |
Events
| Name | Detail | Description |
|---|---|---|
copy-to-clipboard:mounted | { element: HTMLElement; target: { type: 'text'; content: string } | { type: 'element'; element: Element } } | A button was set up Event bus |
copy-to-clipboard:success | { element: HTMLElement; text: string } | The text was copied Event bus |
copy-to-clipboard:error | { element: HTMLElement; error: string } | Copying failed Event bus |