( text: string, options?: IOptions )
| 9 | } |
| 10 | |
| 11 | export default function useCopyClipboard( |
| 12 | text: string, |
| 13 | options?: IOptions |
| 14 | ): [boolean, () => Promise<void>] { |
| 15 | const [isCopied, setIsCopied] = useState(false) |
| 16 | const successDuration = options && options.successDuration |
| 17 | |
| 18 | useEffect(() => { |
| 19 | if (isCopied && successDuration) { |
| 20 | const id = setTimeout(() => { |
| 21 | setIsCopied(false) |
| 22 | }, successDuration) |
| 23 | |
| 24 | return () => { |
| 25 | clearTimeout(id) |
| 26 | } |
| 27 | } |
| 28 | }, [isCopied, successDuration]) |
| 29 | |
| 30 | return [ |
| 31 | isCopied, |
| 32 | async () => { |
| 33 | try { |
| 34 | await navigator.clipboard.writeText(text) |
| 35 | setIsCopied(true) |
| 36 | } catch { |
| 37 | setIsCopied(false) |
| 38 | } |
| 39 | }, |
| 40 | ] |
| 41 | } |
nothing calls this directly
no outgoing calls
no test coverage detected