@knitui/hooks
useClipboard
Copy-to-clipboard state — port of Mantine's useClipboard. Guarded for native/SSR: when navigator.clipboard is unavailable it records an error instead of assuming a global. copied flips true for timeout ms after a successful copy. Backs CopyButton.
Import
import { useClipboard } from "@knitui/hooks";Signature
export function useClipboard({ timeout = 2000, }: UseClipboardOptions = {}): UseClipboardReturnValueExported types: UseClipboardOptions, UseClipboardReturnValue
Copying a string is one line; the interaction around it is not. Users need
confirmation that the copy happened, that confirmation has to time out, and the
underlying navigator.clipboard.writeText returns a promise that can reject —
denied permission, a non-secure origin, no clipboard API at all.
useClipboard wraps all of that into { copy, reset, copied, error }.
copied flips to true after a successful write and back to false after
timeout ms (2000 by default), which is exactly the lifetime of a "Copied!"
label. Calling copy again restarts the timer rather than stacking timers, and
the pending timer is cleared on unmount so a copy just before navigation cannot
set state on a gone component.
copy never throws. A rejected write lands in error and leaves copied
false, so the UI can show a failure state instead of lying about success.
reset clears both.
When to use it
- A copy button for a token, share link, code snippet or colour value.
- Any "copy" affordance that needs a transient confirmation state.
- When you want the failure path visible rather than swallowed.
Notes
- There is no
.nativevariant. The hook checks fornavigator.clipboardand, when it is missing — React Native, and server rendering — setserrortouseClipboard: navigator.clipboard is not supportedinstead of assuming the global exists. It degrades rather than crashing, but it does not copy on native. CopyButtonis built on this hook and hands{ copied, copy }to its render prop, so reach for the component first and the hook only when you need the state somewhere else.