From 7cc28b10a02e846b5abb7969bc4d416fd34d7518 Mon Sep 17 00:00:00 2001 From: zees-dev <63374656+zees-dev@users.noreply.github.com> Date: Mon, 6 Dec 2021 10:01:54 +1300 Subject: [PATCH] fallback to depracted copy text if clipboard api not available (#6200) (#6218) --- .../components/Button/CopyButton/CopyButton.tsx | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/portainer/components/Button/CopyButton/CopyButton.tsx b/app/portainer/components/Button/CopyButton/CopyButton.tsx index f4d33051b..fc5bca888 100644 --- a/app/portainer/components/Button/CopyButton/CopyButton.tsx +++ b/app/portainer/components/Button/CopyButton/CopyButton.tsx @@ -32,7 +32,19 @@ export function CopyButton({ function onClick() { // https://developer.mozilla.org/en-US/docs/Web/API/Clipboard // https://caniuse.com/?search=clipboard - navigator.clipboard.writeText(copyText); + if (navigator.clipboard) { + navigator.clipboard.writeText(copyText); + } else { + // https://stackoverflow.com/a/57192718 + const inputEl = document.createElement('input'); + inputEl.value = copyText; + inputEl.type = 'text'; + document.body.appendChild(inputEl); + inputEl.select(); + document.execCommand('copy'); + inputEl.type = 'hidden'; + document.body.removeChild(inputEl); + } setIsFading(true); }