mirror of
https://github.com/khairul169/garage-webui.git
synced 2025-04-27 22:39:31 +07:00
fix: copyToClipboard fix on non secure context
This commit is contained in:
parent
6ad70370d1
commit
7620f2cba1
@ -24,10 +24,27 @@ export const handleError = (err: unknown) => {
|
||||
};
|
||||
|
||||
export const copyToClipboard = async (text: string) => {
|
||||
let textArea: HTMLTextAreaElement | undefined;
|
||||
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
} else {
|
||||
textArea = document.createElement("textarea");
|
||||
textArea.value = text;
|
||||
|
||||
textArea.style.position = "absolute";
|
||||
textArea.style.left = "-999999px";
|
||||
|
||||
document.body.prepend(textArea);
|
||||
textArea.select();
|
||||
document.execCommand("copy");
|
||||
}
|
||||
|
||||
toast.success("Copied to clipboard");
|
||||
} catch (err) {
|
||||
handleError(err);
|
||||
} finally {
|
||||
textArea?.remove();
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user