Copy to your clipboard by just clicking on the button ->
Installation
Copy and paste the following code into your project.
'use client';
import * as React from 'react';
export function useCopyToClipboard({ timeout = 2000, onCopy }: { timeout?: number; onCopy?: () => void } = {}) {
const [isCopied, setIsCopied] = React.useState(false);
const copyToClipboard = (value: string) => {
if (typeof window === 'undefined' || !navigator.clipboard.writeText) {
return;
}
if (!value) return;
navigator.clipboard.writeText(value).then(() => {
setIsCopied(true);
if (onCopy) {
onCopy();
}
if (timeout !== 0) {
setTimeout(() => {
setIsCopied(false);
}, timeout);
}
}, console.error);
};
return { isCopied, copyToClipboard };
}
Usage
import { useCopyToClipboard } from '@kit/utils/hooks/use-copy-to-clipboard';const { copyToClipboard, isCopied } = useCopyToClipboard();
return (
<Button aria-label="Copy to clipboard" onClick={() => copyToClipboard(TEXT_TO_COPY)}>
{isCopied ? <Check className="size-3.5" /> : <Copy className="size-3.5" />}
</Button>
);API Reference
Nothing special here.
Device Mockup
An animated mockup to switch and render macOS, iPhone and Android devices.
Delay
Delay your react states.
How is this guide?
Last updated on 1/29/2026