| 1 |
export async function copyTextToClipboard(text: string): Promise<boolean> { |
| 2 |
if (typeof window === 'undefined' || typeof document === 'undefined') { |
| 3 |
return false |
| 4 |
} |
| 5 |
|
| 6 |
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) { |
| 7 |
try { |
| 8 |
await navigator.clipboard.writeText(text) |
| 9 |
return true |
| 10 |
} catch { |
| 11 |
// Fall through to legacy fallback |
| 12 |
} |
| 13 |
} |
| 14 |
|
| 15 |
const textarea = document.createElement('textarea') |
| 16 |
textarea.value = text |
| 17 |
textarea.style.position = 'fixed' |
| 18 |
textarea.style.opacity = '0' |
| 19 |
document.body.appendChild(textarea) |
| 20 |
textarea.select() |
| 21 |
|
| 22 |
try { |
| 23 |
return document.execCommand('copy') |
| 24 |
} catch { |
| 25 |
return false |
| 26 |
} finally { |
| 27 |
textarea.remove() |
| 28 |
} |
| 29 |
} |
| 30 |
|