Skip to Content
ohooks

useCopyToClipboard

Copies text to the system clipboard and tracks the copied state with an optional auto-reset timer.

Loading preview...

The useCopyToClipboard hook writes text to the system clipboard via the Clipboard API (with a legacy execCommand('copy') fallback), tracks the last copied string, and optionally resets isCopied after a timeout.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useCopyToClipboard.json

Usage

Examples

Copy Button with Temporary Feedback

Set resetInterval so isCopied automatically reverts to false after a few seconds.

Handling Errors and Inspecting the Copied Value

Read error and copiedText or await the boolean promise returned by copy to trigger custom notifications.

API Reference

UseCopyToClipboardOptions

Configuration options for the clipboard hook.

Property
Specification
resetInterval?
uniondefault:2000
Time in milliseconds before isCopied resets to false, or null to disable auto-reset

UseCopyToClipboardReturn

Clipboard state and helper methods returned by the hook.

Property
Specification
copiedText
union
Last text successfully copied to the clipboard, or null
isCopied
union
Whether text was recently copied (resets after resetInterval)
copy
function
Writes the given string to the system clipboard
reset
function
Clears copiedText and resets isCopied to false