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
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-resetFull Signature
UseCopyToClipboardReturn
Clipboard state and helper methods returned by the hook.
Property
Specification
copiedTextunion
Last text successfully copied to the clipboard, or
nullFull Signature
isCopiedunion
Whether text was recently copied (resets after
resetInterval)Full Signature
copyfunction
Writes the given string to the system clipboard
Full Signature
Parameters
textstringresetfunction
Clears
copiedText and resets isCopied to falseFull Signature