Skip to main content
Light Dark System

Copy Button

<zn-copy-button> | ZnCopyButton
Since 1.0 experimental

Short summary of the component’s intended use.

<zn-copy-button value="Hello, World!"></zn-copy-button>

Examples

Copying a Value

Use the value attribute to specify the text that should be copied to the clipboard.

<zn-copy-button value="This text will be copied"></zn-copy-button>
<zn-copy-button value="support@example.com"></zn-copy-button>
<zn-copy-button value="npm install @kubex/zinc"></zn-copy-button>

Copying from Elements

Use the from attribute to copy content from another element by its ID. By default, it copies the element’s textContent.

This is some text that can be copied.


<div id="content-to-copy">This is some text that can be copied.</div>
<zn-copy-button from="content-to-copy"></zn-copy-button>

<br /><br />

<input id="email-input" type="email" value="user@example.com" />
<zn-copy-button from="email-input.value"></zn-copy-button>

Copying Element Attributes

Append the attribute name in square brackets to copy a specific attribute value.

<a id="my-link" href="https://example.com">Example Link</a>
<zn-copy-button from="my-link[href]"></zn-copy-button>

<br /><br />

<img id="my-image" src="https://via.placeholder.com/150" alt="Placeholder" />
<zn-copy-button from="my-image[src]"></zn-copy-button>

Copying Element Properties

Append a dot and the property name to copy a specific property value from an element.



<input id="username" type="text" value="john.doe" />
<zn-copy-button from="username.value"></zn-copy-button>

<br /><br />

<textarea id="bio">Software developer passionate about web components.</textarea>
<zn-copy-button from="bio.value"></zn-copy-button>

Custom Copy Label

Use the copy-label attribute to customize the tooltip text shown before copying.

<zn-copy-button value="Copy me!" copy-label="Click to copy"></zn-copy-button>
<zn-copy-button value="API Key: abc123xyz" copy-label="Copy API Key"></zn-copy-button>
<zn-copy-button value="https://example.com" copy-label="Copy URL"></zn-copy-button>

Custom Icon

Use the src attribute to change the copy icon.

<zn-copy-button value="Custom icon button" src="content_paste"></zn-copy-button>
<zn-copy-button value="Another icon" src="file_copy"></zn-copy-button>

Icon Size

Use the size attribute to control the icon size.

<zn-copy-button value="Small icon" size="16"></zn-copy-button>
<zn-copy-button value="Default size" size="24"></zn-copy-button>
<zn-copy-button value="Large icon" size="32"></zn-copy-button>
<zn-copy-button value="Extra large" size="48"></zn-copy-button>

Custom Icons with Slots

You can provide custom icons for the copy, success, and error states using slots.

<zn-copy-button value="Custom icons">
  <zn-icon slot="copy-icon" src="content_copy" size="24"></zn-icon>
  <zn-icon slot="success-icon" src="check_circle" size="24"></zn-icon>
  <zn-icon slot="error-icon" src="error" size="24"></zn-icon>
</zn-copy-button>

Status Feedback

The copy button provides automatic visual feedback when the copy operation succeeds or fails. Click the button to see the status change.



<zn-copy-button value="Click to see success feedback"></zn-copy-button>

<br /><br />

<!-- This will show error feedback since the element doesn't exist -->
<zn-copy-button from="non-existent-element"></zn-copy-button>

Listening to Events

The copy button emits zn-copy events when text is successfully copied and zn-error events when the copy operation fails.

<zn-copy-button
  id="event-demo"
  value="Copy me to see the event"
  copy-label="Copy text">
</zn-copy-button>

<div id="event-log"></div>

<script type="module">
  const copyButton = document.getElementById('event-demo');
  const eventLog = document.getElementById('event-log');

  copyButton.addEventListener('zn-copy', (event) => {
    eventLog.innerHTML = `<zn-alert caption="Copy Success" level="success" icon="check">
      Copied: "${event.detail.value}"
    </zn-alert>`;
  });

  copyButton.addEventListener('zn-error', () => {
    eventLog.innerHTML = `<zn-alert caption="Copy Error" level="error" icon="error">
      Failed to copy to clipboard
    </zn-alert>`;
  });
</script>

Real-world Examples

Here are some practical use cases for copy buttons.

npm install @kubex/zinc
import { ZnButton } from '@kubex/zinc';


sk_live_abc123xyz789


<!-- Code snippet with copy button -->
<div style="display: flex; align-items: start; gap: 8px;">
  <pre id="code-snippet" style="background: #f5f5f5; padding: 12px; border-radius: 4px; margin: 0; flex: 1;">npm install @kubex/zinc
import { ZnButton } from '@kubex/zinc';</pre>
  <zn-copy-button from="code-snippet" copy-label="Copy code"></zn-copy-button>
</div>

<br /><br />

<!-- API Key display with copy button -->
<div style="display: flex; align-items: center; gap: 8px;">
  <span style="font-family: monospace; background: #f5f5f5; padding: 8px; border-radius: 4px;">sk_live_abc123xyz789</span>
  <zn-copy-button value="sk_live_abc123xyz789" copy-label="Copy API Key"></zn-copy-button>
</div>

<br /><br />

<!-- URL with copy button -->
<div style="display: flex; align-items: center; gap: 8px;">
  <input
    id="share-url"
    type="text"
    value="https://example.com/share/abc123"
    readonly
    style="flex: 1; padding: 8px; border: 1px solid #ddd; border-radius: 4px;" />
  <zn-copy-button from="share-url.value" copy-label="Copy share link"></zn-copy-button>
</div>

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.

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

<script type="module" src="https://cdn.jsdelivr.net/npm/@kubex/zinc@1.0.66/dist/components/copy-button/copy-button.js"></script>

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

import 'https://cdn.jsdelivr.net/npm/@kubex/zinc@1.0.66/dist/components/copy-button/copy-button.js';

To import this component using a bundler:

import '@kubex/zinc/dist/components/copy-button/copy-button.js';

Slots

Name Description
(default) The default slot.
example An example slot.

Learn more about using slots.

Properties

Name Description Reflects Type Default
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 ''
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
zn-event-name Emitted as an example. -

Learn more about events.

Custom Properties

Name Description Default
--example An example CSS custom property.

Learn more about customizing CSS custom properties.

Parts

Name Description
base The component’s base wrapper.

Learn more about customizing CSS parts.

Dependencies

This component automatically imports the following dependencies.

  • <zn-example>