Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
Merge pull request #270 from gnosis/bug/safari-clipboard-btns
Browse files Browse the repository at this point in the history
Bug #234: Click to copy buttons are not shown in safari
  • Loading branch information
mmv08 authored Nov 18, 2019
2 parents ceef0e1 + a15a8bf commit 8688c2b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
4 changes: 0 additions & 4 deletions src/components/CopyBtn/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ type CopyBtnProps = {
}

const CopyBtn = ({ content, increaseZindex = false }: CopyBtnProps) => {
if (!navigator.clipboard) {
return null
}

const [clicked, setClicked] = useState<boolean>(false)
const classes = useStyles()
const customClasses = increaseZindex ? { popper: classes.inreasedPopperZindex } : {}
Expand Down
20 changes: 12 additions & 8 deletions src/utils/clipboard.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// @flow

export const copyToClipboard = (text: string) => {
if (!navigator.clipboard) {
return
}
export const copyToClipboard = (text: string): void => {
const range = document.createRange()
range.selectNodeContents(document.body)
document.getSelection().addRange(range)

try {
navigator.clipboard.writeText(text)
} catch (err) {
console.error(err.message)
function listener(e: ClipboardEvent) {
e.clipboardData.setData('text/plain', text)
e.preventDefault()
}
document.addEventListener('copy', listener)
document.execCommand('copy')
document.removeEventListener('copy', listener)

document.getSelection().removeAllRanges()
}

0 comments on commit 8688c2b

Please sign in to comment.