@@ -25,6 +25,15 @@ export default class extends React.PureComponent {
2525 }
2626 }
2727
28+ copyToClipboardFallback = ( textToCopy ) => {
29+ const textArea = document . createElement ( 'textarea' ) ;
30+ textArea . value = textToCopy ;
31+ document . body . appendChild ( textArea ) ;
32+ textArea . select ( ) ;
33+ document . execCommand ( 'copy' ) ;
34+ document . body . removeChild ( textArea ) ;
35+ }
36+
2837 handleCopy = ( ) => {
2938 const { clickCallback, src, namespace } = this . props ;
3039
@@ -35,21 +44,13 @@ export default class extends React.PureComponent {
3544 ) ;
3645
3746 if ( navigator . clipboard ) {
38- navigator . clipboard . writeText ( textToCopy ) . catch ( err => {
47+ navigator . clipboard . writeText ( textToCopy ) . catch ( ( ) => {
3948 // Fallback for non-secure contexts (i.e. http)
40- const textArea = document . createElement ( 'textarea' ) ;
41- textArea . value = textToCopy ;
42- document . body . appendChild ( textArea ) ;
43- textArea . select ( ) ;
44- document . execCommand ( 'copy' ) ;
45- document . body . removeChild ( textArea ) ;
49+ copyToClipboardFallback ( textToCopy ) ;
4650 } ) ;
4751 } else {
48- console . error (
49- 'react-json-view error:' ,
50- 'navigator.clipboard not supported by this browser'
51- ) ;
52- return ;
52+ // Fallback for old browsers and test environments
53+ copyToClipboardFallback ( textToCopy ) ;
5354 } ;
5455
5556 this . copiedTimer = setTimeout ( ( ) => {
0 commit comments