File tree 1 file changed +18
-5
lines changed
site/src/components/CopyButton
1 file changed +18
-5
lines changed Original file line number Diff line number Diff line change @@ -29,16 +29,29 @@ 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 . value = text
38
+ document . body . appendChild ( input )
39
+ input . focus ( )
40
+ input . select ( )
41
+ const result = document . execCommand ( "copy" )
42
+ document . body . removeChild ( input )
43
+ if ( result ) {
44
+ setIsCopied ( true )
45
+ window . setTimeout ( ( ) => {
46
+ setIsCopied ( false )
47
+ } , 1000 )
48
+ } else {
49
+ const wrappedErr = new Error ( "copyToClipboard: failed to copy text to clipboard" )
50
+ if ( err instanceof Error ) {
51
+ wrappedErr . stack = err . stack
52
+ }
53
+ console . error ( wrappedErr )
40
54
}
41
- console . error ( wrappedErr )
42
55
}
43
56
}
44
57
You can’t perform that action at this time.
0 commit comments