Copy Button
Every feature it needs is Baseline widely available.
Works in Chrome 125+, Chrome Android 125+, Edge 125+, Firefox 126+, Firefox for Android 126+, Safari 17.4+, Safari on iOS 17.4+.
Why these versions:
- :state() needs
Chrome 125+,Chrome Android 125+,Edge 125+,Firefox 126+,Firefox for Android 126+,Safari 17.4+andSafari on iOS 17.4+.
Partially supported:
- Keyboard events (repeat): Chrome and Chrome Android ship it partially before 137.
Chrome before 137 misses a repeat only while several keys are held.
sv-copy-button copies text to the clipboard and shows for a short time whether the copy worked.
Usage
Give the text in value:
<sv-copy-button value="npm install @staticview/ui"></sv-copy-button>Or point from at the element that holds the text, read again at each click:
<input id="share-link" value="https://example.com/invite/4f9a" readonly /><sv-copy-button from="share-link"></sv-copy-button>
<a id="docs-link" href="https://example.com/docs">Docs</a><sv-copy-button from="docs-link[href]"></sv-copy-button>Since the clipboard works only on pages served over HTTPS or from localhost, every copy elsewhere fires copyfailed.
To copy from a button of your own, call copy() in its click handler.
Icons
Each icon slot takes your own icon or text, and only the slot of the current state shows:
<sv-copy-button value="ABC-123" copy-label=""> <span slot="copy-icon">Copy code</span> <span slot="success-icon">Copied</span> <span slot="error-icon">Not copied</span></sv-copy-button>Tooltip
The element shows copy-label as a native tooltip through its own title, and swaps in success-label or error-label while the result shows. A tooltip that is already open keeps its old text until the pointer enters again.
A title you set is never overwritten. Set an empty one to turn the tooltip off, like for an sv-tooltip to show alone:
<sv-copy-button value="ABC-123" title="" data-sv-tooltip="copy-tooltip"></sv-copy-button>
<sv-tooltip id="copy-tooltip">Copy the code</sv-tooltip>Removing your title brings the built-in tooltip back on the next label change or copy.
A clone of the element, or markup copied from the page, keeps the written title as your own. To keep its tooltip live, remove the title before you clone the element, or leave it out of the copied markup.
Accessibility
The element itself is the button of the WAI-ARIA button pattern, with Enter or Space to copy. An aria-label or aria-labelledby on the element wins over copy-label.
Since only the visible icon slot names a button with copy-label="", give each slot its own text, like in the example above.
The name stays the same after a copy, while a status region announces success-label or error-label.
Setting aria-disabled="true" blocks the button like disabled does.
API References
Methods
copy()
Copies the text and shows the result, even while disabled is set. Browsers allow it only right after a user action, like a click, and reject a call that comes later, like after a fetch.
Properties
value
The text to copy. from wins when it is set.
from
The id of the element to copy from, in the same document or shadow root. It copies the value of an <input> or <textarea>, and the textContent of any other element. id[name] copies an attribute, like link[href], and id.name copies a property, like field.value. It cannot name an id that contains . or [.
feedbackDuration
How long the copied or failed state lasts, in milliseconds.
disabled
Whether the button is disabled. It stays focusable.
Attributes
"copy-label"
The accessible name and the tooltip of the button. An empty label lets the slotted content name the button.
"success-label"
The text announced and shown in the tooltip after a copy.
"error-label"
The text announced and shown in the tooltip after a failed copy.
"value"
The text to copy. from wins when it is set.
"from"
The id of the element to copy from, in the same document or shadow root. It copies the value of an <input> or <textarea>, and the textContent of any other element. id[name] copies an attribute, like link[href], and id.name copies a property, like field.value. It cannot name an id that contains . or [.
"feedback-duration"
How long the copied or failed state lasts, in milliseconds.
"disabled"
Whether the button is disabled. It stays focusable.
Events
copied
Fired after the text is copied.
copyfailed
Fired when the copy fails, like when the browser blocks the clipboard or from finds nothing.
Slots
copy-icon
The icon shown at rest.
success-icon
The icon shown after a copy.
error-icon
The icon shown after a failed copy.
CSS Properties
--sv-copy-button-copied-clr
Text and icon color after a copy.
--sv-copy-button-failed-clr
Text and icon color after a failed copy.
--sv-copy-button-bg
Background color of the button.
--sv-copy-button-hover-bg
Background color of the button under a pointer.
--sv-copy-button-bdr-clr
Border color of the button.
--sv-copy-button-bdr-sz
Border width of the button.
--sv-copy-button-bdr-rad
Border radius of the button.
--sv-copy-button-icon-sz
Size of the default icons.
--sv-copy-button-dur
Duration of the icon swap.
CSS Parts
::part(copy-icon)
The default copy icon.
::part(success-icon)
The default success icon.
::part(error-icon)
The default error icon.
CSS States
:state(--copied)
Set for feedbackDuration after a copy.
:state(--failed)
Set for feedbackDuration after a failed copy.