import React from 'react' ; import { getWindow } from '../helpers/window' ; import copy from 'copy-to-clipboard' ; example function useCopyToClipboard ( { timeout = 1000 } = { } ) { const environment = getWindow ( ) ; const [ copied , setCopied ] = React . useState ( false ) ; function handleCopy ( string ) { copy ( string ) ; setCopied ( true ) ; } React . useEffect ( ( ) => { let timer ; if ( copied ) { timer = environment . setTimeout ( ( ) => setCopied ( false ) , timeout ) ; } return ( ) => { environment . clearTimeout ( timer ) ; } ; } , [ copied , timeout ] ) ; return { copy : handleCopy , copied , } ; } export default useCopyToClipboard ;