{"version":3,"file":"copyToClipboard.mjs","sources":["../../../src/utils/copyToClipboard.ts"],"sourcesContent":["import { type RefObject } from 'react';\n\ntype Ref = RefObject<HTMLElement | null>;\n\n/**\n * Copy text to the clipboard. Uses the modern Clipboard API in secure contexts and falls back\n * to a hidden `<textarea>` + `document.execCommand('copy')` otherwise.\n *  If `appendTo` is provided, the fallback textarea is appended to that element rather than\n * `document.body`, useful when copying from inside a focus-managed region (e.g. react-aria's\n * `FocusScope`) where elements outside the managed subtree can't receive focus.\n *\n * @param text The text to copy, or a promise of it. A pending promise is passed on unresolved so the\n * write is issued inside the click's user activation — Safari refuses one made after an `await`.\n * @param appendTo An optional ref used by the fallback method when the Clipboard API is not available.\n * @returns A promise that rejects when the copy fails.\n */\nexport async function copyTextToClipboard(text: string | Promise<string>, appendTo?: Ref): Promise<void> {\n  if (!navigator.clipboard || !window.isSecureContext) {\n    copyTextToClipboardFallback(await text, appendTo);\n    return;\n  }\n\n  if (typeof ClipboardItem === 'undefined' || !navigator.clipboard.write) {\n    await navigator.clipboard.writeText(await text);\n    return;\n  }\n\n  const type = 'text/plain';\n  // Promise.resolve is identity for a native promise, so a pending one stays pending here.\n  const clipboardItem = new ClipboardItem({ [type]: Promise.resolve(text) });\n  await navigator.clipboard.write([clipboardItem]);\n}\n\nfunction copyTextToClipboardFallback(text: string, appendTo?: Ref) {\n  // select() moves focus into the textarea and removing it drops focus to <body>, so anything\n  // watching blur (tooltips, menus) closes on copy. Put focus back where the caller had it.\n  const previouslyFocused = document.activeElement;\n  // Use a fallback method for browsers/contexts that don't support the Clipboard API.\n  // See https://web.dev/async-clipboard/#feature-detection.\n  // Use textarea so the user can copy multi-line content.\n  const textarea = document.createElement('textarea');\n  (appendTo?.current ?? document.body).appendChild(textarea);\n  textarea.value = text;\n  textarea.focus();\n  textarea.select();\n  document.execCommand('copy');\n  textarea.remove();\n\n  if (previouslyFocused instanceof HTMLElement) {\n    previouslyFocused.focus();\n  }\n}\n"],"names":[],"mappings":";AAg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