Copy button
An icon button that copies text to the clipboard and says it did.
Example
API key sk_live_51H8
Install guide
<p> API key <code id="api-key">sk_live_51H8</code> <focsd-copy-button from="api-key" label="Copy API key"></focsd-copy-button></p><p> Install guide <focsd-copy-button value="https://focsd.dev/install" label="Copy install guide link"></focsd-copy-button></p>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-copy-button.js';</script>
<focsd-copy-button from="api-key" label="Copy API key"></focsd-copy-button>import { CopyButton } from '@focsd-dev/react';
export function Example() { return <CopyButton from="api-key" label="Copy API key"></CopyButton>;}<script setup>import '@focsd-dev/components/focsd-copy-button.js';</script>
<template> <focsd-copy-button from="api-key" label="Copy API key"></focsd-copy-button></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-copy-button.js';
// template<focsd-copy-button from="api-key" label="Copy API key"></focsd-copy-button><script> import '@focsd-dev/components/focsd-copy-button.js';</script>
<focsd-copy-button from="api-key" label="Copy API key"></focsd-copy-button>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
value | value | string | — |
from | from | string | — |
label | label | string | — |
copied-label | copiedLabel | string | — |
Methods
| Method | Description |
|---|---|
copy() | Copies the text to the clipboard, then announces the result and fires focsd-copy or focsd-error.
An empty text (no value, or from naming no element or an empty one) is a failure. |
CSS parts
Style the inside of the component with ::part(), for example focsd-copy-button::part(button).
| Part | Description |
|---|---|
button | The button. |
icon | The copy icon: a check after a copy, an alert after a failed one (`data-icon` names it). |
status | The visually hidden status region that announces the result; `data-tone="critical"` after a failed copy. |
Accessibility
- The button is named "Copy" unless you set
label; name it after what it copies when a page has several ("Copy API key"). - The name stays the same after a copy. A polite status region says "Copied" (or
copied-label), and the icon turns to a check for 2 seconds (an alert icon when the copy failed). - When the browser refuses the write, the status says "Copy failed" and the element fires
focsd-error; it never says "Copied" for a copy that did not happen. - With
from, it copies the value of the form control with that id, or the trimmed text of any other element. Without the Clipboard API (pages not served over HTTPS) it falls back toexecCommand('copy'). - It fires
focsd-copywithdetail.textafter a copy.focsd-codeuses it for its "Copy code" button.
Keyboard
| Key | Action |
|---|---|
| Enter / Space | Copy the text |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.