useClipboard React hook in TypeScript
v1.0.0Added in 1.0.0
A React copy-to-clipboard hook with a copied flag that resets itself, and a fallback for pages without the Clipboard API.
Press Copy next to an IBAN, a tax ID or an invoice link: the icon turns into a check for the timeout and the last copied text shows below. Paste it anywhere to check. Set the timeout to 0 and the check stays until the next copy.
useClipboard is a free React hook in the React hooks and utilities category, written in TypeScript for React 19. It installs as source with npx wingo-ui@latest add use-clipboard.
Add to a project
Requirements: React 19, in Next.js (App Router or Pages), Vite or React Router. Installation guide · Next.js setup · Vite setup
npx wingo-ui@latest add use-clipboardUsage
Import useClipboard from @/hooks/use-clipboard.
import { Check, Copy } from "lucide-react"
import { useClipboard } from "@/hooks/use-clipboard"
export function CopyIban({ iban }: { iban: string }) {
const { copy, copied } = useClipboard()
return (
<button type="button" onClick={() => copy(iban)} aria-label={copied ? "Copied" : "Copy IBAN"}>
{copied ? <Check /> : <Copy />}
</button>
)
}API
timeoutdefault2000numberHow long copied stays true after a copy, in ms. 0 keeps it until reset() or the next copy.
onCopy / onError(text: string) => void / (error: Error) => voidCalled after each copy; the latest callbacks are always used, so there is nothing to memoize.
useClipboard(options?: { timeout?: number; onCopy?; onError? }) => { copy(source: string | (() => string | Promise<string>), options?: { html?: string }): Promise<boolean>; copied: boolean; error: Error | null; text: string | null; reset(): void }copy takes a string or a function read at click time (the latest form value, a link fetched on demand) and resolves true on success. Pass html to put a rich version next to the plain text.
Parts
copyToClipboard
functionThe same copy without state, for event handlers: navigator.clipboard where it exists, a hidden textarea with execCommand on plain http and old webviews. Focus returns to where it was. Signature: (text: string, options?: { html?: string }) => Promise<boolean>.
Examples
Share link
copy takes a function, read at click time. The button label flips to Copied and a live region announces it.
Dependencies
No dependencies beyond React.
Files
- hooks/use-clipboard.ts
FAQ
Is the useClipboard hook free?
Yes. The source is free and needs no account: copy it from the Code tab or install it with the CLI.
How do I install useClipboard?
Run npx wingo-ui@latest add use-clipboard. With shadcn, run npx shadcn@latest add https://wingo-ui.com/r/use-clipboard.json.
See also
Used in
More React hooks and utilities
Articles using this component
- React QR Code with Logo: Rounded Modules, PNG and SVG
Component guides9 min read