-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: zsnmwy <szlszl35622@gmail.com>
- Loading branch information
Showing
3 changed files
with
67 additions
and
17 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,23 @@ | ||
export const copyToClipboard = (textToCopy: string) => { | ||
// navigator clipboard api needs a secure context (https) | ||
if (navigator.clipboard && window.isSecureContext) { | ||
// navigator clipboard api method' | ||
return navigator.clipboard.writeText(textToCopy); | ||
} | ||
// text area method | ||
const textArea = document.createElement('textarea'); | ||
textArea.value = textToCopy; | ||
// make the textarea out of viewport | ||
textArea.style.position = 'fixed'; | ||
textArea.style.left = '-999999px'; | ||
textArea.style.top = '-999999px'; | ||
document.body.appendChild(textArea); | ||
textArea.focus(); | ||
textArea.select(); | ||
return new Promise<void>((res, rej) => { | ||
// here the magic happens | ||
// eslint-disable-next-line no-unused-expressions | ||
document.execCommand('copy') ? res() : rej(); | ||
textArea.remove(); | ||
}); | ||
}; |