Copy Button
Copies text data to the clipboard when the user clicks the trigger.
<pc-copy-button value="Placer Toolkit rocks!"></pc-copy-button> Copy buttons use the browser’s clipboard.writeText() method, which requires a secure context (HTTPS) in most browsers.
Demos
Custom labels
Copy buttons display feedback in a tooltip. You can customise the labels using the copy-label, success-label and error-label attributes.
Custom icons
Use the copy-icon, success-icon and error-icon slots to customise the icons that get displayed for each state. You can use <pc-icon> or your own images.
Copying values from other elements
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[attribute]" where id is the id of the target element and attribute is the name of the attribute you’d like to copy. To copy data from a property, use from="id.property" where id is the id of the target element and property is the name of the property you’d like to copy.
Handling errors
A copy error will occur if the value is an empty string, if the from attribute points to a non‐existent id or if the browser rejects the operation for any reason. When this happens, the pc-error event will be emitted.
This example demonstrates what happens when a copy error occurs. You can customise the error label and icon using the error-label attribute and the error-icon slot, respectively.
Disabled
Copy buttons can be disabled by adding the disabled attributes.
Changing feedback duration
A success indicator is briefly shown after copying. You can customise the length of time the indicator is shown using the feedback-duration attribute.
Custom styles
You can customise the button to your liking with CSS.
Properties
| Name | Description | Reflects | Default |
|---|---|---|---|
value | The text value to copy. string | "" | |
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="element[value]"). To copy a property, append a dot and the property name (e.g., from="element.value"). string | "" | |
disabled | Disables the copy button. boolean | false | |
copyLabelcopy-label | A custom label to show in the tooltip. string | "" | |
successLabelsuccess-label | A custom label to show in the tooltip after copying. string | "" | |
errorLabelerror-label | A custom label to show in the tooltip when a copy error occurs. string | "" | |
feedbackDurationfeedback-duration | The length of time to show feedback before restoring the default trigger. number | 1000 | |
tooltipPlacementtooltip-placement | The preferred placement of the tooltip. "top"
| "right"
| "bottom"
| "left" | "top" | |
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 situations. boolean | false | |
updateComplete | A read‐only promise that resolves when the component has finished updating. | - |
Learn more about attributes and properties.
Slots
| Name | Description |
|---|---|
copy-icon | The icon to show in the default copy state. Works best with <pc-icon>. |
success-icon | The icon to show when the content is copied. Works best with <pc-icon>. |
error-icon | The icon to show when a copy error occurs. Works best with <pc-icon>. |
Learn more about using slots.
Events
| Name | Description | Event detail |
|---|---|---|
pc-copy | Emitted when the data has been copied. | - |
pc-error | Emitted when the data could not be copied. | - |
Learn more about events.
Custom properties
| Name | Description | Default |
|---|---|---|
--success-color | The colour to use for success feedback. | - |
--error-color | The colour to use for error feedback. | - |
Learn more about customising custom properties.
Parts
| 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 base part. |
tooltip__base__popup | The tooltip’s popup part. |
tooltip__base__arrow | The tooltip’s arrow part. |
tooltip__body | The tooltip’s body part. |
Learn more about customising CSS parts.
Animations
| Name | Description |
|---|---|
copy.in | The animation to use when the icons animate in. |
copy.out | The animation to use when the icons animate out. |
Learn more about customising animations.
Importing
If you’re using the autoloader or the standard loader, you can ignore this section. If you’re cherry picking, you can use any of the following snippets to import this component.
To import this component from the CDN with a script tag, copy this snippet and paste it in your HTML.
<script type="module" src="https://cdn.jsdelivr.net/npm/placer-toolkit@0.5.1/dist/components/copy-button/copy-button.js"></script> To import this component from the CDN using a JavaScript import, copy this snippet and paste it in your JavaScript:
import "https://cdn.jsdelivr.net/npm/placer-toolkit@0.5.1/dist/components/copy-button/copy-button.js"; To import this component with a bundler using a JavaScript import, copy this snippet and paste it in your JavaScript:
import "placer-toolkit/dist/components/copy-button/copy-button.js"; Dependencies
This component automatically imports these components: