Light Dark System

Copy Button

<p-copy-button> | PCopyButton
Since 2.7 experimental

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

<p-copy-button value="Pure UI rocks!"></p-copy-button>
import { PCopyButton } from 'pure-uikit/dist/react/copy-button';

const App = () => (
  <PCopyButton value="Pure UI rocks!" />
);

Examples

Custom Labels

Copy Buttons display feedback in a tooltip. You can customize the labels using the copy-label, success-label, and error-label attributes.

<p-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!"
></p-copy-button>
import { PCopyButton } from 'pure-uikit/dist/react/copy-button';

const App = () => (
  <PCopyButton
    value="Custom labels are easy"
    copy-label="Click to copy"
    success-label="You did it!"
    error-label="Whoops, your browser doesn't support this!"
  />
);

Custom Icons

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

<p-copy-button value="Copied from a custom button">
  <p-icon slot="copy-icon" name="clipboard"></p-icon>
  <p-icon slot="success-icon" name="clipboard-check"></p-icon>
  <p-icon slot="error-icon" name="clipboard-x"></p-icon>
</p-copy-button>
import { PCopyButton } from 'pure-uikit/dist/react/copy-button';
import { PIcon } from 'pure-uikit/dist/react/icon';

const App = () => (
  <>
    <PCopyButton value="Copied from a custom button">
      <PIcon slot="copy-icon" name="clipboard" />
      <PIcon slot="success-icon" name="clipboard-check" />
      <PIcon slot="error-icon" name="clipboard-x" />
    </PCopyButton>
  </>
);

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[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.

+1 (234) 456–7890



Pure UI Website
<!-- Copies the span's textContent -->
<span id="my-phone">+1 (234) 456-7890</span>
<p-copy-button from="my-phone"></p-copy-button>

<br><br>

<!-- Copies the input's "value" property -->
<p-input id="my-input" type="text" value="User input" style="display: inline-block; max-width: 300px;"></p-input>
<p-copy-button from="my-input.value"></p-copy-button>

<br><br>

<!-- Copies the link's "href" attribute -->
<a id="my-link" href="https://pureui.xyz/">Pure UI Website</a>
<p-copy-button from="my-link[href]"></p-copy-button>
import { PCopyButton } from 'pure-uikit/dist/react/copy-button';
import { PInput } from 'pure-uikit/dist/react/input';

const App = () => (
  <>
    {/* Copies the span's textContent */}
    <span id="my-phone">+1 (234) 456-7890</span>
    <PCopyButton from="my-phone" />

    <br /><br />

    {/* Copies the input's "value" property */}
    <PInput id="my-input" type="text" />
    <PCopyButton from="my-input.value" />

    <br /><br />

    {/* Copies the link's "href" attribute */}
    <a id="my-link" href="https://pureui.xyz/">Pure UI Website</a>
    <PCopyButton from="my-link[href]" />
  </>
);

Handling Errors

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 p-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.

<p-copy-button from="i-do-not-exist"></p-copy-button>
import { PCopyButton } from 'pure-uikit/dist/react/copy-button';

const App = () => (
  <PCopyButton from="i-do-not-exist" />
);

Disabled

Copy buttons can be disabled by adding the disabled attribute.

<p-copy-button value="You can't copy me" disabled></p-copy-button>
import { PCopyButton } from 'pure-uikit/dist/react/copy-button';

const App = () => (
  <PCopyButton value="You can't copy me" disabled />
);

Changing Feedback Duration

A success indicator is briefly shown after copying. You can customize the length of time the indicator is shown using the feedback-duration attribute.

<p-copy-button value="Pure UI rocks!" feedback-duration="250"></p-copy-button>
import { PCopyButton } from 'pure-uikit/dist/react/copy-button';

const App = () => (
  <PCopyButton value="Pure UI rocks!" feedback-duration={250} />
);

Custom Styles

You can customize the button to your liking with CSS.

<p-copy-button value="I'm so stylish" class="custom-styles">
  <p-icon slot="copy-icon" name="asterisk"></p-icon>
  <p-icon slot="success-icon" name="check-lg"></p-icon>
  <p-icon slot="error-icon" name="x-lg"></p-icon>
</p-copy-button>

<style>
  .custom-styles {
    --success-color: white;
    --error-color: white;
    color: white;
  }

  .custom-styles::part(button) {
    background-color: #ff1493;
    border: solid 4px #ff7ac1;
    border-right-color: #ad005c;
    border-bottom-color: #ad005c;
    border-radius: 0;
    transition: 100ms scale ease-in-out, 100ms translate ease-in-out;
  }

  .custom-styles::part(button):hover {
    scale: 1.1;
  }

  .custom-styles::part(button):active {
    translate: 0 2px;
  }

  .custom-styles::part(button):focus-visible {
    outline: dashed 2px deeppink;
    outline-offset: 4px;
  }
</style>
import { PCopyButton } from 'pure-uikit/dist/react/copy-button';

const css = `
  .custom-styles {
    --success-color: white;
    --error-color: white;
    color: white;
  }

  .custom-styles::part(button) {
    background-color: #ff1493;
    border: solid 4px #ff7ac1;
    border-right-color: #ad005c;
    border-bottom-color: #ad005c;
    border-radius: 0;
    transition: 100ms scale ease-in-out, 100ms translate ease-in-out;
  }

  .custom-styles::part(button):hover {
    scale: 1.1;
  }

  .custom-styles::part(button):active {
    translate: 0 2px;
  }

  .custom-styles::part(button):focus-visible {
    outline: dashed 2px deeppink;
    outline-offset: 4px;
  }
`;

const App = () => (
  <>
    <PCopyButton value="I'm so stylish" className="custom-styles" />

    <style>{css}</style>
  </>
);

Importing

If you’re using the autoloader or the traditional loader, you can ignore this section. Otherwise, feel free to use any of the following snippets to cherry pick this component.

Script Import Bundler React

To import this component from the CDN using a script tag:

<script type="module" src="https://cdn.jsdelivr.net/npm/pure-uikit@1.3.13/cdn/components/copy-button/copy-button.js"></script>

To import this component from the CDN using a JavaScript import:

import 'https://cdn.jsdelivr.net/npm/pure-uikit@1.3.13/cdn/components/copy-button/copy-button.js';

To import this component using a bundler:

import 'pure-uikit/dist/components/copy-button/copy-button.js';

To import this component as a React component:

import PCopyButton from 'pure-uikit/dist/react/copy-button';

Slots

Name Description
copy-icon The icon to show in the default copy state. Works best with <p-icon>.
success-icon The icon to show when the content is copied. Works best with <p-icon>.
error-icon The icon to show when a copy error occurs. Works best with <p-icon>.

Learn more about using slots.

Properties

Name Description Reflects Type 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="el[value]". To copy a property, append a dot and the property name, e.g. from="el.value". string ""
disabled Disables the copy button. boolean false
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. number 1000
tooltipPlacement
tooltip-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, scenarios. boolean false
updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.

Events

Name React Event Description Event Detail
p-copy onPCopy Emitted when the data has been copied. -
p-error onPError Emitted when the data could not be copied. -

Learn more about events.

Custom Properties

Name Description Default
--success-color The color to use for success feedback.
--error-color The color to use for error feedback.

Learn more about customizing CSS 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 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.

Learn more about customizing CSS parts.

Animations

Name Description
copy.in The animation to use when feedback icons animate in.
copy.out The animation to use when feedback icons animate out.

Learn more about customizing animations.

Dependencies

This component automatically imports the following dependencies.

  • <p-icon>
  • <p-popup>
  • <p-tooltip>