CopyToClipboard
One-click copy-to-clipboard component. When the user clicks and successfully copies, the button displays a physically pressed-down state with green badge feedback for 2 seconds.
Demo
变体
尺寸
Installation
pnpm dlx brutx-vue@latest add copy-to-clipboardUsage
<script setup>
import { CopyToClipboard } from 'brutx-ui-vue'
</script>
<template>
<CopyToClipboard text="pnpm install brutx-ui-vue" />
</template>Custom Display Slot
The default slot exposes copied and failed so you can customize the button content for either outcome.
<script setup>
import { CopyToClipboard } from 'brutx-ui-vue'
</script>
<template>
<CopyToClipboard text="Custom Text">
<template #default="{ copied, failed }">
<span>{{ failed ? 'Copy failed' : copied ? 'Copied!' : 'Click to copy' }}</span>
</template>
</CopyToClipboard>
</template>Variants
| Variant | Description |
|---|---|
default | Default background with standard foreground text |
primary | Primary (coral) background with high-contrast foreground |
outline | Transparent background, retaining only border and shadow |
<script setup>
import { CopyToClipboard } from 'brutx-ui-vue'
</script>
<template>
<div class="flex flex-wrap items-center gap-3">
<CopyToClipboard text="default" variant="default" />
<CopyToClipboard text="primary" variant="primary" />
<CopyToClipboard text="outline" variant="outline" />
</div>
</template>Sizes
| Size | Description |
|---|---|
sm | Small size, height h-9, padding px-3, font text-sm |
default | Default size, height h-11, padding px-5, font text-base |
lg | Large size, height h-14, padding px-7, font text-lg |
<script setup>
import { CopyToClipboard } from 'brutx-ui-vue'
</script>
<template>
<div class="flex flex-wrap items-center gap-3">
<CopyToClipboard text="sm" size="sm" />
<CopyToClipboard text="default" size="default" />
<CopyToClipboard text="lg" size="lg" />
</div>
</template>Composable
For a custom copy control, import useClipboard() from brutx-ui-vue or brutx-ui-vue/useClipboard. The UseClipboardReturn type includes a readonly state of idle, copied, or failed. The readonly boolean copied indicates whether state is copied. Each copy(text) call returns a Promise reporting whether that write succeeded.
API Reference
| Name | Type | Default | Description |
|---|---|---|---|
textRequired | string | — | The text written to the clipboard when the user activates the copy action. |
DetailsDeclared typestring Default value details
Sourcepackages/ui/src/components/copy-to-clipboard/CopyToClipboard.vue | |||
duration | number | undefined | 2000 | How long success or failure feedback remains visible, in milliseconds, with a minimum of 100 ms. |
DetailsDeclared typenumber | undefined Default value details
Sourcepackages/ui/src/components/copy-to-clipboard/CopyToClipboard.vue | |||
variant | "default" | "outline" | "primary" | undefined | "default" | Sets the copy button to the default, primary, or outline visual style. |
DetailsDeclared typeNonNullable<"default" | "primary" | "outline" | null | undefined> | undefined Literal values
Default value details
Sourcepackages/ui/src/components/copy-to-clipboard/CopyToClipboard.vue | |||
size | "default" | "lg" | "sm" | undefined | "default" | Sets the preset size of the copy button. |
DetailsDeclared typeNonNullable<"default" | "lg" | "sm" | null | undefined> | undefined Literal values
Default value details
Sourcepackages/ui/src/components/copy-to-clipboard/CopyToClipboard.vue | |||
class | string | undefined | undefined | Custom CSS classes added to the copy button container. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/copy-to-clipboard/CopyToClipboard.vue | |||
iconSize | IconSize | undefined | "md" | Sets the size of the copy and status icons. |
DetailsDeclared typeIconSize | undefined Literal values
Referenced types IconSize Type definition — IconSizeexport type IconSize = NonNullable<VariantProps<typeof iconSizeVariants>['size']> Default value details
Sourcepackages/ui/src/components/copy-to-clipboard/CopyToClipboard.vue | |||
| Name | Signature or type | Description |
|---|---|---|
#default | { copied: boolean; failed: boolean; } | Customizes the copy button content. copied and failed reflect the mutually exclusive outcomes of the most recently started copy. |
DetailsDeclared type{ copied: boolean; failed: boolean; }Sourcepackages/ui/src/components/copy-to-clipboard/CopyToClipboard.vue | ||
Copy failure feedback
On a failed copy the default label switches to "Copy failed" (customizable by overriding the locale copyToClipboard.copyFailed) and enters a red destructive state; a visually-hidden role="status" live region announces success/failure to screen readers. Starting a copy clears the previous feedback. The button displays the outcome of the most recently started copy and returns to its default content after duration for either outcome.
<script setup>
import { CopyToClipboard } from 'brutx-ui-vue'
</script>
<template>
<CopyToClipboard text="Custom Text">
<template #default="{ copied, failed }">
<span>{{ failed ? 'Copy failed' : copied ? 'Copied!' : 'Click to copy' }}</span>
</template>
</CopyToClipboard>
</template>Accessibility
- Keyboard: Supports
Enter/Spaceto trigger the copy operation - ARIA Attributes: The button includes a text description of the copy state; success/failure is announced via a visually-hidden
role="status"live region, perceivable by screen readers even when the button is not focused