Files
fusion/packages/dashboard/app/utils/copyToClipboard.ts
gsxdsm c96a918591 feat(FN-4951): complete Step 1 — add shared clipboard utility
Fusion-Task-Id: FN-4951
Fusion-Task-Lineage: 25de66db-c8cf-463b-86f2-efff1bc22683
2026-05-18 03:10:52 -07:00

42 lines
1023 B
TypeScript

export async function copyTextToClipboard(text: string): Promise<boolean> {
if (
typeof navigator !== "undefined" &&
typeof window !== "undefined" &&
window.isSecureContext !== false &&
navigator.clipboard?.writeText
) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch {
// Fall through to execCommand fallback.
}
}
if (typeof document === "undefined" || !document.body) {
return false;
}
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.setAttribute("readonly", "");
textarea.style.position = "fixed";
textarea.style.left = "-9999px";
textarea.style.top = "0";
textarea.style.opacity = "0";
textarea.style.pointerEvents = "none";
document.body.appendChild(textarea);
try {
textarea.focus();
textarea.select();
textarea.setSelectionRange(0, text.length);
return document.execCommand("copy");
} catch {
return false;
} finally {
textarea.remove();
}
}