useCopyToClipboardutilities
useCopyToClipboard
NewSSR SAFECopies text to clipboard with feedback.
$ pnpm dlx shadcn@latest add @byphongphan/use-copy-to-clipboardRequires
@byphongphan in components.jsonuse-copy-to-clipboard.tsx
Loading preview...
Installation
$ pnpm dlx shadcn@latest add @byphongphan/use-copy-to-clipboardRequires
@byphongphan in components.jsonUsage
Props & Parameters
| Param | Type | Default | Description |
|---|---|---|---|
options.timeout | number | 2000 | Duration in milliseconds before isCopied automatically resets to false. |
options.onSuccess | (text: string) => void | — | Callback invoked when text is successfully written to clipboard. |
options.onError | (error: unknown) => void | — | Callback invoked when writing to clipboard fails or is denied. |
Returns
| Return | Type | Description |
|---|---|---|
isCopied | boolean | Reactive boolean state indicating whether text was copied within the active timeout window. |
copiedText | string | null | The most recently copied text string. |
copy | (text: string) => Promise<boolean> | Async function that writes provided text to browser clipboard. |
reset | () => void | Clears active timer and resets isCopied state back to idle immediately. |