Components
Copy Button
Copies text to the clipboard and confirms the action in place.
Installation
$ npx veil-ui@latest add copy-buttonUsage
import { CopyButton } from "@/components/ui/copy-button";
<CopyButton value="npx veil-ui@latest add button" />;Examples
Basic
npx veil-ui@latest add buttonSizes
Ghost
API key
sk_live_51H8API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| value | string | — | Text written to the clipboard. |
| label | string | "Copy" | Accessible name before copying. |
| copiedLabel | string | "Copied" | Accessible name and announcement after copying. |
| timeout | number | 1600 | How long the confirmed state lasts, in milliseconds. |
| ...props | Omit<IconButtonProps, 'children' | 'aria-label'> | — | Variant, size and native button attributes. |
Accessibility
- The button name changes from “Copy” to “Copied”, and a polite live region announces the result without moving focus.
- Labels are overridable, so the name can describe the target, such as “Copy API key”.
- Clipboard failures leave the button in its original state instead of claiming success.
Vue and plain HTML
This component also ships for Vue (Reka UI) and plain HTML. Install it with npx veil-ui add copy-button --framework vue or switch the framework in components.json.
<script setup lang="ts">
import { Check, Copy } from "lucide-vue-next";
import { computed, onBeforeUnmount, ref } from "vue";
import { cn } from "@/lib/utils";
import { iconButtonVariants, type IconButtonVariants } from "./variants";
defineOptions({ inheritAttrs: false });
const props = withDefaults(
defineProps<{
/** Text written to the clipboard. */
value: string;
/** Accessible name before copying. */
label?: string;
/** Accessible name and announcement after copying. */
copiedLabel?: string;
/** How long the copied state lasts, in milliseconds. */
timeout?: number;
variant?: IconButtonVariants["variant"];
size?: IconButtonVariants["size"];
class?: string;
}>(),
{
label: "Copy",
copiedLabel: "Copied",
timeout: 1600,
variant: "default",
size: "default",
},
);
const copied = ref(false);
let timer: ReturnType<typeof setTimeout> | null = null;
const classes = computed(() =>
cn(iconButtonVariants({ variant: props.variant, size: props.size }), props.class),
);
function clearTimer() {
if (timer) {
clearTimeout(timer);
timer = null;
}
}
async function handleCopy() {
try {
await navigator.clipboard.writeText(props.value);
copied.value = true;
clearTimer();
timer = setTimeout(() => {
copied.value = false;
timer = null;
}, props.timeout);
} catch {
copied.value = false;
}
}
onBeforeUnmount(clearTimer);
</script>
<template>
<button
v-bind="$attrs"
type="button"
data-slot="copy-button"
:data-copied="copied ? '' : undefined"
:aria-label="copied ? copiedLabel : label"
:class="classes"
@click="handleCopy"
>
<Check v-if="copied" aria-hidden />
<Copy v-else aria-hidden />
</button>
<span aria-live="polite" class="sr-only">
{{ copied ? copiedLabel : "" }}
</span>
</template>