From 6bd331bc73b4196337662371c0a4f4db5aa616e0 Mon Sep 17 00:00:00 2001 From: ajnart Date: Thu, 30 Jul 2026 14:33:29 +0200 Subject: [PATCH] fix: preserve assistant trigger focus --- .../src/components/assistant/assistant-panel.tsx | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) 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]);