Change text copy to use fallback (#811)

This commit is contained in:
Salih Altun
2024-09-11 20:01:45 +03:00
committed by GitHub
parent dddfaf98e2
commit 267c859d3b
5 changed files with 51 additions and 18 deletions

View File

@@ -0,0 +1,25 @@
/**
* Progressively enhanced text copying
* https://web.dev/patterns/clipboard/copy-text
*/
async function copyText(text: string): Promise<void> {
if ("clipboard" in navigator) {
return navigator.clipboard.writeText(text);
} else {
const textArea = document.createElement("textarea");
textArea.value = text;
textArea.style.opacity = "0";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
const success = document.execCommand("copy");
document.body.removeChild(textArea);
if (success) {
return Promise.resolve();
} else {
return Promise.reject();
}
}
}
export { copyText };