aboutsummaryrefslogtreecommitdiff
path: root/src/utils/clipboard.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/clipboard.js')
-rw-r--r--src/utils/clipboard.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/utils/clipboard.js b/src/utils/clipboard.js
new file mode 100644
index 0000000..6d139c3
--- /dev/null
+++ b/src/utils/clipboard.js
@@ -0,0 +1,30 @@
+const fallbackCopyTextToClipboard = (text) => {
+ var textArea = document.createElement("textarea");
+ textArea.value = text;
+ textArea.style.top = "0";
+ textArea.style.left = "0";
+ textArea.style.position = "fixed";
+
+ document.body.appendChild(textArea);
+ textArea.focus();
+ textArea.select();
+
+ try {
+ document.execCommand('copy');
+ } catch (err) {
+ console.error('Fallback: Oops, unable to copy', err);
+ }
+
+ document.body.removeChild(textArea);
+}
+
+export const copyTextToClipboard = (text) => {
+ if (!navigator.clipboard) {
+ fallbackCopyTextToClipboard(text);
+ return;
+ }
+ navigator.clipboard.writeText(text).then(function () {
+ }, function (err) {
+ console.error('Async: Could not copy text: ', err);
+ });
+}