uniswap-interface-uncensored/src/hooks/useCopyClipboard.ts
2021-02-16 08:46:17 +00:00

27 lines
633 B
TypeScript

import copy from 'copy-to-clipboard'
import { useCallback, useEffect, useState } from 'react'
export default function useCopyClipboard(timeout = 500): [boolean, (toCopy: string) => void] {
const [isCopied, setIsCopied] = useState(false)
const staticCopy = useCallback((text) => {
const didCopy = copy(text)
setIsCopied(didCopy)
}, [])
useEffect(() => {
if (isCopied) {
const hide = setTimeout(() => {
setIsCopied(false)
}, timeout)
return () => {
clearTimeout(hide)
}
}
return undefined
}, [isCopied, setIsCopied, timeout])
return [isCopied, staticCopy]
}