diff --git a/apps/nextjs/src/components/assistant/assistant-panel.tsx b/apps/nextjs/src/components/assistant/assistant-panel.tsx index 23128ef24..45a56da74 100644 --- a/apps/nextjs/src/components/assistant/assistant-panel.tsx +++ b/apps/nextjs/src/components/assistant/assistant-panel.tsx @@ -1153,10 +1153,19 @@ export const AssistantPanel = ({ if (opened && event.key === "Escape") onClose(); }); useEffect(() => { - if (!opened) return; - previousFocusRef.current = document.activeElement instanceof HTMLElement ? document.activeElement : null; + if (!opened) { + const rememberOutsideFocus = () => { + if (document.activeElement instanceof HTMLElement) { + previousFocusRef.current = document.activeElement; + } + }; + rememberOutsideFocus(); + document.addEventListener("focusin", rememberOutsideFocus); + return () => document.removeEventListener("focusin", rememberOutsideFocus); + } + const restoreTarget = previousFocusRef.current; return () => { - previousFocusRef.current?.focus(); + restoreTarget?.focus(); previousFocusRef.current = null; }; }, [opened]);