se-design 1.0.83-dev.5 → 1.0.84-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"index72.js","sources":["../src/utils/a11y/useFocusTrap.ts"],"sourcesContent":["import { useLayoutEffect, useRef } from 'react';\nimport type { MutableRefObject, RefObject } from 'react';\nimport { getFocusableElements, getFirstFocusableElement } from './focusableElements';\n\nexport interface UseFocusTrapOptions<T extends HTMLElement = HTMLElement> {\n /**\n * Whether the focus trap is active.\n */\n enabled: boolean;\n /**\n * Container element ref to trap focus within.\n */\n containerRef: React.RefObject<T | null>;\n /**\n * Whether to restore focus to the element that had focus before trap activated.\n * Default: true\n */\n restoreFocus?: boolean;\n /**\n * Initial focus target when trap activates.\n * - 'first': Focus first focusable element (default)\n * - 'container': Focus the container itself\n * - 'none': Skip initial focus — browser handles it (e.g. autofocus attribute)\n * - CSS selector: Focus element matching selector\n * - HTMLElement: Focus this specific element\n */\n initialFocus?: 'first' | 'container' | 'none' | string | HTMLElement;\n /**\n * Explicit element to restore focus to when the trap deactivates.\n * Overrides the automatic trigger capture (_lastInteractedElement / document.activeElement).\n * Use when the opener is known at call-site (e.g. a ref on the toggle button).\n */\n returnFocusRef?: React.RefObject<HTMLElement | null>;\n /**\n * Additional container refs that are logically part of this focus trap\n * (e.g., portal-rendered content from Popover/Dropdown). Focus moving to\n * these containers will NOT trigger the safety net redirect.\n */\n portalContainerRefs?: MutableRefObject<RefObject<HTMLElement | null>[]>;\n}\n\nexport interface UseFocusTrapReturn {\n /**\n * Ref to the element that had focus before trap activated.\n * Useful for manual focus restoration if needed.\n */\n triggerRef: React.MutableRefObject<HTMLElement | null>;\n}\n\n// Module-level trigger tracking: React's commit phase is bottom-up, so by the\n// time useFocusTrap's useLayoutEffect runs, document.activeElement may already\n// be the autoFocus element inside the modal rather than the opener button.\n// We capture the last interacted element via pointerdown/keydown listeners\n// (which fire before any React state update commits) to work around this.\nlet _lastInteractedElement: HTMLElement | null = null;\n\nfunction _onPointerDown(e: PointerEvent): void {\n // Walk composedPath to find the nearest focusable element being pressed.\n const path = e.composedPath();\n for (const node of path) {\n if (node instanceof HTMLElement && node.tabIndex >= 0) {\n _lastInteractedElement = node;\n return;\n }\n }\n}\n\nfunction _onKeyDown(e: KeyboardEvent): void {\n // Enter/Space on a focused button will synthesise a click — capture before that.\n if (e.key === 'Enter' || e.key === ' ') {\n _lastInteractedElement = document.activeElement as HTMLElement;\n }\n}\n\nif (typeof document !== 'undefined') {\n document.addEventListener('pointerdown', _onPointerDown, true);\n document.addEventListener('keydown', _onKeyDown, true);\n}\n\n/**\n * Returns (and clears) the last element interacted with before a React commit.\n * Used by SidebarOverlay complementary mode to seed its trigger ref without\n * relying on document.activeElement timing.\n */\nexport function consumeLastInteractedElement(): HTMLElement | null {\n const el = _lastInteractedElement;\n _lastInteractedElement = null;\n return el;\n}\n\n// Focus anchor — explicit stable return-focus target, higher priority than\n// _lastInteractedElement. Auto-clears via setTimeout(0) to prevent a stale\n// anchor leaking to an unrelated open.\nlet _focusAnchor: HTMLElement | null = null;\nlet _focusAnchorClearFrame: ReturnType<typeof requestAnimationFrame> | null = null;\n\n/**\n * Sets an explicit focus-return anchor element.\n * Call this before dispatching an action that will open a modal or sidebar,\n * when the natural last-interacted element is not the right return target\n * (e.g. a popover menu item that will be unmounted when the popover closes).\n */\nexport function setFocusAnchor(el: HTMLElement | null): void {\n if (_focusAnchorClearFrame !== null) cancelAnimationFrame(_focusAnchorClearFrame);\n _focusAnchor = el;\n // Auto-clear if not consumed — prevents stale anchor leaking to unrelated opens.\n // rAF fires after React has committed and painted, giving useFocusTrap time to\n // consume the anchor before it is discarded.\n _focusAnchorClearFrame = requestAnimationFrame(() => {\n _focusAnchor = null;\n _focusAnchorClearFrame = null;\n });\n}\n\n/**\n * Returns (and clears) the focus anchor if one was set, otherwise null.\n * Used internally by useFocusTrap and SidebarOverlay.\n */\nexport function consumeFocusAnchor(): HTMLElement | null {\n if (_focusAnchorClearFrame !== null) {\n cancelAnimationFrame(_focusAnchorClearFrame);\n _focusAnchorClearFrame = null;\n }\n const el = _focusAnchor;\n _focusAnchor = null;\n return el;\n}\n\n/**\n * Resolve the initial focus target based on the initialFocus option.\n */\nfunction resolveInitialFocusTarget(\n container: HTMLElement,\n initialFocus: 'first' | 'container' | 'none' | string | HTMLElement\n): HTMLElement | null {\n if (initialFocus === 'none') return null;\n if (initialFocus === 'first') {\n return getFirstFocusableElement({ container }) || container;\n }\n if (initialFocus === 'container') {\n return container;\n }\n if (typeof initialFocus === 'string') {\n return container.querySelector<HTMLElement>(initialFocus);\n }\n if (initialFocus instanceof HTMLElement) {\n return initialFocus;\n }\n return null;\n}\n\n/**\n * Hook to trap focus within a container (for modals, dialogs, drawers).\n *\n * Implements WCAG 2.1 focus trap pattern:\n * - Moves focus into container on activation\n * - Wraps Tab/Shift+Tab navigation within container\n * - Restores focus to trigger element on deactivation\n * - Safety net: catches focus escaping via other means\n * - Handles autoFocus content: captures trigger before autoFocus fires\n * - Handles {isOpen && <Modal>} pattern: restores focus on unmount\n *\n * Note: For Escape key handling, use `useDismissOnEscape` hook separately.\n * This keeps focus trap (accessibility) separate from Escape handling (UX).\n *\n * @example\n * ```tsx\n * const MyModal = ({ isOpen, onClose }) => {\n * const containerRef = useRef<HTMLDivElement>(null);\n *\n * // Escape handling (UX)\n * useDismissOnEscape({\n * containerRef,\n * onDismiss: onClose,\n * enabled: isOpen\n * });\n *\n * // Focus trap (accessibility)\n * const { triggerRef } = useFocusTrap({\n * enabled: isOpen,\n * containerRef,\n * restoreFocus: true\n * });\n *\n * return (\n * <div ref={containerRef}>\n * <button>First</button>\n * <button>Second</button>\n * </div>\n * );\n * };\n * ```\n */\nexport function useFocusTrap<T extends HTMLElement = HTMLElement>({\n enabled,\n containerRef,\n restoreFocus = true,\n initialFocus = 'first',\n returnFocusRef,\n portalContainerRefs,\n}: UseFocusTrapOptions<T>): UseFocusTrapReturn {\n const triggerRef = useRef<HTMLElement | null>(null);\n const lastFocusedInContainer = useRef<HTMLElement | null>(null);\n\n // Focus management: save trigger, move focus into container on activate, restore on deactivate\n useLayoutEffect(() => {\n if (!enabled) {\n // Restore focus to trigger when trap deactivates\n if (restoreFocus && triggerRef.current) {\n const el = triggerRef.current;\n triggerRef.current = null;\n requestAnimationFrame(() => {\n if (el.isConnected) el.focus();\n });\n }\n return;\n }\n\n const container = containerRef.current;\n if (!container) return;\n\n // Resolve trigger: explicit returnFocusRef wins, then focusAnchor (explicitly set\n // by caller for two-step flows like popover menu → modal), then the pre-commit\n // interaction record, then document.activeElement as final fallback.\n const previousActiveElement = returnFocusRef?.current ?? consumeFocusAnchor() ?? _lastInteractedElement ?? (document.activeElement as HTMLElement);\n _lastInteractedElement = null;\n\n // iframes manage their own internal focus. document.activeElement only ever\n // resolves to the <iframe> element from the parent doc — calling .focus()\n // on any parent-doc element forcibly blurs the iframe content (kills caret\n // in textareas, cancels native <select> dropdowns). Skip capture so the\n // auto-focus on open and focus-restore on close short-circuit on null trigger.\n if (previousActiveElement?.tagName === 'IFRAME') {\n triggerRef.current = null;\n return;\n }\n\n triggerRef.current = previousActiveElement;\n\n // Only move initial focus if autoFocus hasn't already placed it inside the container.\n if (!container.contains(document.activeElement)) {\n requestAnimationFrame(() => {\n resolveInitialFocusTarget(container, initialFocus)?.focus();\n });\n }\n\n // Restore focus on unmount while enabled (covers {isOpen && <Modal>} pattern\n // where the component unmounts before enabled can transition true → false)\n return () => {\n if (restoreFocus && triggerRef.current) {\n const el = triggerRef.current;\n triggerRef.current = null;\n requestAnimationFrame(() => {\n if (el.isConnected) el.focus();\n });\n }\n };\n }, [enabled, containerRef, restoreFocus, initialFocus]);\n\n // Focus trap: Tab wrapping (only when enabled)\n useLayoutEffect(() => {\n if (!enabled) return;\n\n const container = containerRef.current;\n if (!container) return;\n\n const handleKeyDown = (e: KeyboardEvent) => {\n // Tab wrapping\n if (e.key === 'Tab') {\n const focusables = getFocusableElements({ container });\n\n if (focusables.length === 0) {\n e.preventDefault();\n container.focus();\n return;\n }\n\n const first = focusables[0];\n const last = focusables[focusables.length - 1];\n const activeElement = document.activeElement;\n\n if (e.shiftKey && activeElement === first) {\n e.preventDefault();\n last.focus();\n } else if (!e.shiftKey && activeElement === last) {\n e.preventDefault();\n first.focus();\n }\n }\n };\n\n document.addEventListener('keydown', handleKeyDown, true);\n return () => document.removeEventListener('keydown', handleKeyDown, true);\n }, [enabled, containerRef]);\n\n // Focus trap safety net: catch focus escaping\n useLayoutEffect(() => {\n if (!enabled) return;\n\n const container = containerRef.current;\n if (!container) return;\n\n const handleFocusIn = (e: FocusEvent) => {\n const target = e.target as Node;\n\n // iframe focus is opaque from the parent doc — leave it alone, otherwise\n // a click into the iframe gets yanked back into the trap.\n if ((target as Element | null)?.tagName === 'IFRAME') return;\n\n if (container.contains(target)) {\n lastFocusedInContainer.current = target as HTMLElement;\n } else if (portalContainerRefs?.current?.some(ref => ref.current?.contains(target))) {\n // Focus is in a registered portal — allow it, but don't update\n // lastFocusedInContainer so safety net restores to the last main-container\n // element (e.g., the dropdown trigger) when the portal unmounts\n console.log('[FocusTrap] allowed portal focus:', (target as HTMLElement)?.tagName, (target as HTMLElement)?.className?.slice(0, 40));\n } else {\n // Focus escaped — redirect back\n console.log('[FocusTrap] REDIRECT:', (target as HTMLElement)?.tagName, (target as HTMLElement)?.className?.slice(0, 40), '→ back to container');\n const fallback = lastFocusedInContainer.current\n || getFirstFocusableElement({ container })\n || container;\n fallback.focus();\n }\n };\n\n document.addEventListener('focusin', handleFocusIn, true);\n return () => document.removeEventListener('focusin', handleFocusIn, true);\n }, [enabled, containerRef]);\n\n return { triggerRef };\n}\n"],"names":["_lastInteractedElement","_onPointerDown","e","path","composedPath","node","HTMLElement","tabIndex","_onKeyDown","key","document","activeElement","addEventListener","consumeLastInteractedElement","el","_focusAnchor","_focusAnchorClearFrame","setFocusAnchor","cancelAnimationFrame","requestAnimationFrame","consumeFocusAnchor","resolveInitialFocusTarget","container","initialFocus","getFirstFocusableElement","querySelector","useFocusTrap","enabled","containerRef","restoreFocus","returnFocusRef","portalContainerRefs","triggerRef","useRef","lastFocusedInContainer","useLayoutEffect","current","isConnected","focus","previousActiveElement","tagName","contains","handleKeyDown","focusables","getFocusableElements","length","preventDefault","first","last","shiftKey","removeEventListener","handleFocusIn","target","some","ref","console","log","className","slice"],"mappings":";;AAsDA,IAAIA,IAA6C;AAEjD,SAASC,EAAeC,GAAuB;AAE7C,QAAMC,IAAOD,EAAEE,aAAAA;AACf,aAAWC,KAAQF;AACjB,QAAIE,aAAgBC,eAAeD,EAAKE,YAAY,GAAG;AACrDP,MAAAA,IAAyBK;AACzB;AAAA,IACF;AAEJ;AAEA,SAASG,EAAWN,GAAwB;AAE1C,GAAIA,EAAEO,QAAQ,WAAWP,EAAEO,QAAQ,SACjCT,IAAyBU,SAASC;AAEtC;AAEI,OAAOD,WAAa,QACtBA,SAASE,iBAAiB,eAAeX,GAAgB,EAAI,GAC7DS,SAASE,iBAAiB,WAAWJ,GAAY,EAAI;AAQhD,SAASK,IAAmD;AACjE,QAAMC,IAAKd;AACXA,SAAAA,IAAyB,MAClBc;AACT;AAKA,IAAIC,IAAmC,MACnCC,IAA0E;AAQvE,SAASC,EAAeH,GAA8B;AAC3D,EAAIE,MAA2B,QAAME,qBAAqBF,CAAsB,GAChFD,IAAeD,GAIfE,IAAyBG,sBAAsB,MAAM;AACnDJ,IAAAA,IAAe,MACfC,IAAyB;AAAA,EAC3B,CAAC;AACH;AAMO,SAASI,IAAyC;AACvD,EAAIJ,MAA2B,SAC7BE,qBAAqBF,CAAsB,GAC3CA,IAAyB;AAE3B,QAAMF,IAAKC;AACXA,SAAAA,IAAe,MACRD;AACT;AAKA,SAASO,EACPC,GACAC,GACoB;AACpB,SAAIA,MAAiB,SAAe,OAChCA,MAAiB,UACZC,EAAyB;AAAA,IAAEF,WAAAA;AAAAA,EAAAA,CAAW,KAAKA,IAEhDC,MAAiB,cAC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0BJ,GAAwBZ,SAAUY,GAAwBK,WAAWC,MAAM,GAAG,EAAE,GAAG,qBAAqB,IAC7HxB,EAAuBE,WACnCZ,EAAyB;AAAA,QAAEF,WAAAA;AAAAA,MAAAA,CAAW,KACtCA,GACIgB,MAAAA;AAAAA,IAEb;AAEA5B,oBAASE,iBAAiB,WAAWuC,GAAe,EAAI,GACjD,MAAMzC,SAASwC,oBAAoB,WAAWC,GAAe,EAAI;AAAA,EAC1E,GAAG,CAACxB,GAASC,CAAY,CAAC,GAEnB;AAAA,IAAEI,YAAAA;AAAAA,EAAAA;AACX;"}
1
+ {"version":3,"file":"index72.js","sources":["../src/utils/a11y/useFocusTrap.ts"],"sourcesContent":["import { useLayoutEffect, useRef } from 'react';\nimport type { MutableRefObject, RefObject } from 'react';\nimport { getFocusableElements, getFirstFocusableElement } from './focusableElements';\n\nexport interface UseFocusTrapOptions<T extends HTMLElement = HTMLElement> {\n /**\n * Whether the focus trap is active.\n */\n enabled: boolean;\n /**\n * Container element ref to trap focus within.\n */\n containerRef: React.RefObject<T | null>;\n /**\n * Whether to restore focus to the element that had focus before trap activated.\n * Default: true\n */\n restoreFocus?: boolean;\n /**\n * Initial focus target when trap activates.\n * - 'first': Focus first focusable element (default)\n * - 'container': Focus the container itself\n * - 'none': Skip initial focus — browser handles it (e.g. autofocus attribute)\n * - CSS selector: Focus element matching selector\n * - HTMLElement: Focus this specific element\n */\n initialFocus?: 'first' | 'container' | 'none' | string | HTMLElement;\n /**\n * Explicit element to restore focus to when the trap deactivates.\n * Overrides the automatic trigger capture (_lastInteractedElement / document.activeElement).\n * Use when the opener is known at call-site (e.g. a ref on the toggle button).\n */\n returnFocusRef?: React.RefObject<HTMLElement | null>;\n /**\n * Additional container refs that are logically part of this focus trap\n * (e.g., portal-rendered content from Popover/Dropdown). Focus moving to\n * these containers will NOT trigger the safety net redirect.\n */\n portalContainerRefs?: MutableRefObject<RefObject<HTMLElement | null>[]>;\n}\n\nexport interface UseFocusTrapReturn {\n /**\n * Ref to the element that had focus before trap activated.\n * Useful for manual focus restoration if needed.\n */\n triggerRef: React.MutableRefObject<HTMLElement | null>;\n}\n\n// Module-level trigger tracking: React's commit phase is bottom-up, so by the\n// time useFocusTrap's useLayoutEffect runs, document.activeElement may already\n// be the autoFocus element inside the modal rather than the opener button.\n// We capture the last interacted element via pointerdown/keydown listeners\n// (which fire before any React state update commits) to work around this.\nlet _lastInteractedElement: HTMLElement | null = null;\n\nfunction _onPointerDown(e: PointerEvent): void {\n // Walk composedPath to find the nearest focusable element being pressed.\n const path = e.composedPath();\n for (const node of path) {\n if (node instanceof HTMLElement && node.tabIndex >= 0) {\n _lastInteractedElement = node;\n return;\n }\n }\n}\n\nfunction _onKeyDown(e: KeyboardEvent): void {\n // Enter/Space on a focused button will synthesise a click — capture before that.\n if (e.key === 'Enter' || e.key === ' ') {\n _lastInteractedElement = document.activeElement as HTMLElement;\n }\n}\n\nif (typeof document !== 'undefined') {\n document.addEventListener('pointerdown', _onPointerDown, true);\n document.addEventListener('keydown', _onKeyDown, true);\n}\n\n/**\n * Returns (and clears) the last element interacted with before a React commit.\n * Used by SidebarOverlay complementary mode to seed its trigger ref without\n * relying on document.activeElement timing.\n */\nexport function consumeLastInteractedElement(): HTMLElement | null {\n const el = _lastInteractedElement;\n _lastInteractedElement = null;\n return el;\n}\n\n// Focus anchor — explicit stable return-focus target, higher priority than\n// _lastInteractedElement. Auto-clears via setTimeout(0) to prevent a stale\n// anchor leaking to an unrelated open.\nlet _focusAnchor: HTMLElement | null = null;\nlet _focusAnchorClearFrame: ReturnType<typeof requestAnimationFrame> | null = null;\n\n/**\n * Sets an explicit focus-return anchor element.\n * Call this before dispatching an action that will open a modal or sidebar,\n * when the natural last-interacted element is not the right return target\n * (e.g. a popover menu item that will be unmounted when the popover closes).\n */\nexport function setFocusAnchor(el: HTMLElement | null): void {\n if (_focusAnchorClearFrame !== null) cancelAnimationFrame(_focusAnchorClearFrame);\n _focusAnchor = el;\n // Auto-clear if not consumed — prevents stale anchor leaking to unrelated opens.\n // rAF fires after React has committed and painted, giving useFocusTrap time to\n // consume the anchor before it is discarded.\n _focusAnchorClearFrame = requestAnimationFrame(() => {\n _focusAnchor = null;\n _focusAnchorClearFrame = null;\n });\n}\n\n/**\n * Returns (and clears) the focus anchor if one was set, otherwise null.\n * Used internally by useFocusTrap and SidebarOverlay.\n */\nexport function consumeFocusAnchor(): HTMLElement | null {\n if (_focusAnchorClearFrame !== null) {\n cancelAnimationFrame(_focusAnchorClearFrame);\n _focusAnchorClearFrame = null;\n }\n const el = _focusAnchor;\n _focusAnchor = null;\n return el;\n}\n\n/**\n * Resolve the initial focus target based on the initialFocus option.\n */\nfunction resolveInitialFocusTarget(\n container: HTMLElement,\n initialFocus: 'first' | 'container' | 'none' | string | HTMLElement\n): HTMLElement | null {\n if (initialFocus === 'none') return null;\n if (initialFocus === 'first') {\n return getFirstFocusableElement({ container }) || container;\n }\n if (initialFocus === 'container') {\n return container;\n }\n if (typeof initialFocus === 'string') {\n return container.querySelector<HTMLElement>(initialFocus);\n }\n if (initialFocus instanceof HTMLElement) {\n return initialFocus;\n }\n return null;\n}\n\n/**\n * Hook to trap focus within a container (for modals, dialogs, drawers).\n *\n * Implements WCAG 2.1 focus trap pattern:\n * - Moves focus into container on activation\n * - Wraps Tab/Shift+Tab navigation within container\n * - Restores focus to trigger element on deactivation\n * - Safety net: catches focus escaping via other means\n * - Handles autoFocus content: captures trigger before autoFocus fires\n * - Handles {isOpen && <Modal>} pattern: restores focus on unmount\n *\n * Note: For Escape key handling, use `useDismissOnEscape` hook separately.\n * This keeps focus trap (accessibility) separate from Escape handling (UX).\n *\n * @example\n * ```tsx\n * const MyModal = ({ isOpen, onClose }) => {\n * const containerRef = useRef<HTMLDivElement>(null);\n *\n * // Escape handling (UX)\n * useDismissOnEscape({\n * containerRef,\n * onDismiss: onClose,\n * enabled: isOpen\n * });\n *\n * // Focus trap (accessibility)\n * const { triggerRef } = useFocusTrap({\n * enabled: isOpen,\n * containerRef,\n * restoreFocus: true\n * });\n *\n * return (\n * <div ref={containerRef}>\n * <button>First</button>\n * <button>Second</button>\n * </div>\n * );\n * };\n * ```\n */\nexport function useFocusTrap<T extends HTMLElement = HTMLElement>({\n enabled,\n containerRef,\n restoreFocus = true,\n initialFocus = 'first',\n returnFocusRef,\n portalContainerRefs,\n}: UseFocusTrapOptions<T>): UseFocusTrapReturn {\n const triggerRef = useRef<HTMLElement | null>(null);\n const lastFocusedInContainer = useRef<HTMLElement | null>(null);\n\n // Focus management: save trigger, move focus into container on activate, restore on deactivate\n useLayoutEffect(() => {\n if (!enabled) {\n // Restore focus to trigger when trap deactivates\n if (restoreFocus && triggerRef.current) {\n const el = triggerRef.current;\n triggerRef.current = null;\n requestAnimationFrame(() => {\n if (el.isConnected) el.focus();\n });\n }\n return;\n }\n\n const container = containerRef.current;\n if (!container) return;\n\n // Resolve trigger: explicit returnFocusRef wins, then focusAnchor (explicitly set\n // by caller for two-step flows like popover menu → modal), then the pre-commit\n // interaction record, then document.activeElement as final fallback.\n const previousActiveElement = returnFocusRef?.current ?? consumeFocusAnchor() ?? _lastInteractedElement ?? (document.activeElement as HTMLElement);\n _lastInteractedElement = null;\n\n // iframes manage their own internal focus. document.activeElement only ever\n // resolves to the <iframe> element from the parent doc — calling .focus()\n // on any parent-doc element forcibly blurs the iframe content (kills caret\n // in textareas, cancels native <select> dropdowns). Skip capture so the\n // auto-focus on open and focus-restore on close short-circuit on null trigger.\n if (previousActiveElement?.tagName === 'IFRAME') {\n triggerRef.current = null;\n return;\n }\n\n triggerRef.current = previousActiveElement;\n\n // Only move initial focus if autoFocus hasn't already placed it inside the container.\n if (!container.contains(document.activeElement)) {\n requestAnimationFrame(() => {\n resolveInitialFocusTarget(container, initialFocus)?.focus();\n });\n }\n\n // Restore focus on unmount while enabled (covers {isOpen && <Modal>} pattern\n // where the component unmounts before enabled can transition true → false)\n return () => {\n if (restoreFocus && triggerRef.current) {\n const el = triggerRef.current;\n triggerRef.current = null;\n requestAnimationFrame(() => {\n if (el.isConnected) el.focus();\n });\n }\n };\n }, [enabled, containerRef, restoreFocus, initialFocus]);\n\n // Focus trap: Tab wrapping (only when enabled)\n useLayoutEffect(() => {\n if (!enabled) return;\n\n const container = containerRef.current;\n if (!container) return;\n\n const handleKeyDown = (e: KeyboardEvent) => {\n // Tab wrapping\n if (e.key === 'Tab') {\n const focusables = getFocusableElements({ container });\n\n if (focusables.length === 0) {\n e.preventDefault();\n container.focus();\n return;\n }\n\n const first = focusables[0];\n const last = focusables[focusables.length - 1];\n const activeElement = document.activeElement;\n\n if (e.shiftKey && activeElement === first) {\n e.preventDefault();\n last.focus();\n } else if (!e.shiftKey && activeElement === last) {\n e.preventDefault();\n first.focus();\n }\n }\n };\n\n document.addEventListener('keydown', handleKeyDown, true);\n return () => document.removeEventListener('keydown', handleKeyDown, true);\n }, [enabled, containerRef]);\n\n // Focus trap safety net: catch focus escaping\n useLayoutEffect(() => {\n if (!enabled) return;\n\n const container = containerRef.current;\n if (!container) return;\n\n const handleFocusIn = (e: FocusEvent) => {\n const target = e.target as Node;\n\n // iframe focus is opaque from the parent doc — leave it alone, otherwise\n // a click into the iframe gets yanked back into the trap.\n if ((target as Element | null)?.tagName === 'IFRAME') return;\n\n if (container.contains(target)) {\n lastFocusedInContainer.current = target as HTMLElement;\n } else if (portalContainerRefs?.current?.some(ref => ref.current?.contains(target))) {\n // Focus is in a registered portal — allow it, but don't update\n // lastFocusedInContainer so safety net restores to the last main-container\n // element (e.g., the dropdown trigger) when the portal unmounts\n } else {\n // Focus escaped — redirect back\n const fallback = lastFocusedInContainer.current\n || getFirstFocusableElement({ container })\n || container;\n fallback.focus();\n }\n };\n\n document.addEventListener('focusin', handleFocusIn, true);\n return () => document.removeEventListener('focusin', handleFocusIn, true);\n }, [enabled, containerRef]);\n\n return { triggerRef };\n}\n"],"names":["_lastInteractedElement","_onPointerDown","e","path","composedPath","node","HTMLElement","tabIndex","_onKeyDown","key","document","activeElement","addEventListener","consumeLastInteractedElement","el","_focusAnchor","_focusAnchorClearFrame","setFocusAnchor","cancelAnimationFrame","requestAnimationFrame","consumeFocusAnchor","resolveInitialFocusTarget","container","initialFocus","getFirstFocusableElement","querySelector","useFocusTrap","enabled","containerRef","restoreFocus","returnFocusRef","portalContainerRefs","triggerRef","useRef","lastFocusedInContainer","useLayoutEffect","current","isConnected","focus","previousActiveElement","tagName","contains","handleKeyDown","focusables","getFocusableElements","length","preventDefault","first","last","shiftKey","removeEventListener","handleFocusIn","target","some","ref"],"mappings":";;AAsDA,IAAIA,IAA6C;AAEjD,SAASC,EAAeC,GAAuB;AAE7C,QAAMC,IAAOD,EAAEE,aAAAA;AACf,aAAWC,KAAQF;AACjB,QAAIE,aAAgBC,eAAeD,EAAKE,YAAY,GAAG;AACrDP,MAAAA,IAAyBK;AACzB;AAAA,IACF;AAEJ;AAEA,SAASG,EAAWN,GAAwB;AAE1C,GAAIA,EAAEO,QAAQ,WAAWP,EAAEO,QAAQ,SACjCT,IAAyBU,SAASC;AAEtC;AAEI,OAAOD,WAAa,QACtBA,SAASE,iBAAiB,eAAeX,GAAgB,EAAI,GAC7DS,SAASE,iBAAiB,WAAWJ,GAAY,EAAI;AAQhD,SAASK,IAAmD;AACjE,QAAMC,IAAKd;AACXA,SAAAA,IAAyB,MAClBc;AACT;AAKA,IAAIC,IAAmC,MACnCC,IAA0E;AAQvE,SAASC,EAAeH,GAA8B;AAC3D,EAAIE,MAA2B,QAAME,qBAAqBF,CAAsB,GAChFD,IAAeD,GAIfE,IAAyBG,sBAAsB,MAAM;AACnDJ,IAAAA,IAAe,MACfC,IAAyB;AAAA,EAC3B,CAAC;AACH;AAMO,SAASI,IAAyC;AACvD,EAAIJ,MAA2B,SAC7BE,qBAAqBF,CAAsB,GAC3CA,IAAyB;AAE3B,QAAMF,IAAKC;AACXA,SAAAA,IAAe,MACRD;AACT;AAKA,SAASO,EACPC,GACAC,GACoB;AACpB,SAAIA,MAAiB,SAAe,OAChCA,MAAiB,UACZC,E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EF,WAAAA;AAAAA,MAAAA,CAAW,KACtCA,GACIgB,MAAAA;AAAAA,IAEb;AAEA5B,oBAASE,iBAAiB,WAAWuC,GAAe,EAAI,GACjD,MAAMzC,SAASwC,oBAAoB,WAAWC,GAAe,EAAI;AAAA,EAC1E,GAAG,CAACxB,GAASC,CAAY,CAAC,GAEnB;AAAA,IAAEI,YAAAA;AAAAA,EAAAA;AACX;"}
package/dist/index75.js CHANGED
@@ -1,20 +1,22 @@
1
- var u = Object.defineProperty;
2
- var l = (i, e, t) => e in i ? u(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
3
- var r = (i, e, t) => l(i, typeof e != "symbol" ? e + "" : e, t);
4
- const h = "sr-only", a = "data-batch-id";
5
- let s = null;
6
- function d() {
7
- return s || (s = new f()), s;
1
+ var m = Object.defineProperty;
2
+ var g = (r, e, t) => e in r ? m(r, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : r[e] = t;
3
+ var o = (r, e, t) => g(r, typeof e != "symbol" ? e + "" : e, t);
4
+ const u = "sr-only", h = "data-batch-id", f = "data-modal-announcer";
5
+ let c = null;
6
+ function p() {
7
+ return c || (c = new T()), c;
8
8
  }
9
- class f {
9
+ class T {
10
10
  constructor() {
11
- r(this, "container", null);
12
- r(this, "assertiveLog", null);
13
- r(this, "politeLog", null);
14
- r(this, "cleanupTimers", /* @__PURE__ */ new Map());
15
- r(this, "ready", !1);
16
- r(this, "pendingQueue", []);
17
- typeof document > "u" || (this.container = document.createElement("div"), this.container.dataset.liveAnnouncer = "true", this.container.className = h, this.assertiveLog = this.createLog("assertive"), this.politeLog = this.createLog("polite"), this.container.appendChild(this.assertiveLog), this.container.appendChild(this.politeLog), document.body.prepend(this.container), this.isTestEnvironment() ? this.ready = !0 : setTimeout(() => {
11
+ o(this, "container", null);
12
+ o(this, "assertiveLog", null);
13
+ o(this, "politeLog", null);
14
+ o(this, "cleanupTimers", /* @__PURE__ */ new Map());
15
+ o(this, "delayTimers", /* @__PURE__ */ new Map());
16
+ o(this, "pendingRafByBatchId", /* @__PURE__ */ new Map());
17
+ o(this, "ready", !1);
18
+ o(this, "pendingQueue", []);
19
+ typeof document > "u" || (this.container = document.createElement("div"), this.container.dataset.liveAnnouncer = "true", this.container.className = u, this.assertiveLog = this.createLog("assertive"), this.politeLog = this.createLog("polite"), this.container.appendChild(this.assertiveLog), this.container.appendChild(this.politeLog), document.body.prepend(this.container), this.isTestEnvironment() ? this.ready = !0 : setTimeout(() => {
18
20
  this.ready = !0, this.flushPendingQueue();
19
21
  }, 100));
20
22
  }
@@ -22,25 +24,37 @@ class f {
22
24
  // Public
23
25
  // -----------------------------------------------------------------------
24
26
  announce(e, t = {}) {
25
- const n = {
27
+ const i = {
26
28
  assertiveness: t.assertiveness ?? "polite",
27
29
  timeout: t.timeout ?? 7e3,
28
- batchId: t.batchId ?? ""
30
+ batchId: t.batchId ?? "",
31
+ delay: t.delay ?? 0
29
32
  };
33
+ if (i.batchId) {
34
+ const n = this.delayTimers.get(i.batchId);
35
+ n && (clearTimeout(n), this.delayTimers.delete(i.batchId));
36
+ }
30
37
  if (!this.ready) {
31
38
  this.pendingQueue.push({
32
39
  message: e,
33
- options: n
40
+ options: i
34
41
  });
35
42
  return;
36
43
  }
37
- this.doAnnounce(e, n);
44
+ if (i.delay > 0) {
45
+ const n = i.batchId || `__delay_${Date.now()}_${Math.random()}`, a = setTimeout(() => {
46
+ this.delayTimers.delete(n), this.doAnnounce(e, i);
47
+ }, i.delay);
48
+ this.delayTimers.set(n, a);
49
+ return;
50
+ }
51
+ this.doAnnounce(e, i);
38
52
  }
39
53
  clear(e) {
40
54
  (!e || e === "assertive") && this.clearLog(this.assertiveLog), (!e || e === "polite") && this.clearLog(this.politeLog);
41
55
  }
42
56
  destroy() {
43
- this.cleanupTimers.forEach((e) => clearTimeout(e)), this.cleanupTimers.clear(), this.container?.remove(), this.container = null, this.assertiveLog = null, this.politeLog = null, this.ready = !1, this.pendingQueue = [], s === this && (s = null);
57
+ this.cleanupTimers.forEach((e) => clearTimeout(e)), this.cleanupTimers.clear(), this.delayTimers.forEach((e) => clearTimeout(e)), this.delayTimers.clear(), this.pendingRafByBatchId.forEach((e) => cancelAnimationFrame(e)), this.pendingRafByBatchId.clear(), this.container?.remove(), this.container = null, this.assertiveLog = null, this.politeLog = null, this.ready = !1, this.pendingQueue = [], c === this && (c = null);
44
58
  }
45
59
  isAttached() {
46
60
  return this.container?.isConnected ?? !1;
@@ -52,20 +66,69 @@ class f {
52
66
  const t = document.createElement("div");
53
67
  return t.setAttribute("role", "log"), t.setAttribute("aria-live", e), t.setAttribute("aria-relevant", "additions"), t;
54
68
  }
69
+ /**
70
+ * Returns the appropriate log element for the current context.
71
+ * When an aria-modal dialog is active, announcements must be made from
72
+ * within the modal (screen readers ignore content outside aria-modal).
73
+ * A live region is lazily created inside the modal and cleaned up when
74
+ * the modal is removed from the DOM.
75
+ */
76
+ getLogForContext(e) {
77
+ const t = this.findVisibleModal();
78
+ if (!t)
79
+ return e === "assertive" ? this.assertiveLog : this.politeLog;
80
+ let i = t.querySelector(`[${f}]`);
81
+ if (!i) {
82
+ i = document.createElement("div"), i.setAttribute(f, "true"), i.className = u;
83
+ const s = this.createLog("assertive"), d = this.createLog("polite");
84
+ i.appendChild(s), i.appendChild(d), t.appendChild(i);
85
+ }
86
+ const n = i.querySelectorAll('[role="log"]'), a = n[0], l = n[1];
87
+ return e === "assertive" ? a : l;
88
+ }
89
+ /**
90
+ * Returns the first aria-modal element that is actually visible.
91
+ * A modal is considered hidden if:
92
+ * - it or an ancestor has aria-hidden="true", or
93
+ * - its computed visibility is "hidden" (se-design Modal uses the
94
+ * `invisible` CSS class on the wrapper when closed).
95
+ */
96
+ findVisibleModal() {
97
+ const e = document.querySelectorAll('[aria-modal="true"]');
98
+ for (const t of e) {
99
+ const i = t;
100
+ if (!i.closest('[aria-hidden="true"]') && getComputedStyle(i).visibility !== "hidden")
101
+ return i;
102
+ }
103
+ return null;
104
+ }
55
105
  doAnnounce(e, t) {
56
- const n = t.assertiveness === "assertive" ? this.assertiveLog : this.politeLog;
57
- if (!n) return;
106
+ const i = this.getLogForContext(t.assertiveness);
107
+ if (!i) return;
108
+ let n = !1;
58
109
  if (t.batchId) {
59
- const c = n.querySelector(`[${a}="${CSS.escape(t.batchId)}"]`);
60
- c && this.removeNode(c);
61
- }
62
- const o = document.createElement("div");
63
- if (o.textContent = e, t.batchId && o.setAttribute(a, t.batchId), n.appendChild(o), e !== "") {
64
- const c = setTimeout(() => {
65
- this.removeNode(o);
66
- }, t.timeout);
67
- this.cleanupTimers.set(o, c);
110
+ const s = i.querySelector(`[${h}="${CSS.escape(t.batchId)}"]`);
111
+ s && (this.removeNode(s), n = !0);
68
112
  }
113
+ const a = document.createElement("div");
114
+ a.textContent = e, t.batchId && a.setAttribute(h, t.batchId);
115
+ const l = () => {
116
+ if (i.isConnected && (i.appendChild(a), e !== "")) {
117
+ const s = setTimeout(() => {
118
+ this.removeNode(a);
119
+ }, t.timeout);
120
+ this.cleanupTimers.set(a, s);
121
+ }
122
+ };
123
+ if (n) {
124
+ const s = this.pendingRafByBatchId.get(t.batchId);
125
+ s !== void 0 && cancelAnimationFrame(s);
126
+ const d = requestAnimationFrame(() => {
127
+ this.pendingRafByBatchId.delete(t.batchId), l();
128
+ });
129
+ this.pendingRafByBatchId.set(t.batchId, d);
130
+ } else
131
+ l();
69
132
  }
70
133
  removeNode(e) {
71
134
  const t = this.cleanupTimers.get(e);
@@ -73,15 +136,15 @@ class f {
73
136
  }
74
137
  clearLog(e) {
75
138
  e && (Array.from(e.children).forEach((t) => {
76
- const n = this.cleanupTimers.get(t);
77
- n && (clearTimeout(n), this.cleanupTimers.delete(t));
139
+ const i = this.cleanupTimers.get(t);
140
+ i && (clearTimeout(i), this.cleanupTimers.delete(t));
78
141
  }), e.innerHTML = "");
79
142
  }
80
143
  flushPendingQueue() {
81
144
  this.pendingQueue.splice(0).forEach(({
82
145
  message: t,
83
- options: n
84
- }) => this.doAnnounce(t, n));
146
+ options: i
147
+ }) => this.doAnnounce(t, i));
85
148
  }
86
149
  isTestEnvironment() {
87
150
  try {
@@ -91,21 +154,21 @@ class f {
91
154
  return !1;
92
155
  }
93
156
  }
94
- function T(i, e) {
157
+ function A(r, e) {
95
158
  const t = typeof e == "string" ? {
96
159
  assertiveness: e
97
160
  } : e ?? {};
98
- d().announce(i, t);
161
+ p().announce(r, t);
99
162
  }
100
- function p(i) {
101
- s?.clear(i);
163
+ function L(r) {
164
+ c?.clear(r);
102
165
  }
103
- function g() {
104
- s?.destroy();
166
+ function I() {
167
+ c?.destroy();
105
168
  }
106
169
  export {
107
- T as announce,
108
- p as clearAnnouncer,
109
- g as destroyAnnouncer
170
+ A as announce,
171
+ L as clearAnnouncer,
172
+ I as destroyAnnouncer
110
173
  };
111
174
  //# sourceMappingURL=index75.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index75.js","sources":["../src/utils/a11y/liveAnnouncer/LiveAnnouncer.ts"],"sourcesContent":["/**\n * LiveAnnouncer — singleton vanilla-DOM announcer for screen readers.\n *\n * Architecture:\n * - Prepends a visually-hidden container to document.body on first use.\n * - Two <div role=\"log\" aria-live=\"...\"> regions: one polite, one assertive.\n * - Each announce() appends a NEW child node (aria-relevant=\"additions\" makes\n * screen readers announce only additions, never re-read existing content).\n * - Child nodes auto-remove after a timeout (default 7 s) to keep the DOM clean.\n * - batchId option: if a node with the same batchId already exists in a log,\n * it is replaced — so only the final value is announced (e.g. \"N results\").\n *\n * No React dependency. No role=\"status\" or role=\"alert\" (avoids double-announce\n * bugs on iOS VoiceOver).\n */\n\nexport type Assertiveness = 'assertive' | 'polite';\n\nexport interface AnnounceOptions {\n /** Default: 'polite' */\n assertiveness?: Assertiveness;\n /** Auto-clear timeout in ms. Default: 7000 */\n timeout?: number;\n /**\n * When provided, any pending node with the same batchId is replaced rather\n * than appending a second node. Useful for rapidly-updating counts\n * (e.g. search results, selection count).\n */\n batchId?: string;\n}\n\nconst DEFAULT_TIMEOUT = 7000;\nconst SAFARI_INIT_DELAY = 100;\n\nconst SR_ONLY_CLASS = 'sr-only';\n\n/** Attribute used to find batchId nodes for replacement. */\nconst BATCH_ATTR = 'data-batch-id';\n\n// ---------------------------------------------------------------------------\n// Singleton\n// ---------------------------------------------------------------------------\n\nlet instance: LiveAnnouncer | null = null;\n\n/**\n * Get or create the singleton LiveAnnouncer.\n * Safe to call multiple times — returns the same instance.\n */\nfunction getInstance(): LiveAnnouncer {\n if (!instance) {\n instance = new LiveAnnouncer();\n }\n return instance;\n}\n\n// ---------------------------------------------------------------------------\n// Class\n// ---------------------------------------------------------------------------\n\nclass LiveAnnouncer {\n private container: HTMLDivElement | null = null;\n private assertiveLog: HTMLDivElement | null = null;\n private politeLog: HTMLDivElement | null = null;\n private cleanupTimers: Map<HTMLElement, ReturnType<typeof setTimeout>> = new Map();\n private ready = false;\n private pendingQueue: Array<{ message: string; options: Required<AnnounceOptions> }> = [];\n\n constructor() {\n if (typeof document === 'undefined') return;\n\n this.container = document.createElement('div');\n this.container.dataset.liveAnnouncer = 'true';\n this.container.className = SR_ONLY_CLASS;\n\n this.assertiveLog = this.createLog('assertive');\n this.politeLog = this.createLog('polite');\n\n this.container.appendChild(this.assertiveLog);\n this.container.appendChild(this.politeLog);\n\n // Prepend so screen readers encounter it early in the DOM.\n document.body.prepend(this.container);\n\n // Safari/VoiceOver ignores content injected within ~100 ms of the live\n // region being added to the DOM. Delay readiness accordingly.\n // In test environments, skip the delay.\n if (this.isTestEnvironment()) {\n this.ready = true;\n } else {\n setTimeout(() => {\n this.ready = true;\n this.flushPendingQueue();\n }, SAFARI_INIT_DELAY);\n }\n }\n\n // -----------------------------------------------------------------------\n // Public\n // -----------------------------------------------------------------------\n\n announce(message: string, options: AnnounceOptions = {}): void {\n const resolved: Required<AnnounceOptions> = {\n assertiveness: options.assertiveness ?? 'polite',\n timeout: options.timeout ?? DEFAULT_TIMEOUT,\n batchId: options.batchId ?? '',\n };\n\n if (!this.ready) {\n this.pendingQueue.push({ message, options: resolved });\n return;\n }\n\n this.doAnnounce(message, resolved);\n }\n\n clear(assertiveness?: Assertiveness): void {\n if (!assertiveness || assertiveness === 'assertive') {\n this.clearLog(this.assertiveLog);\n }\n if (!assertiveness || assertiveness === 'polite') {\n this.clearLog(this.politeLog);\n }\n }\n\n destroy(): void {\n // Cancel all pending auto-clear timers.\n this.cleanupTimers.forEach((timer) => clearTimeout(timer));\n this.cleanupTimers.clear();\n\n this.container?.remove();\n this.container = null;\n this.assertiveLog = null;\n this.politeLog = null;\n this.ready = false;\n this.pendingQueue = [];\n\n if (instance === this) {\n instance = null;\n }\n }\n\n isAttached(): boolean {\n return this.container?.isConnected ?? false;\n }\n\n // -----------------------------------------------------------------------\n // Private\n // -----------------------------------------------------------------------\n\n private createLog(ariaLive: Assertiveness): HTMLDivElement {\n const log = document.createElement('div');\n log.setAttribute('role', 'log');\n log.setAttribute('aria-live', ariaLive);\n log.setAttribute('aria-relevant', 'additions');\n return log;\n }\n\n private doAnnounce(message: string, options: Required<AnnounceOptions>): void {\n const log = options.assertiveness === 'assertive' ? this.assertiveLog : this.politeLog;\n if (!log) return;\n\n // batchId dedup: remove any existing node with the same batchId.\n if (options.batchId) {\n const existing = log.querySelector(`[${BATCH_ATTR}=\"${CSS.escape(options.batchId)}\"]`) as HTMLElement | null;\n if (existing) {\n this.removeNode(existing);\n }\n }\n\n const node = document.createElement('div');\n node.textContent = message;\n\n if (options.batchId) {\n node.setAttribute(BATCH_ATTR, options.batchId);\n }\n\n log.appendChild(node);\n\n // Auto-remove after timeout to keep the DOM clean.\n if (message !== '') {\n const timer = setTimeout(() => {\n this.removeNode(node);\n }, options.timeout);\n this.cleanupTimers.set(node, timer);\n }\n }\n\n private removeNode(node: HTMLElement): void {\n const timer = this.cleanupTimers.get(node);\n if (timer) {\n clearTimeout(timer);\n this.cleanupTimers.delete(node);\n }\n node.remove();\n }\n\n private clearLog(log: HTMLDivElement | null): void {\n if (!log) return;\n // Cancel timers for all children in this log.\n Array.from(log.children).forEach((child) => {\n const timer = this.cleanupTimers.get(child as HTMLElement);\n if (timer) {\n clearTimeout(timer);\n this.cleanupTimers.delete(child as HTMLElement);\n }\n });\n log.innerHTML = '';\n }\n\n private flushPendingQueue(): void {\n const queued = this.pendingQueue.splice(0);\n queued.forEach(({ message, options }) => this.doAnnounce(message, options));\n }\n\n private isTestEnvironment(): boolean {\n try {\n // React 18+ test flag\n // @ts-expect-error – global test flag\n if (typeof IS_REACT_ACT_ENVIRONMENT === 'boolean' && IS_REACT_ACT_ENVIRONMENT) return true;\n // Jest / Vitest\n // @ts-expect-error – jest global may not be typed\n if (typeof jest !== 'undefined') return true;\n // @ts-expect-error – Vitest global\n if (typeof vitest !== 'undefined') return true;\n } catch {\n // ignore\n }\n return false;\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public imperative API (module-level)\n// ---------------------------------------------------------------------------\n\n/**\n * Announce a message to screen readers.\n *\n * Works anywhere — React components, event handlers, thunks, vanilla JS.\n * The singleton live region is lazily created on first call.\n *\n * @example\n * announce('5 results found');\n * announce('Error: upload failed', { assertiveness: 'assertive' });\n * announce('3 items selected', { batchId: 'selection-count' });\n */\nexport function announce(message: string, options?: AnnounceOptions): void;\nexport function announce(message: string, assertiveness?: Assertiveness): void;\nexport function announce(\n message: string,\n optionsOrAssertiveness?: AnnounceOptions | Assertiveness\n): void {\n const options: AnnounceOptions =\n typeof optionsOrAssertiveness === 'string'\n ? { assertiveness: optionsOrAssertiveness }\n : optionsOrAssertiveness ?? {};\n\n getInstance().announce(message, options);\n}\n\n/**\n * Clear all pending announcements.\n * @param assertiveness — if omitted, clears both polite and assertive logs.\n */\nexport function clearAnnouncer(assertiveness?: Assertiveness): void {\n instance?.clear(assertiveness);\n}\n\n/**\n * Remove the live-region container from the DOM and reset the singleton.\n * Typically called when the host app unmounts (via LiveAnnouncerProvider).\n */\nexport function destroyAnnouncer(): void {\n instance?.destroy();\n}\n\n/**\n * Check whether the live-region container is currently in the DOM.\n */\nexport function isAnnouncerAttached(): boolean {\n return instance?.isAttached() ?? false;\n}\n"],"names":["SR_ONLY_CLASS","BATCH_ATTR","instance","getInstance","LiveAnnouncer","constructor","container","assertiveLog","politeLog","cleanupTimers","Map","ready","pendingQueue","document","createElement","dataset","liveAnnouncer","className","createLog","appendChild","body","prepend","isTestEnvironment","setTimeout","flushPendingQueue","SAFARI_INIT_DELAY","announce","message","options","resolved","assertiveness","timeout","DEFAULT_TIMEOUT","batchId","push","doAnnounce","clear","clearLog","destroy","forEach","timer","clearTimeout","remove","isAttached","isConnected","ariaLive","log","setAttribute","existing","querySelector","CSS","escape","removeNode","node","textContent","set","get","delete","Array","from","children","child","innerHTML","queued","splice","IS_REACT_ACT_ENVIRONMENT","jest","vitest","optionsOrAssertiveness","clearAnnouncer","destroyAnnouncer"],"mappings":";;;AAkCA,MAAMA,IAAgB,WAGhBC,IAAa;AAMnB,IAAIC,IAAiC;AAMrC,SAASC,IAA6B;AACpC,SAAKD,MACHA,IAAW,IAAIE,EAAAA,IAEVF;AACT;AAMA,MAAME,EAAc;AAAA,EAQlBC,cAAc;AAPNC,IAAAA,EAAAA,mBAAmC;AACnCC,IAAAA,EAAAA,sBAAsC;AACtCC,IAAAA,EAAAA,mBAAmC;AACnCC,IAAAA,EAAAA,2CAAqEC,IAAAA;AACrEC,IAAAA,EAAAA,eAAQ;AACRC,IAAAA,EAAAA,sBAA+E,CAAA;AAGrF,IAAI,OAAOC,WAAa,QAExB,KAAKP,YAAYO,SAASC,cAAc,KAAK,GAC7C,KAAKR,UAAUS,QAAQC,gBAAgB,QACvC,KAAKV,UAAUW,YAAYjB,GAE3B,KAAKO,eAAe,KAAKW,UAAU,WAAW,GAC9C,KAAKV,YAAY,KAAKU,UAAU,QAAQ,GAExC,KAAKZ,UAAUa,YAAY,KAAKZ,YAAY,GAC5C,KAAKD,UAAUa,YAAY,KAAKX,SAAS,GAGzCK,SAASO,KAAKC,QAAQ,KAAKf,SAAS,GAKhC,KAAKgB,sBACP,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AAAA;AAAAA,MAASC,SAAAA;AAAAA,IAAAA,MAAc,KAAKO,WAAWR,GAASC,CAAO,CAAC;AAAA,EAC5E;AAAA,EAEQN,oBAA6B;AACnC,QAAI;AAQF,UALI,OAAO2C,4BAA6B,aAAaA,4BAGjD,OAAOC,OAAS,OAEhB,OAAOC,SAAW,IAAa,QAAO;AAAA,IAC5C,QAAQ;AAAA,IACN;AAEF,WAAO;AAAA,EACT;AACF;AAmBO,SAASzC,EACdC,GACAyC,GACM;AACN,QAAMxC,IACJ,OAAOwC,KAA2B,WAC9B;AAAA,IAAEtC,eAAesC;AAAAA,EAAAA,IACjBA,KAA0B,CAAA;AAEhCjE,EAAAA,IAAcuB,SAASC,GAASC,CAAO;AACzC;AAMO,SAASyC,EAAevC,GAAqC;AAClE5B,EAAAA,GAAUkC,MAAMN,CAAa;AAC/B;AAMO,SAASwC,IAAyB;AACvCpE,EAAAA,GAAUoC,QAAAA;AACZ;"}
1
+ {"version":3,"file":"index75.js","sources":["../src/utils/a11y/liveAnnouncer/LiveAnnouncer.ts"],"sourcesContent":["/**\n * LiveAnnouncer — singleton vanilla-DOM announcer for screen readers.\n *\n * Architecture:\n * - Prepends a visually-hidden container to document.body on first use.\n * - Two <div role=\"log\" aria-live=\"...\"> regions: one polite, one assertive.\n * - Each announce() appends a NEW child node (aria-relevant=\"additions\" makes\n * screen readers announce only additions, never re-read existing content).\n * - Child nodes auto-remove after a timeout (default 7 s) to keep the DOM clean.\n * - batchId option: if a node with the same batchId already exists in a log,\n * it is replaced — so only the final value is announced (e.g. \"N results\").\n *\n * No React dependency. No role=\"status\" or role=\"alert\" (avoids double-announce\n * bugs on iOS VoiceOver).\n */\n\nexport type Assertiveness = 'assertive' | 'polite';\n\nexport interface AnnounceOptions {\n /** Default: 'polite' */\n assertiveness?: Assertiveness;\n /** Auto-clear timeout in ms. Default: 7000 */\n timeout?: number;\n /**\n * When provided, any pending node with the same batchId is replaced rather\n * than appending a second node. Useful for rapidly-updating counts\n * (e.g. search results, selection count).\n */\n batchId?: string;\n /**\n * Delay in ms before the announcement is made. Useful when focus changes\n * would otherwise swallow the announcement (e.g. after adding a tag in\n * a combobox). A subsequent call with the same batchId cancels any\n * pending delayed announcement.\n */\n delay?: number;\n}\n\nconst DEFAULT_TIMEOUT = 7000;\nconst SAFARI_INIT_DELAY = 100;\n\nconst SR_ONLY_CLASS = 'sr-only';\n\n/** Attribute used to find batchId nodes for replacement. */\nconst BATCH_ATTR = 'data-batch-id';\n\n/** Attribute used to identify announcer containers injected into modals. */\nconst MODAL_ANNOUNCER_ATTR = 'data-modal-announcer';\n\n// ---------------------------------------------------------------------------\n// Singleton\n// ---------------------------------------------------------------------------\n\nlet instance: LiveAnnouncer | null = null;\n\n/**\n * Get or create the singleton LiveAnnouncer.\n * Safe to call multiple times — returns the same instance.\n */\nfunction getInstance(): LiveAnnouncer {\n if (!instance) {\n instance = new LiveAnnouncer();\n }\n return instance;\n}\n\n// ---------------------------------------------------------------------------\n// Class\n// ---------------------------------------------------------------------------\n\nclass LiveAnnouncer {\n private container: HTMLDivElement | null = null;\n private assertiveLog: HTMLDivElement | null = null;\n private politeLog: HTMLDivElement | null = null;\n private cleanupTimers: Map<HTMLElement, ReturnType<typeof setTimeout>> = new Map();\n private delayTimers: Map<string, ReturnType<typeof setTimeout>> = new Map();\n private pendingRafByBatchId: Map<string, number> = new Map();\n private ready = false;\n private pendingQueue: Array<{ message: string; options: Required<AnnounceOptions> }> = [];\n\n constructor() {\n if (typeof document === 'undefined') return;\n\n this.container = document.createElement('div');\n this.container.dataset.liveAnnouncer = 'true';\n this.container.className = SR_ONLY_CLASS;\n\n this.assertiveLog = this.createLog('assertive');\n this.politeLog = this.createLog('polite');\n\n this.container.appendChild(this.assertiveLog);\n this.container.appendChild(this.politeLog);\n\n // Prepend so screen readers encounter it early in the DOM.\n document.body.prepend(this.container);\n\n // Safari/VoiceOver ignores content injected within ~100 ms of the live\n // region being added to the DOM. Delay readiness accordingly.\n // In test environments, skip the delay.\n if (this.isTestEnvironment()) {\n this.ready = true;\n } else {\n setTimeout(() => {\n this.ready = true;\n this.flushPendingQueue();\n }, SAFARI_INIT_DELAY);\n }\n }\n\n // -----------------------------------------------------------------------\n // Public\n // -----------------------------------------------------------------------\n\n announce(message: string, options: AnnounceOptions = {}): void {\n const resolved: Required<AnnounceOptions> = {\n assertiveness: options.assertiveness ?? 'polite',\n timeout: options.timeout ?? DEFAULT_TIMEOUT,\n batchId: options.batchId ?? '',\n delay: options.delay ?? 0,\n };\n\n // Cancel any pending delayed announcement with the same batchId.\n if (resolved.batchId) {\n const pendingTimer = this.delayTimers.get(resolved.batchId);\n if (pendingTimer) {\n clearTimeout(pendingTimer);\n this.delayTimers.delete(resolved.batchId);\n }\n }\n\n if (!this.ready) {\n this.pendingQueue.push({ message, options: resolved });\n return;\n }\n\n if (resolved.delay > 0) {\n const key = resolved.batchId || `__delay_${Date.now()}_${Math.random()}`;\n const timer = setTimeout(() => {\n this.delayTimers.delete(key);\n this.doAnnounce(message, resolved);\n }, resolved.delay);\n this.delayTimers.set(key, timer);\n return;\n }\n\n this.doAnnounce(message, resolved);\n }\n\n clear(assertiveness?: Assertiveness): void {\n if (!assertiveness || assertiveness === 'assertive') {\n this.clearLog(this.assertiveLog);\n }\n if (!assertiveness || assertiveness === 'polite') {\n this.clearLog(this.politeLog);\n }\n }\n\n destroy(): void {\n // Cancel all pending auto-clear timers.\n this.cleanupTimers.forEach((timer) => clearTimeout(timer));\n this.cleanupTimers.clear();\n // Cancel all pending delayed announcements.\n this.delayTimers.forEach((timer) => clearTimeout(timer));\n this.delayTimers.clear();\n // Cancel any pending rAF deferred appends.\n this.pendingRafByBatchId.forEach((rafId) => cancelAnimationFrame(rafId));\n this.pendingRafByBatchId.clear();\n\n this.container?.remove();\n this.container = null;\n this.assertiveLog = null;\n this.politeLog = null;\n this.ready = false;\n this.pendingQueue = [];\n\n if (instance === this) {\n instance = null;\n }\n }\n\n isAttached(): boolean {\n return this.container?.isConnected ?? false;\n }\n\n // -----------------------------------------------------------------------\n // Private\n // -----------------------------------------------------------------------\n\n private createLog(ariaLive: Assertiveness): HTMLDivElement {\n const log = document.createElement('div');\n log.setAttribute('role', 'log');\n log.setAttribute('aria-live', ariaLive);\n log.setAttribute('aria-relevant', 'additions');\n return log;\n }\n\n /**\n * Returns the appropriate log element for the current context.\n * When an aria-modal dialog is active, announcements must be made from\n * within the modal (screen readers ignore content outside aria-modal).\n * A live region is lazily created inside the modal and cleaned up when\n * the modal is removed from the DOM.\n */\n private getLogForContext(assertiveness: Assertiveness): HTMLDivElement | null {\n // Find the first *visible* aria-modal dialog. Hidden modals (e.g. pre-\n // rendered paywall, upload-contacts) must be skipped — otherwise the\n // announcement lands inside a hidden container and is never read.\n const modal = this.findVisibleModal();\n if (!modal) {\n return assertiveness === 'assertive' ? this.assertiveLog : this.politeLog;\n }\n\n // Find or create a live region container inside the modal.\n let modalAnnouncer = modal.querySelector(`[${MODAL_ANNOUNCER_ATTR}]`) as HTMLDivElement | null;\n if (!modalAnnouncer) {\n modalAnnouncer = document.createElement('div');\n modalAnnouncer.setAttribute(MODAL_ANNOUNCER_ATTR, 'true');\n modalAnnouncer.className = SR_ONLY_CLASS;\n\n const assertiveLog = this.createLog('assertive');\n const politeLog = this.createLog('polite');\n modalAnnouncer.appendChild(assertiveLog);\n modalAnnouncer.appendChild(politeLog);\n\n modal.appendChild(modalAnnouncer);\n }\n\n const logs = modalAnnouncer.querySelectorAll('[role=\"log\"]');\n const assertiveLog = logs[0] as HTMLDivElement | null;\n const politeLog = logs[1] as HTMLDivElement | null;\n\n return assertiveness === 'assertive' ? assertiveLog : politeLog;\n }\n\n /**\n * Returns the first aria-modal element that is actually visible.\n * A modal is considered hidden if:\n * - it or an ancestor has aria-hidden=\"true\", or\n * - its computed visibility is \"hidden\" (se-design Modal uses the\n * `invisible` CSS class on the wrapper when closed).\n */\n private findVisibleModal(): HTMLElement | null {\n const modals = document.querySelectorAll('[aria-modal=\"true\"]');\n for (const m of modals) {\n const el = m as HTMLElement;\n if (el.closest('[aria-hidden=\"true\"]')) continue;\n if (getComputedStyle(el).visibility === 'hidden') continue;\n return el;\n }\n return null;\n }\n\n private doAnnounce(message: string, options: Required<AnnounceOptions>): void {\n const log = this.getLogForContext(options.assertiveness);\n if (!log) return;\n\n // batchId dedup: remove any existing node with the same batchId.\n let hadExisting = false;\n if (options.batchId) {\n const existing = log.querySelector(`[${BATCH_ATTR}=\"${CSS.escape(options.batchId)}\"]`) as HTMLElement | null;\n if (existing) {\n this.removeNode(existing);\n hadExisting = true;\n }\n }\n\n const node = document.createElement('div');\n node.textContent = message;\n\n if (options.batchId) {\n node.setAttribute(BATCH_ATTR, options.batchId);\n }\n\n const appendNode = () => {\n // Guard: destroy() may have been called during the rAF window\n if (!log.isConnected) return;\n log.appendChild(node);\n // Auto-remove after timeout to keep the DOM clean.\n if (message !== '') {\n const timer = setTimeout(() => {\n this.removeNode(node);\n }, options.timeout);\n this.cleanupTimers.set(node, timer);\n }\n };\n\n // When replacing a batched node, defer the append by one animation frame.\n // Remove + append in the same tick looks like a mutation to screen readers\n // (aria-relevant=\"additions\" is not triggered), so the announcement is missed.\n // Deferring ensures the SR sees a clean addition event.\n // Cancel any pending rAF for this batchId to avoid duplicate nodes if two\n // announce() calls fire before the first rAF runs.\n if (hadExisting) {\n const pendingRaf = this.pendingRafByBatchId.get(options.batchId);\n if (pendingRaf !== undefined) {\n cancelAnimationFrame(pendingRaf);\n }\n const rafId = requestAnimationFrame(() => {\n this.pendingRafByBatchId.delete(options.batchId);\n appendNode();\n });\n this.pendingRafByBatchId.set(options.batchId, rafId);\n } else {\n appendNode();\n }\n }\n\n private removeNode(node: HTMLElement): void {\n const timer = this.cleanupTimers.get(node);\n if (timer) {\n clearTimeout(timer);\n this.cleanupTimers.delete(node);\n }\n node.remove();\n }\n\n private clearLog(log: HTMLDivElement | null): void {\n if (!log) return;\n // Cancel timers for all children in this log.\n Array.from(log.children).forEach((child) => {\n const timer = this.cleanupTimers.get(child as HTMLElement);\n if (timer) {\n clearTimeout(timer);\n this.cleanupTimers.delete(child as HTMLElement);\n }\n });\n log.innerHTML = '';\n }\n\n private flushPendingQueue(): void {\n const queued = this.pendingQueue.splice(0);\n queued.forEach(({ message, options }) => this.doAnnounce(message, options));\n }\n\n private isTestEnvironment(): boolean {\n try {\n // React 18+ test flag\n // @ts-expect-error – global test flag\n if (typeof IS_REACT_ACT_ENVIRONMENT === 'boolean' && IS_REACT_ACT_ENVIRONMENT) return true;\n // Jest / Vitest\n // @ts-expect-error – jest global may not be typed\n if (typeof jest !== 'undefined') return true;\n // @ts-expect-error – Vitest global\n if (typeof vitest !== 'undefined') return true;\n } catch {\n // ignore\n }\n return false;\n }\n}\n\n// ---------------------------------------------------------------------------\n// Public imperative API (module-level)\n// ---------------------------------------------------------------------------\n\n/**\n * Announce a message to screen readers.\n *\n * Works anywhere — React components, event handlers, thunks, vanilla JS.\n * The singleton live region is lazily created on first call.\n *\n * @example\n * announce('5 results found');\n * announce('Error: upload failed', { assertiveness: 'assertive' });\n * announce('3 items selected', { batchId: 'selection-count' });\n * announce('Invalid email', { assertiveness: 'assertive', delay: 300 });\n */\nexport function announce(message: string, options?: AnnounceOptions): void;\nexport function announce(message: string, assertiveness?: Assertiveness): void;\nexport function announce(\n message: string,\n optionsOrAssertiveness?: AnnounceOptions | Assertiveness\n): void {\n const options: AnnounceOptions =\n typeof optionsOrAssertiveness === 'string'\n ? { assertiveness: optionsOrAssertiveness }\n : optionsOrAssertiveness ?? {};\n\n getInstance().announce(message, options);\n}\n\n/**\n * Clear all pending announcements.\n * @param assertiveness — if omitted, clears both polite and assertive logs.\n */\nexport function clearAnnouncer(assertiveness?: Assertiveness): void {\n instance?.clear(assertiveness);\n}\n\n/**\n * Remove the live-region container from the DOM and reset the singleton.\n * Typically called when the host app unmounts (via LiveAnnouncerProvider).\n */\nexport function destroyAnnouncer(): void {\n instance?.destroy();\n}\n\n/**\n * Check whether the live-region container is currently in the DOM.\n */\nexport function isAnnouncerAttached(): boolean {\n return instance?.isAttached() ?? false;\n}\n"],"names":["SR_ONLY_CLASS","BATCH_ATTR","MODAL_ANNOUNCER_ATTR","instance","getInstance","LiveAnnouncer","constructor","container","assertiveLog","politeLog","cleanupTimers","Map","delayTimers","pendingRafByBatchId","ready","pendingQueue","document","createElement","dataset","liveAnnouncer","className","createLog","appendChild","body","prepend","isTestEnvironment","setTimeout","flushPendingQueue","SAFARI_INIT_DELAY","announce","message","options","resolved","assertiveness","timeout","DEFAULT_TIMEOUT","batchId","delay","pendingTimer","get","clearTimeout","delete","push","key","Date","now","Math","random","timer","doAnnounce","set","clear","clearLog","destroy","forEach","rafId","cancelAnimationFrame","remove","isAttached","isConnected","ariaLive","log","setAttribute","getLogForContext","modal","findVisibleModal","modalAnnouncer","querySelector","logs","querySelectorAll","modals","m","el","closest","getComputedStyle","visibility","hadExisting","existing","CSS","escape","removeNode","node","textContent","appendNode","pendingRaf","undefined","requestAnimationFrame","Array","from","children","child","innerHTML","queued","splice","IS_REACT_ACT_ENVIRONMENT","jest","vitest","optionsOrAssertiveness","clearAnnouncer","destroyAnnouncer"],"mappings":";;;AAyCA,MAAMA,IAAgB,WAGhBC,IAAa,iBAGbC,IAAuB;AAM7B,IAAIC,IAAiC;AAMrC,SAASC,IAA6B;AACpC,SAAKD,MACHA,IAAW,IAAIE,EAAAA,IAEVF;AACT;AAMA,MAAME,EAAc;AAAA,EAUlBC,cAAc;AATNC,IAAAA,EAAAA,mBAAmC;AACnCC,IAAAA,EAAAA,sBAAsC;AACtCC,IAAAA,EAAAA,mBAAmC;AACnCC,IAAAA,EAAAA,2CAAqEC,IAAAA;AACrEC,IAAAA,EAAAA,yCAA8DD,IAAAA;AAC9DE,IAAAA,EAAAA,iDAA+CF,IAAAA;AAC/CG,IAAAA,EAAAA,eAAQ;AACRC,IAAAA,EAAAA,sBAA+E,CAAA;AAGrF,IAAI,OAAOC,WAAa,QAExB,KAAKT,YAAYS,SAASC,cAAc,KAAK,GAC7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A,IAAAA,MAAc,KAAKkB,WAAWnB,GAASC,CAAO,CAAC;AAAA,EAC5E;AAAA,EAEQN,oBAA6B;AACnC,QAAI;AAQF,UALI,OAAOqE,4BAA6B,aAAaA,4BAGjD,OAAOC,OAAS,OAEhB,OAAOC,SAAW,IAAa,QAAO;AAAA,IAC5C,QAAQ;AAAA,IACN;AAEF,WAAO;AAAA,EACT;AACF;AAoBO,SAASnE,EACdC,GACAmE,GACM;AACN,QAAMlE,IACJ,OAAOkE,KAA2B,WAC9B;AAAA,IAAEhE,eAAegE;AAAAA,EAAAA,IACjBA,KAA0B,CAAA;AAEhC7F,EAAAA,IAAcyB,SAASC,GAASC,CAAO;AACzC;AAMO,SAASmE,EAAejE,GAAqC;AAClE9B,EAAAA,GAAUgD,MAAMlB,CAAa;AAC/B;AAMO,SAASkE,IAAyB;AACvChG,EAAAA,GAAUkD,QAAAA;AACZ;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "se-design",
3
- "version": "1.0.83-dev.5",
3
+ "version": "1.0.84-dev.0",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "exports": {