mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2026-10-24 23:06:33 +08:00
23 lines
513 B
TypeScript
23 lines
513 B
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
export function useCopyText() {
|
|
const [isCopied, setIsCopied] = useState(false);
|
|
|
|
const copyText = (text: string) => {
|
|
navigator.clipboard.writeText(text).then();
|
|
setIsCopied(true);
|
|
};
|
|
|
|
useEffect(() => {
|
|
let timeout: ReturnType<typeof setTimeout>;
|
|
if (isCopied) {
|
|
timeout = setTimeout(() => {
|
|
setIsCopied(false);
|
|
}, 2000);
|
|
}
|
|
return () => clearTimeout(timeout);
|
|
}, [isCopied]);
|
|
|
|
return { isCopied, copyText };
|
|
}
|