From 369bca90fc4051da8daff89b78b21ceac84b10cf Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Sat, 31 Jul 2021 20:43:02 -0400 Subject: [PATCH] feat: use navigator clipboard to copy text is possible --- helpers/utils/clipboard.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/helpers/utils/clipboard.ts b/helpers/utils/clipboard.ts index 44dbcbf0..6516652d 100644 --- a/helpers/utils/clipboard.ts +++ b/helpers/utils/clipboard.ts @@ -5,10 +5,14 @@ * @param content The content to be copied */ export function copyToClipboard(content: string) { - const dummy = document.createElement("textarea") - document.body.appendChild(dummy) - dummy.value = content - dummy.select() - document.execCommand("copy") - document.body.removeChild(dummy) + if (navigator.clipboard) { + navigator.clipboard.writeText(content) + } else { + const dummy = document.createElement("textarea") + document.body.appendChild(dummy) + dummy.value = content + dummy.select() + document.execCommand("copy") + document.body.removeChild(dummy) + } }