-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
48 lines (46 loc) · 1.22 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
export const copy = async (
text: string,
callback?: () => void
): Promise<boolean> => {
try {
if (window && navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
if (callback) {
callback();
}
return true;
} else {
// Fallback for older browsers
let textArea = document.createElement("textarea");
textArea.value = text;
// Avoid scrolling to bottom in Safari
textArea.style.position = "fixed";
textArea.style.left = "-9999px";
textArea.style.opacity = "0";
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
// Attempt to use execCommand for clipboard actions
let successful = document.execCommand("copy");
if (successful) {
if (callback) {
callback();
}
return true;
} else {
return false;
}
} catch (err) {
console.error("Error copying text:", err);
return false;
} finally {
// Cleanup
document.body.removeChild(textArea);
}
}
} catch (error) {
console.error("Error copying text:", error);
return false;
}
};