Skip to content

Commit ce5f832

Browse files
committed
refactor: simplify clipboard logic
1 parent 459d60b commit ce5f832

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

src/js/components/CopyToClipboard.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ export default class extends React.PureComponent {
2525
}
2626
}
2727

28-
copyToClipboardFallback = textToCopy => {
28+
toClipboard = async textToCopy => {
29+
if (navigator.clipboard) return navigator.clipboard.writeText(textToCopy)
2930
const textArea = document.createElement('textarea')
3031
textArea.value = textToCopy
32+
textArea.style.visibility = 'hidden'
3133
document.body.appendChild(textArea)
3234
textArea.select()
3335
document.execCommand('copy')
@@ -37,17 +39,7 @@ export default class extends React.PureComponent {
3739
handleCopy = () => {
3840
const { clickCallback, src, namespace } = this.props
3941

40-
const textToCopy = JSON.stringify(this.clipboardValue(src), null, ' ')
41-
42-
if (navigator.clipboard) {
43-
navigator.clipboard.writeText(textToCopy).catch(() => {
44-
// Fallback for non-secure contexts (i.e. http)
45-
this.copyToClipboardFallback(textToCopy)
46-
})
47-
} else {
48-
// Fallback for old browsers and test environments
49-
this.copyToClipboardFallback(textToCopy)
50-
}
42+
this.toClipboard(JSON.stringify(this.clipboardValue(src), null, ' '))
5143

5244
this.copiedTimer = setTimeout(() => {
5345
this.setState({
@@ -95,7 +87,7 @@ export default class extends React.PureComponent {
9587
}
9688

9789
render () {
98-
const { src, theme, hidden, rowHovered } = this.props
90+
const { theme, hidden, rowHovered } = this.props
9991
const style = Theme(theme, 'copy-to-clipboard').style
10092
let display = 'inline'
10193

0 commit comments

Comments
 (0)