Files
next-ai-draw-io/lib/clipboard.ts
T

23 lines
710 B
TypeScript
Raw Normal View History

/** Copy text, falling back to execCommand for non-secure contexts (HTTP) */
export async function copyText(text: string): Promise<boolean> {
try {
await navigator.clipboard.writeText(text)
return true
} catch {
const textarea = document.createElement("textarea")
textarea.value = text
textarea.style.position = "fixed"
textarea.style.left = "-9999px"
textarea.style.opacity = "0"
document.body.appendChild(textarea)
try {
textarea.select()
return document.execCommand("copy")
} catch {
return false
} finally {
document.body.removeChild(textarea)
}
}
}