shadimage
Hookagain
hookuse-copy-to-clipboardsource: ok · refreshed

Use Copy To Clipboard

npx shadcn add "https://hookagain.vercel.app/r/use-copy-to-clipboard.json"
free

Where it goes

kanban

Tags auto-generated by rules on 9/24/2026 — empty means no evidence, not lack of support.

Files1 · source cached 10/2/2026, 1:58:23 AM

hooks/use-copy-to-clipboard.ts
import { useState } from "react";

export function useCopyToClipboard({
	timeout = 2000,
	onCopy,
}: {
	timeout?: number;
	onCopy?: (text: string) => void;
} = {}) {
	const [isCopied, setIsCopied] = useState(false);

	const copyToClipboard = (value: string) => {
		if (typeof window === "undefined" || !navigator.clipboard.writeText) {
			return;
		}

		if (!value) return;

		navigator.clipboard.writeText(value).then(() => {
			setIsCopied(true);
			onCopy?.(value);
			setTimeout(() => {
				setIsCopied(false);
			}, timeout);
		}, console.error);
	};

	return { isCopied, copyToClipboard };
}

More from Hookagain

Browse all components in this library →