0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…
Clipboard copy and paste via data attributes. Add data-copy, data-copy-target, or data-paste-target to any button for clipboard functionality with visible, screen-reader and failure feedback.
Overview
The data-copy attribute turns a native <button> into a clipboard copy control. There is no wrapper element: put the attribute on the button itself. Its siblings data-copy-target, data-copy-attr and data-paste-target copy from another element, copy an attribute value, or paste into a field.
data-copy — the attribute value is the text to copy.
data-copy-target — a CSS selector for the element to copy from. Inputs and textareas give their current value, and other elements give their textContent.
data-copy-target + data-copy-attr — copies the named attribute of the target instead.
data-paste-target — a CSS selector for the element that receives the clipboard text.
On click, the text goes to the clipboard and the button gets data-state="copied" for 1.5 seconds. A polite announcement is made, and a copy:done event fires from the button. One delegated click listener on the document serves every button, so buttons added later, cloned with cloneNode() or restored from saved markup work without set-up. Disabled buttons (disabled or aria-disabled="true") do nothing.
Element to copy from: .value for <input> / <textarea>, else textContent. If copying fails, this element's text is selected so the user can copy it by hand.
data-copy-attr
string
With data-copy-target: copy the named attribute of the target instead (read at click time). A missing attribute copies nothing.
data-paste-target
selector
Element that receives the clipboard text. Form controls get .value, other elements get .textContent, and an input event is dispatched on the target.
data-label-copied
string
Announcement after a copy. Default "Copied to clipboard". Read from the button or any ancestor, so it can be set once on <html>.
data-label-pasted
string
Announcement after a paste. Default "Pasted from clipboard".
data-label-copy-failed
string
Announcement when copying fails. Default "Copy failed. Text selected, press Ctrl+C to copy" when the source text could be selected, else "Copy failed".
data-label-paste-failed
string
Announcement when reading the clipboard fails. Default "Paste failed".
data-state
(reflected)
"copied", "pasted" or "failed", held for 1.5 seconds. Set by the script. Do not author it.
Events
Events fire from the button and bubble. They are namespaced. Earlier versions dispatched CustomEvents named copy and paste, which reached page-level native ClipboardEvent handlers (analytics, paste sanitizers) without clipboardData and broke them.
Event
Detail
When
copy:done
{ text } ({ payload } from copyRich)
The clipboard write succeeded.
copy:error
{ text, error }
The Clipboard API and the execCommand fallback both failed.
paste:done
{ text, target }
Clipboard text was written into the target.
paste:error
{ target, error }
Reading the clipboard failed (no Clipboard API, permission denied).
Announcements go through one shared, visually hidden live region on <body> (src/lib/announce.js), never inside the button. The button's accessible name stays the same, and repeating a copy is announced again.
Failure is never silent. If navigator.clipboard is missing or refuses (an insecure http: page, denied permission), a hidden-textarea execCommand('copy') is tried. If that fails too, the button gets data-state="failed", the source text (data-copy-target) is selected for a manual copy, the failure is announced, and copy:error fires.
Colour is not the only cue: a mark is added (WCAG 1.4.1), and forced-colors mode gets an outline.
The button stays a native <button>, so it keeps keyboard support and its own name. Icon-only buttons need an aria-label.
Pasting asks for permission in most browsers the first time. A denial sets data-state="failed" and is announced.
Examples
Copy from a target element
Point data-copy-target at the element to copy from. A form field gives its current value.
When the value lives in an attribute rather than in the text, add data-copy-attr next to data-copy-target. Common targets are <output value>, <data value>, <time datetime>, <a href> and any data-*attribute.
For a snippet copy button, lay the code and the button out with a VB layout. Absolute positioning is not needed. An icon-only button needs an aria-label.
copyRich() writes a ClipboardItem that combines text/plain, text/html and a binary Blob (typically image/png). If the browser has no ClipboardItem, or rejects one of the types, it falls back to copying the text with copyText(). <qr-code>'s copy() method uses it.
Multi-format copy
1234567
import { copyRich } from'/src/utils/copy-init.js';const blob = awaitnewPromise((r) => canvas.toBlob(r, 'image/png'));awaitcopyRich({ text: '#3366cc', blob }, {button: myButton,announceMessage: 'Color copied as text and image',});
JavaScript helpers
Components that copy or paste import the same helpers, so they get the same feedback:
Function
Returns
Description
copyText(text, options)
Promise<boolean>
Writes text, falling back to execCommand('copy'), then gives feedback.
copyRich({ text, html, blob }, options)
Promise<boolean>
Multi-format write. If that fails, copies the text only.
pasteFromClipboard(target, options)
Promise<string | null>
Reads the clipboard and writes it into target (pass null to only read). Never throws.
Options:
button — gets data-state and dispatches the events.
announceMessage — success announcement (default: the button's data-label-copied / data-label-pasted, else the English default).
failureMessage — failure announcement.
source — the element whose text is selected when copying fails.
duration — how long data-state is held, in milliseconds (default 1500).
Programmatic copy and paste
1234
import { copyText, pasteFromClipboard } from'/src/utils/copy-init.js';const ok = awaitcopyText(url, { button, source: urlField });const text = awaitpasteFromClipboard(document.querySelector('#dest'), { button });
Related
<code-block> — has its own copy button for code samples.
<qr-code> — copy() copies the encoded text and a PNG.
<color-picker> — its built-in copy button copies the current colour.
<share-wc> — a "Copy link" platform next to the share targets.
data-copy — any button that copies one known string, field or attribute: install commands, API tokens, hex values, share URLs.
A component — when copying is part of a larger widget. Use <code-block> for highlighted code, <qr-code> for images, <color-picker> for colours, <share-wc> for share menus. They use the same helpers, so they give the same feedback.