Created
October 12, 2021 11:05
-
-
Save charisTheo/e011c4dae966e4e4cbeaa7b48c5fa9b8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| export async function copyToClipboard (text) { | |
| try { | |
| // try to use Clipboard API | |
| await navigator.clipboard.writeText(text); | |
| return true | |
| } catch (_) { | |
| // Clipboard API is not supported | |
| const el = document.createElement('textarea') | |
| el.value = text | |
| document.body.appendChild(el) | |
| el.select() | |
| const result = document.execCommand('copy') | |
| document.body.removeChild(el) | |
| return result === 'unsuccessful' ? false : true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment