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>

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

NameTypeDefaultDescription
data-copytoclipboardflagnoneMarks the button On a button.
data-copytoclipboard-targetselectornoneThe element whose value or text is copied
data-copytoclipboard-textstringnoneText to copy instead of a target
data-copytoclipboard-success-messagestringCopied!Shown and announced after copying For every instance, CopyToClipboard.defaults.successMessage.
data-copytoclipboard-error-messagestringCopy failedShown and announced when copying fails For every instance, CopyToClipboard.defaults.errorMessage.
data-copytoclipboard-success-durationnumber2000Milliseconds before the label returns For every instance, CopyToClipboard.defaults.successDuration.
data-copytoclipboard-success-classstringcopy-successClass on the button after copying For every instance, CopyToClipboard.defaults.successClass.
data-copytoclipboard-error-classstringcopy-errorClass on the button when copying fails For every instance, CopyToClipboard.defaults.errorClass.
data-copytoclipboard-labelflagnoneThe text to swap for the message On an element inside the button.
data-copytoclipboard-stateset by the componentcopied | failednoneThe result of the last copy, until the label returns

Events

NameDetailDescription
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