/
dedaMazai
/
reactGoodPattern
Обзор
Документация
Войти
/
dedaMazai
/
reactGoodPattern
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
moduleFederation/container/src/shared/lib/hooks/useCopy/useCopy.ts
38 строк
1 KB
Andrey Chipizubov
add microFronts
25 май 2023, 01:05
25 май 2023, 01:05
ec973d2
Код
Авторство
О чём код?
import React from "react"; type CopiedValue = string | null; type CopyFn = (text: string) => Promise<boolean>; export const useCopyToClipboard = (): [CopiedValue, CopyFn, boolean] => { const [copiedText, setCopiedText] = React.useState<CopiedValue>(null); const [isSuccess, setIsSuccess] = React.useState(false); React.useEffect(() => { if (isSuccess) { setTimeout(() => { setIsSuccess(false); }, 800); } }, [isSuccess]); const copy: CopyFn = React.useCallback(async (text) => { if (!navigator?.clipboard) { console.warn("Clipboard not supported"); return false; } try { await navigator.clipboard.writeText(text); setCopiedText(text); setIsSuccess(true); return true; } catch (error) { console.warn("Copy failed", error); setCopiedText(null); setIsSuccess(false); return false; } }, []); return [copiedText, copy, isSuccess]; };