(text: string)
| 10 | // Returns whether the copy succeeded, so callers only show their "copied" |
| 11 | // confirmation when the value actually reached the clipboard. |
| 12 | export async function copyToClipboard(text: string): Promise<boolean> { |
| 13 | // Preferred path: the async Clipboard API (secure contexts). |
| 14 | if (typeof navigator !== "undefined" && navigator.clipboard?.writeText) { |
| 15 | // oxlint-disable-next-line executor/no-try-catch-or-throw -- boundary: the Clipboard API rejects (permission denied, blur) and we recover via the legacy path |
| 16 | try { |
| 17 | await navigator.clipboard.writeText(text); |
| 18 | return true; |
| 19 | } catch { |
| 20 | // Permission denied / document not focused — fall through to the legacy |
| 21 | // path rather than failing the copy. |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | return legacyCopy(text); |
| 26 | } |
| 27 | |
| 28 | // Legacy fallback for non-secure origins (plain-HTTP self-host). Select the text |
| 29 | // via a document Range on a hidden node, then `execCommand("copy")`. |
no test coverage detected