From dd44338cdc0b25c99e23986a2d72009e227bc0ad Mon Sep 17 00:00:00 2001 From: Josh Cohenzadeh Date: Tue, 24 Mar 2026 12:24:33 -0700 Subject: [PATCH] fix(nav): Restore focus to previous element after closing command palette The GlobalModal's focus-trap doesn't reliably return focus because the command palette input's autoFocus moves focus into the portal before the trap activates, causing it to store the wrong "previous" element. Explicitly save document.activeElement when opening the palette and restore it when the modal closes, regardless of close method. --- static/app/views/navigation/index.tsx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/static/app/views/navigation/index.tsx b/static/app/views/navigation/index.tsx index 09fc7cb06c35cb..fe80127ac710be 100644 --- a/static/app/views/navigation/index.tsx +++ b/static/app/views/navigation/index.tsx @@ -39,9 +39,14 @@ function UserAndOrganizationNavigation() { useGlobalCommandPaletteActions(); const commandPaletteOpenRef = useRef(false); + const previousFocusRef = useRef(null); useEffect(() => { - if (!visible) { + if (!visible && commandPaletteOpenRef.current) { + if (previousFocusRef.current instanceof HTMLElement) { + previousFocusRef.current.focus(); + } + previousFocusRef.current = null; commandPaletteOpenRef.current = false; } }, [visible]); @@ -55,6 +60,7 @@ function UserAndOrganizationNavigation() { if (visible && commandPaletteOpenRef.current) { closeModal(); } else if (!visible) { + previousFocusRef.current = document.activeElement; openCommandPalette(); commandPaletteOpenRef.current = true; }