Copy Button

<wa-copy-button> Stable Since 3.6

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.

Copy buttons use the browser's clipboard.writeText() method, which requires a secure context (HTTPS) in most browsers.

Examples

Link to This Section

Custom Labels

Link to This Section

The default copy button shows a tooltip on hover and focus, and the tooltip text changes briefly to confirm a successful or failed copy. You can customize these labels using the copy-label, success-label, and error-label attributes. The copy-label is also used as the button's accessible name.

Custom Icons

Link to This Section

Use the copy-icon, success-icon, and error-icon slots to customize the icons that get displayed for each state. You can use <wa-icon> or your own images.

Custom Trigger

Link to This Section

By default, the copy button renders an icon-only button. You can slot in any element to use as a custom trigger instead. This works with Web Awesome buttons, native buttons, or any clickable element.

Copy to Clipboard

You can also use a native button as the trigger.

Custom triggers automatically receive the same tooltip and copy feedback as the default trigger — no extra wiring required. The icon swap is the only piece that's specific to the default trigger. Use without-tooltip to opt out of the tooltip, and use the wa-copy and wa-error events or the :state(success) and :state(error) CSS custom states for additional feedback.

Copying Values From Other Elements

Link to This Section

Normally, the data that gets copied will come from the component's value attribute, but you can copy data from any element within the same document by providing its id to the from attribute.

When using the from attribute, the element's textContent will be copied by default. Passing an attribute or property modifier will let you copy data from one of the element's attributes or properties instead.

To copy data from an attribute, use from="id[attr]" where id is the id of the target element and attr is the name of the attribute you'd like to copy. To copy data from a property, use from="id.prop" where id is the id of the target element and prop is the name of the property you'd like to copy.



Handling Errors

Link to This Section

A copy error will occur if the value is an empty string, if the from attribute points to an id that doesn't exist, or if the browser rejects the operation for any reason. When this happens, the wa-error event will be emitted.

This example demonstrates what happens when a copy error occurs. You can customize the error label and icon using the error-label attribute and the error-icon slot, respectively.

Disabled

Link to This Section

Copy buttons can be disabled by adding the disabled attribute.

Changing Feedback Duration

Link to This Section

After copying, the tooltip briefly displays a success or error label. Use the feedback-duration attribute to control how long it stays visible.

Tooltip Modes

Link to This Section

The tooltip attribute controls when the built-in tooltip appears. It applies to both the default trigger and custom triggers.

Full Copy None

Changing Tooltip Placement

Link to This Section

The tooltip is shown above the trigger by default. Use the tooltip-placement attribute to position it on the top, right, bottom, or left.

The same attribute applies to custom triggers.

Above Right Below Left

Custom Styles

Link to This Section

You can customize the button to your liking with CSS.

Importing

Link to This Section

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.

CDN npm Self-Hosted React

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

Link to This Section

Learn more about using slots.

Attributes & Properties

Link to This Section

Learn more about attributes and properties.

Events

Link to This Section

Learn more about events.

Custom States

Link to This Section

Learn more about custom states.

CSS parts

Link to This Section

Learn more about CSS parts.

Dependencies

Link to This Section

This component automatically imports the following elements. Sub-dependencies, if any exist, will also be included in this list.

Need a hand? Report a bug Ask for help
    No results