Skip to main content Design tokens Components Style utilities
Skip to table of contents

    Copy Button

    Copies text data to the clipboard when the user clicks the trigger.

    <pc-copy-button value="Placer Toolkit rocks!"></pc-copy-button>
    Code

    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

    NameDescriptionReflectsDefault
    valueThe text value to copy. string""
    fromAn 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""
    disabledDisables the copy button. booleanfalse
    copyLabel
    copy-label
    A custom label to show in the tooltip. string""
    successLabel
    success-label
    A custom label to show in the tooltip after copying. string""
    errorLabel
    error-label
    A custom label to show in the tooltip when a copy error occurs. string""
    feedbackDuration
    feedback-duration
    The length of time to show feedback before restoring the default trigger. number1000
    tooltipPlacement
    tooltip-placement
    The preferred placement of the tooltip. "top" | "right" | "bottom" | "left""top"
    hoistEnable 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. booleanfalse
    updateComplete A read‐only promise that resolves when the component has finished updating. -

    Learn more about attributes and properties.

    Slots

    NameDescription
    copy-iconThe icon to show in the default copy state. Works best with <pc-icon>.
    success-iconThe icon to show when the content is copied. Works best with <pc-icon>.
    error-iconThe icon to show when a copy error occurs. Works best with <pc-icon>.

    Learn more about using slots.

    Events

    NameDescriptionEvent detail
    pc-copyEmitted when the data has been copied.-
    pc-errorEmitted when the data could not be copied.-

    Learn more about events.

    Custom properties

    NameDescriptionDefault
    --success-colorThe colour to use for success feedback.-
    --error-colorThe colour to use for error feedback.-

    Learn more about customising custom properties.

    Parts

    NameDescription
    buttonThe internal <button> element.
    copy-iconThe container that holds the copy icon.
    success-iconThe container that holds the success icon.
    error-iconThe container that holds the error icon.
    tooltip__baseThe tooltip’s base part.
    tooltip__base__popupThe tooltip’s popup part.
    tooltip__base__arrowThe tooltip’s arrow part.
    tooltip__bodyThe tooltip’s body part.

    Learn more about customising CSS parts.

    Animations

    NameDescription
    copy.inThe animation to use when the icons animate in.
    copy.outThe 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.

    ScriptImportBundler

    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: