File tree 1 file changed +14
-5
lines changed
site/src/components/CopyButton
1 file changed +14
-5
lines changed Original file line number Diff line number Diff line change @@ -29,16 +29,25 @@ export const CopyButton: React.FC<CopyButtonProps> = ({
29
29
try {
30
30
await window . navigator . clipboard . writeText ( text )
31
31
setIsCopied ( true )
32
-
33
32
window . setTimeout ( ( ) => {
34
33
setIsCopied ( false )
35
34
} , 1000 )
36
35
} catch ( err ) {
37
- const wrappedErr = new Error ( "copyToClipboard: failed to copy text to clipboard" )
38
- if ( err instanceof Error ) {
39
- wrappedErr . stack = err . stack
36
+ const input = document . createElement ( "input" )
37
+ input . hidden = true
38
+ input . value = text
39
+ document . body . appendChild ( input )
40
+ input . focus ( )
41
+ input . select ( )
42
+ const result = document . execCommand ( "copy" )
43
+ document . body . removeChild ( input )
44
+ if ( ! result ) {
45
+ const wrappedErr = new Error ( "copyToClipboard: failed to copy text to clipboard" )
46
+ if ( err instanceof Error ) {
47
+ wrappedErr . stack = err . stack
48
+ }
49
+ console . error ( wrappedErr )
40
50
}
41
- console . error ( wrappedErr )
42
51
}
43
52
}
44
53
You can’t perform that action at this time.
0 commit comments