Copy Buttons

Copy Buttons

<button type="button" class="btn" data-variant="ghost" data-size="icon-xs" aria-label="Copy email address" data-tooltip="Copy" data-copy="maya.chen@example.com"><i class="icon icon-copy" aria-hidden="true"></i></button>

data-copy-target copies an input's value

<div class="field w-full max-w-sm">
<label for="demo-copy-client-id">Client ID</label>
<div class="input-group">
<input type="text" id="demo-copy-client-id" value="9f3b2c71-4d6e-4a8b-b1f0-2e7c5d9a4f18" readonly />
<button type="button" class="btn" data-variant="ghost" data-size="icon-xs" data-align="end" aria-label="Copy client ID" data-tooltip="Copy" data-copy-target="#demo-copy-client-id"><i class="icon icon-copy" aria-hidden="true"></i></button>
</div>
</div>

data-copy-target copies the text of any other element

shpat_4f9a1c7e2b8d6035
<div class="bg-muted relative w-full max-w-sm rounded-md">
<pre class="overflow-x-auto p-3 pe-10 text-xs/5"><code id="demo-copy-token">shpat_4f9a1c7e2b8d6035</code></pre>
<button type="button" class="btn absolute end-2 top-2.5" data-variant="ghost" data-size="icon-xs" aria-label="Copy access token" data-copy-target="#demo-copy-token"><i class="icon icon-copy" aria-hidden="true"></i></button>
</div>

data-copied is the tooltip shown once the text is copied

<button type="button" class="btn" data-variant="outline" data-copy="https://example.com/vouchers/SPRING25" data-copied="Link copied">Copy link</button>

copy-success is sent once the clipboard holds the text

Nothing copied yet.
<div class="flex items-center gap-3">
<button type="button" id="demo-copy-event" class="btn" data-variant="outline" data-copy="SPRING25">Copy code</button>
<span id="demo-copy-event-output" class="text-muted-foreground text-sm">Nothing copied yet.</span>
</div>
<script>
document.getElementById("demo-copy-event").addEventListener("copy-success", (event) => {
document.getElementById("demo-copy-event-output").textContent = "Copied " + event.detail.text;
});
</script>

API Reference

Attributes

Attribute On Values Default Description
data-copy the button text none The text a click copies.
data-copy-target the button a selector none Copies the matching element instead: the value of an input or a textarea, the text of any other element. Read on each click. data-copy wins when both are written.
data-copied the button text "Copied" The tooltip shown once the text is copied.
data-tooltip the button text absent Set by the script to the data-copied text after a copy, and put back to what the page wrote (or removed) when the pointer leaves.

Events

Event On Detail Description
copy-success the button { text } Sent once the clipboard holds the text. Bubbles.
copy-error the button { error } Sent when the browser refuses to write to the clipboard. No tooltip is shown. Bubbles.

Keyboard

Key Description
Enter or Space The browser's own: they click the button, which copies.

It writes with navigator.clipboard.writeText, which browsers allow only in secure contexts (HTTPS).

The tooltip is CSS and shows on hover only, so a copy made from the keyboard shows no Copied.

Once the script has started it, the element carries data-copy-initialized="true" and sends tsugi:initialized. The methods and properties above are on the element itself.