File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { useCallback , useState } from 'react'
2
+
3
+ const copy = toCopy => {
4
+ const el = document . createElement ( `textarea` )
5
+ el . value = toCopy
6
+ el . setAttribute ( `readonly` , `` )
7
+ el . style . position = `absolute`
8
+ el . style . left = `-9999px`
9
+ document . body . appendChild ( el )
10
+ el . select ( )
11
+ document . execCommand ( `copy` )
12
+ document . body . removeChild ( el )
13
+ }
14
+
15
+ const useCopyToClipboard = ( ) => {
16
+ const [ hasCopied , setHasCopied ] = useState ( false )
17
+
18
+ const copyToClipboard = useCallback (
19
+ toCopy => {
20
+ if ( hasCopied ) return
21
+ copy ( toCopy )
22
+ setHasCopied ( true )
23
+ setTimeout ( ( ) => {
24
+ setHasCopied ( false )
25
+ } , 2000 )
26
+ } ,
27
+ [ hasCopied ]
28
+ )
29
+
30
+ return { hasCopied, copyToClipboard }
31
+ }
32
+
33
+ export default useCopyToClipboard
You can’t perform that action at this time.
0 commit comments