Copy Button

A button that copies text to the clipboard and confirms with a check.

Installation

npx lily-svelte@latest add copy-button

Installs the style saved in lily.json. For a new Diamond project, run lily-svelte init --style diamond first.

Usage

<script lang="ts">
	import { CopyButton } from '$lib/components/ui/copy-button';
</script>
 
<CopyButton text="Hello, lily!" />

CopyButton is a Button — variant, size and every button attribute pass through. It defaults to the ghost variant and the icon size.

With a label

Pass children to show text next to the icon; the size switches to default so there is room.

<CopyButton text={command} size="sm">
	{#snippet icon()}
		<Icon icon="heroicons:command-line-solid" />
	{/snippet}
	<span class="font-mono text-xs">{command}</span>
</CopyButton>

Feedback

After a copy the icon swaps to a check (or a cross if the clipboard was unavailable) for delay milliseconds — 1500 by default. onCopy receives the same 'success' | 'failure' status.

<CopyButton text={token} delay={800} onCopy={(status) => console.log(status)} />

The hook

The button is built on UseClipboard, which you can use on its own.

<script lang="ts">
	import { UseClipboard } from '$lib/hooks/use-clipboard.svelte';
	import { Button } from '$lib/components/ui/button';
 
	const clipboard = new UseClipboard();
</script>
 
<Button onclick={() => clipboard.copy('hello')}>
	{clipboard.copied ? 'Copied' : 'Copy'}
</Button>

Built by levish. The source code is available on GitHub.

Quiet by design.