Copy Button
Copy buttons copy text to the clipboard when the user activates them. They provide built-in success and error feedback so users know the copy worked.
Copying requires a secure context.
Copy buttons use the browser's clipboard.writeText() method, which requires a secure context (HTTPS) in most browsers.
Examples
Copying from Other Elements
Set the value attribute to copy a literal string, or point the from attribute at another element's id to copy live content. When both are present, from wins.
By default from copies the target's textContent. Add a modifier to copy an attribute or property instead:
| Syntax | Copies | Example |
|---|---|---|
from="id" |
The element's textContent |
from="my-phone" |
from="id[attr]" |
The named attribute | from="my-link[href]" |
from="id.prop" |
The named property | from="my-input.value" |
Custom Labels
The copy button shows a tooltip on hover and focus, then briefly swaps it to confirm a copy. Set the copy-label, success-label, and error-label attributes to customize the text for each state. copy-label also serves as the button's accessible name.
Custom Icons
Use the copy-icon, success-icon, and error-icon slots to replace the icon shown in each state. <wa-icon> works best, but any image will do.
Custom Trigger
By default the copy button renders an icon-only button. Slot in any clickable element to use as the trigger instead — a Web Awesome button, a native button, or anything else.
Custom triggers get the same feedback with no extra wiring.
They receive the same tooltip and copy feedback as the default trigger; the icon swap is the one piece unique to it. Set tooltip="none" to opt out of the tooltip, and listen for the wa-copy and wa-error events or style the :state(success) and :state(error) custom states for your own feedback.
Disabled
Add the disabled attribute to turn off the copy button.
Handling Errors
A copy fails when value is empty, when from points to an id that doesn't exist, or when the browser rejects the operation. Either way, the button shows its error state and emits the wa-error event. Customize the message with error-label and the icon with the error-icon slot.
Feedback Duration
After a copy, the tooltip briefly shows the success or error label. Set the feedback-duration attribute (in milliseconds) to control how long it stays visible.
Tooltip Mode
The tooltip attribute controls when the built-in tooltip appears, on both the default and custom triggers.
| Value | Behavior |
|---|---|
full |
Shows on hover and focus, and reused for copy feedback |
copy |
Stays silent on hover and focus; appears only to confirm a copy |
none |
Never shown |
Tooltip Placement
The tooltip sits above the trigger by default. Set the tooltip-placement attribute to top, right, bottom, or left to move it.
Customizing
Style the button through its CSS parts — button, copy-icon, success-icon, and error-icon — to match your design.
API
Importing
If you're using the autoloader or a hosted project, components load on demand — no manual import needed. To cherry-pick a component manually, use one of the following snippets.
Import this component directly from the CDN:
import 'https://ka-f.webawesome.com/[email protected]/components/copy-button/copy-button.js';
After installing Web Awesome via npm, import this component:
import '@awesome.me/webawesome/dist/components/copy-button/copy-button.js';
If you're self-hosting Web Awesome, import this component from your server:
import './webawesome/dist/components/copy-button/copy-button.js';
To import this component for React 18 or below, use the following code:
import WaCopyButton from '@awesome.me/webawesome/dist/react/copy-button/index.js';
Slots
Learn more about using slots.
| Name | Description |
|---|---|
| (default) | The trigger element. By default, a copy icon button is rendered so this is optional. If desired, you can slot in a custom element such as <wa-button> or <button>. |
copy-icon
|
The icon to show in the default copy state. Works best with <wa-icon>. |
error-icon
|
The icon to show when a copy error occurs. Works best with <wa-icon>. |
success-icon
|
The icon to show when the content is copied. Works best with <wa-icon>. |
Attributes & Properties
Learn more about attributes and properties.
| Name | Description | Reflects |
|---|---|---|
copyLabelcopy-label |
A custom label to use as the accessible name and tooltip text in the default copy state.
Type
string
Default
''
|
|
disableddisabled |
Disables the copy button.
Type
boolean
Default
false
|
|
errorLabelerror-label |
A custom label to show in the tooltip when a copy error occurs.
Type
string
Default
''
|
|
feedbackDurationfeedback-duration |
The length of time to show feedback before restoring the default trigger.
Type
number
Default
1000
|
|
fromfrom |
An id that references an element in the same document from which data will be copied. If both this and
value are
present, this value will take precedence. By default, the target element's textContent will be copied. To copy an
attribute, append the attribute name wrapped in square brackets, e.g. from="el[value]". To copy a property,
append a dot and the property name, e.g. from="el.value".
Type
string
Default
''
|
|
successLabelsuccess-label |
A custom label to show in the tooltip after copying.
Type
string
Default
''
|
|
tooltiptooltip |
Controls the built-in tooltip.
full (default) shows the tooltip on hover and focus and during copy feedback.
copy keeps the tooltip silent on hover/focus and only shows it briefly to confirm a successful or failed copy.
none disables the tooltip entirely. Applies to both the default and custom triggers.
Type
'full' | 'copy' | 'none'
Default
'full'
|
|
tooltipPlacementtooltip-placement |
The preferred placement of the tooltip.
Type
'top' | 'right' | 'bottom' | 'left'
Default
'top'
|
|
valuevalue |
The text value to copy.
Type
string
Default
''
|
Events
Learn more about events.
| Name | Description |
|---|---|
wa-copy |
Emitted when the data has been copied. |
wa-error |
Emitted when the data could not be copied. |
Custom States
Learn more about custom states.
| Name | Description | CSS selector |
|---|---|---|
error |
Applied when the copy operation fails. |
:state(error)
|
success |
Applied when the copy operation succeeds. |
:state(success)
|
CSS Parts
Learn more about CSS parts.
| Name | Description | CSS selector |
|---|---|---|
button |
The internal <button> element. |
::part(button)
|
copy-icon |
The container that holds the copy icon. |
::part(copy-icon)
|
error-icon |
The container that holds the error icon. |
::part(error-icon)
|
feedback |
The internal <wa-tooltip> element. |
::part(feedback)
|
success-icon |
The container that holds the success icon. |
::part(success-icon)
|
Dependencies
This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.