<wa-copy-button>
Copies text data to the clipboard when the user clicks the trigger.
<wa-copy-button value="Web Awesome rocks!"></wa-copy-button>
Copy Buttons display feedback in a tooltip. You can customize the labels using the copy-label
,
success-label
, and error-label
attributes.
<wa-copy-button value="Custom labels are easy" copy-label="Click to copy" success-label="You did it!" error-label="Whoops, your browser doesn't support this!" ></wa-copy-button>
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.
<wa-copy-button value="Copied from a custom button"> <wa-icon slot="copy-icon" name="clipboard" variant="regular"></wa-icon> <wa-icon slot="success-icon" name="thumbs-up" variant="solid"></wa-icon> <wa-icon slot="error-icon" name="xmark" variant="solid"></wa-icon> </wa-copy-button>
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.
<!-- Copies the span's textContent --> <span id="my-phone">+1 (234) 456-7890</span> <wa-copy-button from="my-phone"></wa-copy-button> <br><br> <!-- Copies the input's "value" property --> <wa-input id="my-input" type="text" value="User input" style="display: inline-block; max-width: 300px;"></wa-input> <wa-copy-button from="my-input.value"></wa-copy-button> <br><br> <!-- Copies the link's "href" attribute --> <a id="my-link" href="https://shoelace.style/">Web Awesome Website</a> <wa-copy-button from="my-link[href]"></wa-copy-button>
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.
<wa-copy-button from="i-do-not-exist"></wa-copy-button>
Copy buttons can be disabled by adding the disabled
attribute.
<wa-copy-button value="You can't copy me" disabled></wa-copy-button>
A success indicator is briefly shown after copying. You can customize the length of time the indicator is
shown using the feedback-duration
attribute.
<wa-copy-button value="Web Awesome rocks!" feedback-duration="250"></wa-copy-button>
You can customize the button to your liking with CSS.
<wa-copy-button value="I'm so stylish" class="custom-styles"> <wa-icon slot="copy-icon" name="clipboard"></wa-icon> <wa-icon slot="success-icon" name="thumbs-up"></wa-icon> <wa-icon slot="error-icon" name="thumbs-down"></wa-icon> </wa-copy-button> <style> .custom-styles { --success-color: white; --error-color: white; color: white; } .custom-styles::part(button) { background-color: #ff1493; border: solid 2px #ff7ac1; border-right-color: #ad005c; border-bottom-color: #ad005c; border-radius: 6px; transition: all var(--wa-transition-slow) var(--wa-transition-easing); } .custom-styles::part(button):hover { transform: scale(1.05); } .custom-styles::part(button):active { transform: translateY(1px); } .custom-styles::part(button):focus-visible { outline: dashed 2px deeppink; outline-offset: 4px; } </style>
Name | Description |
---|---|
copy-icon
|
The icon to show in the default copy state. Works best with <wa-icon> .
|
success-icon
|
The icon to show when the content is copied. Works best with <wa-icon> .
|
error-icon
|
The icon to show when a copy error occurs. Works best with <wa-icon> .
|
Name | Description | Reflects | |
---|---|---|---|
value value
|
The text value to copy.
Type
string
Default
''
|
||
from from
|
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
''
|
||
disabled disabled
|
Disables the copy button.
Type
boolean
Default
false
|
|
|
copyLabel copy-label
|
A custom label to show in the tooltip.
Type
string
Default
''
|
||
successLabel success-label
|
A custom label to show in the tooltip after copying.
Type
string
Default
''
|
||
errorLabel error-label
|
A custom label to show in the tooltip when a copy error occurs.
Type
string
Default
''
|
||
feedbackDuration feedback-duration
|
The length of time to show feedback before restoring the default trigger.
Type
number
Default
1000
|
||
tooltipPlacement tooltip-placement
|
The preferred placement of the tooltip.
Type
'top' | 'right' | 'bottom' | 'left'
Default
'top'
|
||
hoist hoist
|
Enable this option to prevent the tooltip from being clipped when the component is placed inside a
container with
overflow: auto|hidden|scroll . Hoisting uses a fixed positioning strategy
that works in many, but not all, scenarios.
Type
boolean
Default
false
|
Name | Description |
---|---|
wa-copy |
Emitted when the data has been copied. |
wa-error |
Emitted when the data could not be copied. |
Name | Description |
---|---|
--background-color |
The color of the button's background.
|
--background-color-hover |
The color of the button's background on hover.
|
--success-color |
The color to use for success feedback.
|
--error-color |
The color to use for error feedback.
|
Name | Description |
---|---|
button |
The internal <button> element. |
copy-icon |
The container that holds the copy icon. |
success-icon |
The container that holds the success icon. |
error-icon |
The container that holds the error icon. |
tooltip__base |
The tooltip's exported base part. |
tooltip__base__popup |
The tooltip's exported popup part. |
tooltip__base__arrow |
The tooltip's exported arrow part. |
tooltip__body |
The tooltip's exported body part. |
This component automatically imports the following elements. Subdependencies, if any exist, will also be included in this list.
The autoloader is the recommended way to import components. If you prefer to do it manually, use one of the following code snippets.
To manually import this component from the CDN, use the following code.
import 'https://early.webawesome.com/webawesome@3.0.0-alpha.4/dist/components/copy-button/copy-button.js';