|
| 1 | +/** |
| 2 | + * Stack of popover/modal launcher elements — the element that opened a focus trap. Top is the most recent. |
| 3 | + * pickLauncher prefers the topmost active entry, else the most recent deactivated-within-LAUNCHER_CLEAR_DELAY_MS. |
| 4 | + */ |
| 5 | + |
| 6 | +// deactivatedAt is set on trap close; entry lives LAUNCHER_CLEAR_DELAY_MS so deferred-nav popovers can still consume it. |
| 7 | +type LauncherEntry = {element: HTMLElement; deactivatedAt?: number}; |
| 8 | + |
| 9 | +// Covers click → state-listener → captureTriggerForRoute on slow devices. |
| 10 | +const LAUNCHER_CLEAR_DELAY_MS = 1000; |
| 11 | +const LAUNCHER_STACK_MAX = 8; |
| 12 | + |
| 13 | +// Stack (not slot) so nested + sequential traps retain correct launcher context. |
| 14 | +const launcherStack: LauncherEntry[] = []; |
| 15 | +let hasWarnedAboutOverflow = false; |
| 16 | + |
| 17 | +// Two passes so nested traps resolve to the outer (active) launcher, not the just-closed inner. |
| 18 | +function pickLauncher(): HTMLElement | null { |
| 19 | + if (typeof document === 'undefined') { |
| 20 | + return null; |
| 21 | + } |
| 22 | + // Monotonic — Date.now() would misbehave on clock jumps. |
| 23 | + const now = performance.now(); |
| 24 | + for (let i = launcherStack.length - 1; i >= 0; i -= 1) { |
| 25 | + const entry = launcherStack.at(i); |
| 26 | + if (!entry) { |
| 27 | + continue; |
| 28 | + } |
| 29 | + if (!document.contains(entry.element)) { |
| 30 | + launcherStack.splice(i, 1); |
| 31 | + continue; |
| 32 | + } |
| 33 | + if (entry.deactivatedAt === undefined) { |
| 34 | + return entry.element; |
| 35 | + } |
| 36 | + } |
| 37 | + for (let i = launcherStack.length - 1; i >= 0; i -= 1) { |
| 38 | + const entry = launcherStack.at(i); |
| 39 | + if (entry?.deactivatedAt === undefined) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + if (!document.contains(entry.element)) { |
| 43 | + launcherStack.splice(i, 1); |
| 44 | + continue; |
| 45 | + } |
| 46 | + if (now - entry.deactivatedAt > LAUNCHER_CLEAR_DELAY_MS) { |
| 47 | + launcherStack.splice(i, 1); |
| 48 | + continue; |
| 49 | + } |
| 50 | + return entry.element; |
| 51 | + } |
| 52 | + return null; |
| 53 | +} |
| 54 | + |
| 55 | +function consumeLauncher(element: HTMLElement): void { |
| 56 | + const idx = launcherStack.findIndex((e) => e.element === element); |
| 57 | + if (idx >= 0) { |
| 58 | + launcherStack.splice(idx, 1); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function setActivePopoverLauncher(element: HTMLElement): void { |
| 63 | + if (typeof document === 'undefined') { |
| 64 | + return; |
| 65 | + } |
| 66 | + // Reactivation must move the entry to the tail — pickLauncher scans end-first, so leaving a reactivated entry mid-stack lets newer (still-active) entries shadow it. |
| 67 | + const existingIdx = launcherStack.findIndex((e) => e.element === element); |
| 68 | + if (existingIdx >= 0) { |
| 69 | + launcherStack.splice(existingIdx, 1); |
| 70 | + } |
| 71 | + launcherStack.push({element}); |
| 72 | + if (launcherStack.length > LAUNCHER_STACK_MAX) { |
| 73 | + if (!hasWarnedAboutOverflow) { |
| 74 | + hasWarnedAboutOverflow = true; |
| 75 | + // Once-per-session so a pathological trap loop doesn't spam dev logs. |
| 76 | + // eslint-disable-next-line no-console |
| 77 | + console.warn('[NavigationFocusReturn] launcherStack overflow — dropping oldest entry'); |
| 78 | + } |
| 79 | + launcherStack.shift(); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** Mark a launcher (or top-of-stack) as deactivated. pickLauncher lazy-prunes on LAUNCHER_CLEAR_DELAY_MS. */ |
| 84 | +function scheduleClearActivePopoverLauncher(element?: HTMLElement): void { |
| 85 | + if (typeof document === 'undefined') { |
| 86 | + return; |
| 87 | + } |
| 88 | + const index = element ? launcherStack.findIndex((e) => e.element === element) : launcherStack.length - 1; |
| 89 | + if (index < 0) { |
| 90 | + return; |
| 91 | + } |
| 92 | + // Splice-then-push so end-first scan returns the most-recently-deactivated (correct for nested-trap close: outer closes after inner). |
| 93 | + const [entry] = launcherStack.splice(index, 1); |
| 94 | + entry.deactivatedAt = performance.now(); |
| 95 | + launcherStack.push(entry); |
| 96 | +} |
| 97 | + |
| 98 | +function resetLauncherStackForTests(): void { |
| 99 | + launcherStack.length = 0; |
| 100 | + hasWarnedAboutOverflow = false; |
| 101 | +} |
| 102 | + |
| 103 | +export {pickLauncher, consumeLauncher, setActivePopoverLauncher, scheduleClearActivePopoverLauncher, resetLauncherStackForTests, LAUNCHER_CLEAR_DELAY_MS, LAUNCHER_STACK_MAX}; |
0 commit comments