# CopyButton

> Button preset wired to the clipboard, morphing its icon and label to a success or failure state for a moment after each copy attempt.

- Package: `fancy-ui-svelte` (Svelte 5 + Tailwind CSS v4)
- Category: Actions
- Tags: copy, clipboard, button, success
- Dependencies: button, status-morph
- Types: `CopyButtonProps`, `CopyButtonAttributes`
- Docs: https://fancy-ui.rama.app/docs/components/copy-button
- Source: https://github.com/RamaHerbin/fancy-ui/tree/main/src/lib/fancy-ui/copy-button

## Import

```svelte
<script lang="ts">
  import { CopyButton } from 'fancy-ui-svelte';
</script>
```

Setup (once per project): `npm install fancy-ui-svelte`, then add `@import "fancy-ui-svelte/tailwind.css";` after `@import "tailwindcss";` in your main CSS file. Full guide: https://fancy-ui.rama.app/llms.txt

React: `import { CopyButton } from 'fancy-ui-react';` (`class` becomes `className`, snippets become `ReactNode`, DOM callbacks are camel-cased; exact names on https://fancy-ui.rama.app/docs/components/react/copy-button.md).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` (required) | `string` | — | The text written to the clipboard on activation |
| `label` | `string` | `"Copy"` | Idle label |
| `copiedLabel` | `string` | `"Copied"` | Label shown for resetMs after a successful copy |
| `errorLabel` | `string` | `"Copy failed"` | Label and announcement shown after a failed copy |
| `resetMs` | `number` | `2000` | How long the copied state holds before reverting, in milliseconds |
| `variant` | `ButtonVariant` | `"outline"` | Passed straight through to the underlying Button |
| `size` | `ButtonSize` | `"md"` | Passed straight through to the underlying Button |
| `disabled` | `boolean` | `false` | Disables the button and blocks the copy |
| `iconOnly` | `boolean` | `false` | Drops the visible label, moving it to aria-label instead |
| `onCopy` | `(value: string, ok: boolean) => void` | — | Called with the value and whether the write actually succeeded |
| `onclick` | `(event: MouseEvent) => void` | — | Click handler, called right after the copy starts; never called while disabled |
| `class` | `string` | — | Additional CSS classes |
| `ref` | `HTMLButtonElement \| HTMLAnchorElement \| null` | `null` | Bindable element reference, matching Button's own ref type |
| `sound` | `boolean` | `false` | Plays the copy cue on a successful copy and error on a failed one, once the user has enabled sound |

## Snippets

| Snippet | Description |
| --- | --- |
| `children` | Overrides the default icon + label content |

## Examples

### Basic Usage

An outline button that copies a value on click.

```svelte
<script lang="ts">
	import { CopyButton } from "fancy-ui-svelte";
</script>

<CopyButton sound value="npm install fancy-ui-svelte" />
```

### Icon Only

Drops the visible label into aria-label for a toolbar.

```svelte
<script lang="ts">
	import { CopyButton } from "fancy-ui-svelte";
</script>

<CopyButton sound value="npm install fancy-ui-svelte" iconOnly label="Copy install command" />
```

### Custom Labels

Overrides the idle and copied label text.

```svelte
<script lang="ts">
	import { CopyButton } from "fancy-ui-svelte";
</script>

<CopyButton
	sound
	value="https://example.com/invite/9k2f"
	label="Copy link"
	copiedLabel="Link copied"
/>
```

### Variants

Four Button variants used as the idle skin.

```svelte
<script lang="ts">
	import { CopyButton } from "fancy-ui-svelte";
	import type { ButtonVariant } from "fancy-ui-svelte";

	const variants: ButtonVariant[] = ["outline", "ghost", "secondary", "accent"];
</script>

<div class="flex flex-wrap items-center gap-3">
	{#each variants as variant}
		<CopyButton sound value="npm install fancy-ui-svelte" {variant} />
	{/each}
</div>
```

### Interactive Playground

```svelte
<script lang="ts">
	import { CopyButton } from "fancy-ui-svelte";
	import type { ButtonSize, ButtonVariant } from "fancy-ui-svelte";
	import PropsPlayground from "$lib/components/PropsPlayground.svelte";
</script>

<PropsPlayground
	component="CopyButton"
	slug="copy-button"
	required={["value"]}
	constraints={[
		"`value` is required: it is the text written to the clipboard.",
		"Clipboard access needs a secure context (https or localhost).",
	]}
	controls={[
		{ key: "value", type: "text", label: "Value to copy", maxLength: 80 },
		{ key: "label", type: "text", label: "Label", maxLength: 24 },
		{
			key: "variant",
			type: "select",
			label: "Variant",
			options: ["primary", "secondary", "outline", "ghost", "accent", "destructive"].map((v) => ({
				value: v,
				label: v,
			})),
		},
		{
			key: "size",
			type: "select",
			label: "Size",
			options: ["sm", "md", "lg"].map((v) => ({ value: v, label: v })),
		},
		{ key: "iconOnly", type: "boolean", label: "Icon only" },
		{ key: "disabled", type: "boolean", label: "Disabled" },
	]}
	initialValues={{
		value: "npm install fancy-ui-svelte",
		label: "Copy",
		variant: "outline",
		size: "md",
		iconOnly: false,
		disabled: false,
	}}
>
	{#snippet preview(values)}
		<CopyButton
			value={values.value as string}
			label={values.label as string}
			variant={values.variant as ButtonVariant}
			size={values.size as ButtonSize}
			iconOnly={values.iconOnly as boolean}
			disabled={values.disabled as boolean}
		/>
	{/snippet}
</PropsPlayground>
```
