This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #270 from gnosis/bug/safari-clipboard-btns
Bug #234: Click to copy buttons are not shown in safari
- Loading branch information
Showing
2 changed files
with
12 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |