Skip to content
Focsd homeFocsd homeFocsd
Forms

Copy button

An icon button that copies text to the clipboard and says it did.

  • Tag: focsd-copy-button
  • Pattern: Button + polite status
  • Category: Actions

Example

API key sk_live_51H8

Install guide

HTML
<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>

API

Attributes

AttributePropertyTypeDefault
valuevaluestring—
fromfromstring—
labellabelstring—
copied-labelcopiedLabelstring—

Methods

MethodDescription
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).

PartDescription
buttonThe button.
iconThe copy icon: a check after a copy, an alert after a failed one (`data-icon` names it).
statusThe 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 to execCommand('copy').
  • It fires focsd-copy with detail.text after a copy. focsd-code uses it for its "Copy code" button.

Keyboard

KeyAction
Enter / SpaceCopy the text