fix: preserve assistant trigger focus
This commit is contained in:
1 parent
ee08870925
commit
6bd331bc73
1 file changed
+12
-3
@@ -1153,10 +1153,19 @@ export const AssistantPanel = ({
|
|||||||
if (opened && event.key === "Escape") onClose();
|
if (opened && event.key === "Escape") onClose();
|
||||||
});
|
});
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!opened) return;
|
if (!opened) {
|
||||||
previousFocusRef.current = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
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 () => {
|
return () => {
|
||||||
previousFocusRef.current?.focus();
|
restoreTarget?.focus();
|
||||||
previousFocusRef.current = null;
|
previousFocusRef.current = null;
|
||||||
};
|
};
|
||||||
}, [opened]);
|
}, [opened]);
|
||||||
|
|||||||
Reference in new issue
Block a user