From a15a8bf3c34c2fcdd8070ad8581de06f3d28309f Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Fri, 15 Nov 2019 18:29:55 +0400 Subject: [PATCH] Use polyfill instead of navigator.clipboard because it doesn't work in safari --- src/components/CopyBtn/index.jsx | 4 ---- src/utils/clipboard.js | 20 ++++++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/components/CopyBtn/index.jsx b/src/components/CopyBtn/index.jsx index dd076ffd3b..b401367c6b 100644 --- a/src/components/CopyBtn/index.jsx +++ b/src/components/CopyBtn/index.jsx @@ -31,10 +31,6 @@ type CopyBtnProps = { } const CopyBtn = ({ content, increaseZindex = false }: CopyBtnProps) => { - if (!navigator.clipboard) { - return null - } - const [clicked, setClicked] = useState(false) const classes = useStyles() const customClasses = increaseZindex ? { popper: classes.inreasedPopperZindex } : {} diff --git a/src/utils/clipboard.js b/src/utils/clipboard.js index d8ad191fad..7322f54c03 100644 --- a/src/utils/clipboard.js +++ b/src/utils/clipboard.js @@ -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() }