useCopyToClipboard
Copy text to the clipboard
4a5e1e8e-37e8-4154-82e9-6c53e0cdb567
Installation
Copy-paste the hook
Copy and paste the hook code in a .ts
file.
Usage
Details
Clipboard Copying Mechanism
- The hook provides a seamless way to copy text to the clipboard using the modern
navigator.clipboard
API when available. This ensures a secure and reliable copying experience. - If the
navigator.clipboard
API is not supported (e.g., in older browsers), the hook falls back to an old-school method using a temporary<textarea>
element and thedocument.execCommand('copy')
method.
State Management
isCopied
: A boolean state that indicates whether the text has been successfully copied to the clipboard. This can be used to provide user feedback (e.g., showing a "Copied!" message).error
: An optional state that captures any errors that occur during the copy operation. This allows for better error handling and user notifications.
Timeout for Resetting State
- The hook includes a
timeoutDuration
parameter (defaulting to 1000 milliseconds) that determines how long theisCopied
state remainstrue
before resetting tofalse
. This feature helps in providing temporary feedback to users after a copy action. - The timeout can be customized by passing a different value when using the hook, allowing for flexibility based on user interface requirements.
Return Value
- The hook returns an object containing:
isCopied
: A boolean indicating whether the text has been copied successfully.copyToClipboard
: A function that takes a string as an argument and attempts to copy it to the clipboard. It returns a promise that resolves to a boolean indicating the success of the operation.error
: An optional error object that can be used to handle any errors that occur during the copy operation.