useCopyToClipboard
Copy text with a temporary copied state and a fallback.
Writes text with the Clipboard API and falls back to a hidden textarea (restoring focus and selection) where the API is blocked. isCopied resets after the timeout. Merges two earlier implementations: copyToClipboard and copy are the same function.
Usage
Examples
Copy a share link
One hook, several copy buttons
Reset pending feedback
API
2 options
3 return values
Accessibility
Keep the button name stable ("Copy link") and announce the result in a polite status region; swapping only the icon is not announced.
Notes
- CodeBlock already has a copy button; use this hook for custom copy actions.
Related
CodeBlockCodeBlock renders syntax-highlighted source with an optional header, copy button, line numbers, highlighted and focused lines, diffs, folding and line selection.10 examplesuseEventListenerDeclarative DOM event listener with cleanup.useHotkeyKeyboard shortcuts with platform-aware mod keys.useHoverPointer hover state with props to spread, ignoring touch.useMousePositionTrack viewport, page and optional element-relative pointer coordinates.useOnClickOutsideRun a callback when a press lands outside one or more elements.