{"version":3,"file":"useFocusReturn.mjs","names":[],"sources":["../../../../../src/components/Accessibility/hooks/useFocusReturn.ts"],"sourcesContent":["import { useCallback, useRef } from 'react';\n\nexport type ReserveFocusReturnOptions = {\n  /**\n   * Element focus returns to. Defaults to the current `document.activeElement`. A non-focusable\n   * value or `document.body` reserves nothing.\n   */\n  target?: HTMLElement | null;\n};\n\nexport type UseFocusReturnResult = {\n  /** Captures the element focus should return to: `options.target`, or the active element. */\n  reserve: (options?: ReserveFocusReturnOptions) => void;\n  /**\n   * Clears the reservation and moves focus back to the reserved element if it is still connected.\n   * Focuses synchronously; defer any announcement that must be spoken after the focus announcement.\n   */\n  restore: () => void;\n};\n\nconst isFocusableElement = (value: Element | null): value is HTMLElement =>\n  value instanceof HTMLElement && typeof value.focus === 'function';\n\n/**\n * Focus restoration for transient surfaces: `reserve()` before moving focus into the surface,\n * `restore()` when it completes, without the surface knowing which element had focus.\n *\n * Each hook instance holds its own reservation, so concurrent or nested surfaces restore their own\n * origin.\n */\nexport const useFocusReturn = (): UseFocusReturnResult => {\n  const reservedTargetRef = useRef<HTMLElement | null>(null);\n\n  const reserve = useCallback((options?: ReserveFocusReturnOptions) => {\n    const candidate = options?.target ?? (document.activeElement as Element | null);\n    reservedTargetRef.current =\n      isFocusableElement(candidate) && candidate !== document.body ? candidate : null;\n  }, []);\n\n  const restore = useCallback(() => {\n    const target = reservedTargetRef.current;\n    reservedTargetRef.current = null;\n    if (target?.isConnected) target.focus();\n  }, []);\n\n  return { reserve, restore };\n};\n"],"mappings":";;AAoBA,IAAM,sBAAsB,UAC1B,iBAAiB,eAAe,OAAO,MAAM,UAAU;;;;;;;;AASzD,IAAa,uBAA6C;CACxD,MAAM,oBAAoB,OAA2B,IAAI;CAczD,OAAO;EAAE,SAZO,aAAa,YAAwC;GACnE,MAAM,YAAY,SAAS,UAAW,SAAS;GAC/C,kBAAkB,UAChB,mBAAmB,SAAS,KAAK,cAAc,SAAS,OAAO,YAAY;EAC/E,GAAG,CAAC,CAQK;EAAS,SANF,kBAAkB;GAChC,MAAM,SAAS,kBAAkB;GACjC,kBAAkB,UAAU;GAC5B,IAAI,QAAQ,aAAa,OAAO,MAAM;EACxC,GAAG,CAAC,CAEc;CAAQ;AAC5B"}