saleor-dashboard/src/hooks/useClipboard.ts
Patryk Andrzejewski 1d2eeb7592
Strict mode plugin (#3778)
* Stric mode plugin

* Update command
2023-06-21 11:28:00 +02:00

31 lines
714 B
TypeScript

// @ts-strict-ignore
import { useEffect, useRef, useState } from "react";
function useClipboard(): [boolean, (text: string) => void] {
const [copied, setCopyStatus] = useState(false);
const timeout = useRef(null);
const clear = () => {
if (timeout.current) {
clearTimeout(timeout.current);
timeout.current = null;
}
};
const copy = (text: string) => {
navigator.clipboard.writeText(text).then(() => {
setCopyStatus(true);
timeout.current = setTimeout(() => {
clear();
setCopyStatus(false);
}, 2000);
});
};
// Clear timeout after hook unmounting
useEffect(() => clear, []);
return [copied, copy];
}
export default useClipboard;