Copy Button Examples
The examples below assume that you have imported the component and set up the theme.
Basic
Add a button that copies a fixed text, like an install command. value holds the text, and the default icons and built-in tooltip show the result.
The duration control sets feedback-duration, the time the result stays on screen.
<div class="copy-button-basic"> <div class="field"> <code>npm install @staticview/ui</code> <sv-copy-button value="npm install @staticview/ui"></sv-copy-button> </div></div>
<style> .copy-button-basic { display: grid; place-content: center;
.field { display: flex; gap: 0.5em; }
code { align-content: center; padding: 0.4em 0.75em; background-color: var(--sv-gray-7); border: solid var(--sv-bdr-sz-sm) var(--sv-gray-5); border-radius: var(--sv-bdr-rad-md); }
svg { display: block; inline-size: var(--sv-copy-button-icon-sz, 1.25em); block-size: var(--sv-copy-button-icon-sz, 1.25em); fill: currentcolor; } }</style>Copy from another element
Copy the current text of a field or the address of a link on the page. from="id" copies the current value of the input with that id, and from="id[href]" copies the href of the link.
<div class="copy-button-from"> <figure> <div class="field"> <input id="copy-button-invite-link" aria-label="Invite link" readonly value="https://example.com/invite/4f9a" /> <sv-copy-button copy-label="Copy invite link" from="copy-button-invite-link"></sv-copy-button> </div> <figcaption><code>from="id"</code></figcaption> </figure>
<figure> <div class="field"> <a id="copy-button-docs-link" href="https://example.com/docs/getting-started"> Getting started </a> <sv-copy-button copy-label="Copy link address" from="copy-button-docs-link[href]" ></sv-copy-button> </div> <figcaption><code>from="id[href]"</code></figcaption> </figure></div>
<style> .copy-button-from { display: grid; gap: 2em; justify-items: center;
figure { display: grid; gap: 0.75rem; justify-items: center; margin: 0;
&:has(input) { inline-size: min(100%, 22rem); } }
figcaption { font-size: 0.8rem; color: var(--sv-gray-3); }
.field { display: flex; gap: 0.5em;
&:has(a) { align-items: center; }
&:has(input) { justify-self: stretch; } }
a { color: var(--sv-accent); }
input { flex-grow: 1; inline-size: 0; padding: 0.4em 0.75em; font: inherit; color: inherit; background-color: var(--sv-gray-7); border: solid var(--sv-bdr-sz-sm) var(--sv-gray-5); border-radius: var(--sv-bdr-rad-md); } }</style>Text and labels
Show a text button, or translate the labels. The copy-icon, success-icon, and error-icon slots take text as well as icons, copy-label sets the name and the tooltip, and success-label and error-label set the result text.
With copy-label="", the visible slot names the button, and each of the three slots needs its own text.
<div class="copy-button-content"> <figure> <sv-copy-button copy-label="" value="ABC-123"> <span slot="copy-icon">Copy code</span> <span slot="success-icon">Copied</span> <span slot="error-icon">Not copied</span> </sv-copy-button> <figcaption><code>copy-label=""</code></figcaption> </figure>
<figure> <sv-copy-button copy-label="Kopieren" error-label="Kopieren fehlgeschlagen" success-label="Kopiert" value="ABC-123" ></sv-copy-button> <figcaption><code>copy-label="Kopieren"</code></figcaption> </figure></div>
<style> .copy-button-content { display: grid; gap: 2em; justify-items: center;
figure { display: grid; gap: 0.75rem; justify-items: center; margin: 0; }
figcaption { font-size: 0.8rem; color: var(--sv-gray-3); } }</style>Custom tooltip
Show the result in a styled tooltip, in place of the built-in one. title="" turns off the built-in tooltip, data-sv-tooltip with aria-describedby attaches an sv-tooltip, and the --copied and --failed states pick the text it shows.
<div class="copy-button-tooltip"> <code>npm install @staticview/ui</code> <sv-copy-button aria-describedby="copy-button-command-tooltip" data-sv-tooltip="copy-button-command-tooltip" title="" value="npm install @staticview/ui" ></sv-copy-button>
<sv-tooltip id="copy-button-command-tooltip"> <span class="copy-text">Copy the command</span> <span class="copied-text">Copied to the clipboard</span> <span class="failed-text">Press Ctrl+C to copy</span> </sv-tooltip></div>
<style> .copy-button-tooltip { display: flex; gap: 0.5em; justify-content: center;
code { align-content: center; padding: 0.4em 0.75em; background-color: var(--sv-gray-7); border: solid var(--sv-bdr-sz-sm) var(--sv-gray-5); border-radius: var(--sv-bdr-rad-md); }
sv-tooltip { font-size: 0.875em; white-space: nowrap; }
.copied-text, .failed-text, &:has(sv-copy-button:is(:state(--copied), :state(--failed))) .copy-text { display: none; }
&:has(sv-copy-button:state(--copied)) .copied-text, &:has(sv-copy-button:state(--failed)) .failed-text { display: inline; } }</style>