Copy button and keyboard key
Two small helpers: zb:copy-button copies a text to the clipboard with one click, and zb:kbd shows keyboard keys styled as keys.
zb:copy-button · zb:kbd zubazar/ui · free (MIT)
Copying text
Pass the text to copy in text; variant and size set the look and label the text.
<div class="flex flex-wrap items-center gap-3">
<span class="font-mono text-sm text-ink">ORD-2026-004817</span>
<zb:copy-button text="ORD-2026-004817" size="sm" />
<zb:copy-button text="ORD-2026-004817" variant="soft" size="sm" label="Copy order number" />
</div>
| Prop | Values |
|---|---|
text | Text to copy |
label | Button text; default "Copy" |
variant | zb:button variants; default white |
size | sm | md | lg | icon |
Copying from an element
Pass a CSS selector in target; the form field's value or the element's text is copied.
<div class="space-y-2">
<p id="delivery-address" class="rounded-sm bg-sunken p-3 text-sm">No. 7, Moda St., Caferağa Neighborhood, Kadıköy / Istanbul</p>
<zb:copy-button target="#delivery-address" size="sm" label="Copy address" />
</div>
<div class="space-y-2">
<zb:textarea id="order-note" label="Order note" rows="2">Please gift-wrap it and email the invoice separately.</zb:textarea>
<zb:copy-button target="#order-note" size="sm" label="Copy note" />
</div>
| Prop | Values |
|---|---|
target | CSS selector, e.g. #delivery-address |
Inside an input group
size="icon" shows only the icon; label then becomes the screen reader name and the tooltip.
<zb:input-group id="invite-link" label="Invite link" value="https://panel.ornek.test/davet/K7M2-Q9XA" readonly>
<zb:slot:after>
<zb:copy-button
target="#invite-link"
variant="ghost"
size="icon"
label="Copy link"
class="-me-1.5"
x-on:copied="$dispatch('toast', { type: 'success', message: 'Invite link copied to the clipboard.' })"
/>
</zb:slot:after>
</zb:input-group>
| Prop | Values |
|---|---|
size="icon" | Icon only; the name comes from label |
x-on:copied | Runs after a successful copy |
Keyboard key
zb:kbd shows a single key within text, without breaking the flow of the line.
<div class="space-y-3 text-sm text-dim">
<p>Press <zb:kbd>Esc</zb:kbd> to close the open window.</p>
<p>Move through the list with <zb:kbd>↑</zb:kbd> and <zb:kbd>↓</zb:kbd>, and press <zb:kbd>↵</zb:kbd> to select.</p>
</div>
| Prop | Values |
|---|---|
slot | Name or symbol of the key |
Key combinations
keys takes the keys pressed together as an array and puts separator between them; size="sm" draws smaller keys.
<div class="space-y-3 text-sm text-dim">
<p>Open search: <zb:kbd :keys="['⌘', 'K']" /> or <zb:kbd :keys="['Ctrl', 'K']" /></p>
<p>Save your changes: <zb:kbd :keys="['Ctrl', 'Shift', 'S']" /></p>
<div class="flex flex-wrap items-center gap-4">
<zb:kbd :keys="['⌘', '⇧', 'P']" separator="" size="sm" />
<zb:kbd :keys="['Alt', 'F4']" size="sm" />
</div>
</div>
| Prop | Values |
|---|---|
keys | Array, e.g. ['⌘', 'K'] |
separator | Mark between the keys; default '+' |
size | sm | md |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
copy-button: text | — | null | Text written to the clipboard |
copy-button: target | — | null | CSS selector used when text is not given: the value of a form field or the text of the element is copied |
copy-button: label | — | Copy | Button text; with size="icon", the screen reader name and the tooltip |
copy-button: variant | — | white | One of the zb:button variants (primary, ghost, soft…) |
copy-button: size | — | md | sm · md · lg · icon (icon only) |
copy-button: timeout | number | 2000 | How many milliseconds the success or error state stays visible |
copy-button: copied | — | — | Event dispatched from the button after a successful copy; detail: { text } |
kbd: keys | — | null | Array of keys pressed together; if omitted, the slot is a single key |
kbd: separator | — | + | Mark between the keys of a combination; an empty string removes it |
kbd: size | — | md | sm · md |
Slots: kbd: default content (a single key). copy-button takes no slot; attributes such as class, disabled, and wire:click are added to the button itself.
Developer notes
- On click, the button shows a check icon and "Copied" for
timeout(2 seconds); the result is also announced to screen readers. - If the Clipboard API is not available (plain http, an iframe without permission), the older method is tried; if that fails too, the button says "Could not copy".
targetis read at the moment of the click, so changes in the field and Livewire updates show in the copy; leading and trailing whitespace is removed.- After a successful copy, the button dispatches the
copiedevent ($event.detail.text); the example shows a notification with it. - The default
separatoris+, and an empty string removes it;size="sm"is for menu rows and button hints. - Symbols such as ⌘, ⇧ and ↵ are read to screen readers as "Command", "Shift" and "Enter".
Events
| Event | detail | Description |
|---|---|---|
copied | { text } | zb:copy-button: sent from the button after a successful copy; it bubbles. Not sent when copying fails. |