From 7578d48d3dcf75fc6a36b6486781d4c8ccec16a3 Mon Sep 17 00:00:00 2001 From: Lucas Berger Date: Thu, 18 Jun 2026 13:56:32 -0400 Subject: [PATCH] fix(17): IN-01 pull focus back into dialog when activeElement escapes --- apps/pwa/src/hooks/useFocusTrap.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/apps/pwa/src/hooks/useFocusTrap.ts b/apps/pwa/src/hooks/useFocusTrap.ts index 12fb791..e55c725 100644 --- a/apps/pwa/src/hooks/useFocusTrap.ts +++ b/apps/pwa/src/hooks/useFocusTrap.ts @@ -43,6 +43,16 @@ export function useFocusTrap( const first = focusable[0]; const last = focusable[focusable.length - 1]; + // Containment guard (IN-01): if focus has somehow landed outside the dialog + // (programmatic focus, browser quirk), neither boundary condition below + // matches and Tab would walk background content. Pull focus back to the + // first focusable instead of relying on a boundary-only trap. + if (!dialogRef.current.contains(document.activeElement)) { + e.preventDefault(); + first.focus(); + return; + } + if (e.shiftKey) { // Shift+Tab: if on first element, wrap to last if (document.activeElement === first) {