/
dedaMazai
/
reactGoodPattern
Обзор
Документация
Войти
/
dedaMazai
/
reactGoodPattern
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/shared/lib/hooks/useCopy/useCopy.ts
38 строк
1 KB
Andrei Chipizubov
fix
23 май 2023, 17:48
23 май 2023, 17:48
2c8152c
Код
Авторство
О чём код?
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]; };