ui/hooks/copy-to-clipboard
Copy to Clipboard
hookclipboardfeedback
Copy-to-clipboard state machine. Writes text to the clipboard and exposes an idle, copied, or failed status that resets after a delay.
tsx
Installation
$pnpm exec dgadd add ui/copy-to-clipboard
[Installs to]src/hooks/use-copy-to-clipboard.ts[Item]ui/copy-to-clipboard
dgadd ships in @diffgazer/add on npm: pnpm add -D @diffgazer/add puts it on pnpm exec, and npx @diffgazer/add runs it without the install.
$npx shadcn add https://r.b4r7.dev/r/ui/copy-to-clipboard.json
[Copies to]src/hooks/use-copy-to-clipboard.ts
$npm install @diffgazer/ui @diffgazer/keys
[Import]@diffgazer/ui/hooks/copy-to-clipboard
UI components require Tailwind CSS v4. Local copy mode imports src/styles/styles.css; package mode uses @diffgazer/ui CSS from the installed package.
Parameters
Returns
UseCopyToClipboardResultCurrent copy status plus an async copy command.
Notes
Reset behavior
Each copy attempt restarts the reset timer. The pending timer is cleared when the component unmounts.
Clipboard writer
By default the hook calls navigator.clipboard.writeText. Pass write when your environment needs a custom clipboard implementation.
Source
Highlighted source loads after this disclosure opens. Browse the source repository.