The copy state for a React control: run() copies (or shares) and keeps the outcome and its message for as long as the message should show. A repeat restarts it. Never throws.
copyText
(text: string): Promise<boolean>
Writes text to the clipboard. Never throws, synchronously or not: a missing clipboard (an insecure page) or a refused write is false. Sites are served over HTTPS, so there is no execCommand fallback (the guide keeps that for pages that cannot control their protocol).
Shares a link with the system share sheet where there is one, and copies it where there is not. A share the person cancels (AbortError) is not a failure and says nothing.
textOf
(target: Element): string
The text a target holds: a form control's value, a link's address, or the rendered text (innerText keeps line breaks that textContent loses).
Shows an outcome on a button and its bubble (the next .cap-copy-tip in its .cap-copy), then clears both. A repeat press restarts the message rather than stacking timers. The button is never disabled, so focus stays and a failed copy can be retried at once.
Runs one command from a button on its target: --copy copies the target's text, --share shares the target link's address (or the page's) and copies it where sharing is missing.
enhance
(root?: ParentNode): () => void
Attaches to every copy button under root. Where Invoker Commands exist the browser resolves commandfor and sends a command event (it does not bubble, so it is caught in the capture phase); elsewhere a click listener resolves commandfor the same way. Only one path is registered, so a press never copies twice. Returns a function that detaches.
Parts and data attributes
Class
Is
.cap-copy
the root
.cap-copy-button
a part
.cap-copy-done
a part
.cap-copy-icon
a part
.cap-copy-tip
a part
.cap-copyable
its own class
Attribute
Values
data-state
done
State
Read from
:empty
the platform
:focus-visible
the platform
:focus-within
the platform
:hover
the platform
Accessibility
Key
Does
Tab
Moves to the button; in a repeated block, also reveals it
Enter, Space
Copies, or opens the share sheet
An icon-only button is named for what it copies (aria-label="Copy API key"); a button with visible text is named by that text.
At least var(--target) in each direction, and allowed to grow.
In a repeated block the button is hidden with opacity only where hover exists, and shows on hover, on focus within the block and while its message shows. On touch it stays visible, in flow beside the block. It is never hidden, inert or aria-hidden.