(
text: string,
deps: CopyToClipboardDeps = {}
)
| 35 | export const SCREENSHOT_COPY_SUCCESS_DURATION = 4000; |
| 36 | |
| 37 | export async function copyToClipboard( |
| 38 | text: string, |
| 39 | deps: CopyToClipboardDeps = {} |
| 40 | ): Promise<boolean> { |
| 41 | const clipboard = deps.clipboard ?? navigator.clipboard; |
| 42 | if (clipboard) { |
| 43 | try { |
| 44 | await clipboard.writeText(text); |
| 45 | return true; |
| 46 | } catch { |
| 47 | // Fall through to the textarea copy path for older or restricted browsers. |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | const documentRef = deps.document ?? document; |
| 52 | const textarea = documentRef.createElement('textarea'); |
| 53 | textarea.value = text; |
| 54 | textarea.style.position = 'fixed'; |
| 55 | textarea.style.opacity = '0'; |
| 56 | documentRef.body.appendChild(textarea); |
| 57 | textarea.select(); |
| 58 | |
| 59 | try { |
| 60 | const execCommand = deps.execCommand ?? documentRef.execCommand.bind(documentRef); |
| 61 | return execCommand('copy'); |
| 62 | } catch { |
| 63 | return false; |
| 64 | } finally { |
| 65 | documentRef.body.removeChild(textarea); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | export async function copyWithFeedback( |
| 70 | text: string, |
no test coverage detected