Copy Button
Button that writes a string to the clipboard, confirms with a check icon for two seconds and announces the result. It also reports a rejected clipboard write.
How it differs. The smallest copy primitive: one icon or icon plus label button with idle, copied and failed states, a stable name and feedback through a status region.
Install
pnpm dlx shadcn@latest add @hazine/copy-buttonAdds 1 file, 1 shadcn primitive (button), 1 package (lucide-react), and the success token.
First time? Add the registry to components.json
{
"registries": {
"@hazine": "https://api.hazine.dev/r/{name}.json"
}
}Needed once per project. Items resolve each other through the @hazine namespace.
The demo in the preview, 26 lines
import { CopyButton } from "@/components/hazine/copy-button"
const COMMAND = "pnpm dlx shadcn@latest add @hazine/copy-button"
export default function CopyButtonDemo() {
return (
<div className="flex h-9 max-w-full items-center gap-2 rounded-md border border-border bg-muted/50 ps-3 pe-1 font-mono text-sm">
<span className="truncate" title={COMMAND}>
{COMMAND}
</span>
<CopyButton value={COMMAND} copyLabel="Copy install command" />
</div>
)
}
function CopyButtonWithLabel() {
return (
<CopyButton value="https://tallyroom.example/invite/8f2k" variant="outline">
Copy invite link
</CopyButton>
)
}
export const scenarios = {
"with-label": CopyButtonWithLabel,
}
Props6
- valuerequired
- string
- Text written to the clipboard.
- timeout
- number = 2000
- Milliseconds the result stays visible before the button returns to rest.
- copyLabel
- string = "Copy to clipboard"
- Accessible name of the icon-only button. It does not change with the result.
- copiedLabel
- string = "Copied"
- Announced after a successful copy.
- failedLabel
- string = "Copy failed. Select the text manually."
- Announced when the browser rejects the clipboard write.
- onValueCopy
- (value: string) => void
Also accepts every prop of Omit<React.ComponentProps<typeof Button>, "value">.
Accessibility
- Keyboard
Keyboard support for Copy Button Key Effect Enter or Space Copies the value and announces the result - Screen reader
A native button whose accessible name stays the same in every state. The result is announced by a status region next to the button.
- Announces: Copied
- Announces: Copy failed. Select the text manually.
- Reduced motion
- No motion. The icon swaps without a transition.
- Checked
- Not checked yet. Automated results appear here once CI has run.
Dependencies2
- button
- lucide-react
Filled chips are registry items the CLI adds for you. Outlined chips are npm packages.
Tokens shipped2
- --success
- --success-foreground
Added to your stylesheet in light and dark, unless you already define them.