-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f58627
commit a34acc3
Showing
3 changed files
with
34 additions
and
90 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
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// from https://stackoverflow.com/a/65996386 | ||
export const copyToClipboard = async (textToCopy: string) => { | ||
// Navigator clipboard api needs a secure context (https) | ||
if (navigator.clipboard && window.isSecureContext) { | ||
await navigator.clipboard.writeText(textToCopy); | ||
} else { | ||
// Use the 'out of viewport hidden text area' trick | ||
const textArea = document.createElement('textarea'); | ||
textArea.value = textToCopy; | ||
|
||
// Move textarea out of the viewport so it's not visible | ||
textArea.style.position = 'absolute'; | ||
textArea.style.left = '-999999px'; | ||
|
||
document.body.prepend(textArea); | ||
textArea.select(); | ||
|
||
try { | ||
document.execCommand('copy'); | ||
} catch (error) { | ||
console.error(error); | ||
} finally { | ||
textArea.remove(); | ||
} | ||
} | ||
}; |