mirror of
https://github.com/zadam/trilium.git
synced 2025-10-20 15:19:01 +02:00
fix(react/dialogs): restore focus after modal is dismissed
This commit is contained in:
parent
fd1ea05c78
commit
2d3b99c959
@ -69,6 +69,7 @@ export default function Modal({ children, className, size, title, header, footer
|
|||||||
const modalInstanceRef = useRef<BootstrapModal>();
|
const modalInstanceRef = useRef<BootstrapModal>();
|
||||||
const formRef = _formRef ?? useRef<HTMLFormElement>(null);
|
const formRef = _formRef ?? useRef<HTMLFormElement>(null);
|
||||||
const parentWidget = useContext(ParentComponent);
|
const parentWidget = useContext(ParentComponent);
|
||||||
|
const elementToFocus = useRef<Element | null>();
|
||||||
|
|
||||||
if (onShown || onHidden) {
|
if (onShown || onHidden) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -79,16 +80,17 @@ export default function Modal({ children, className, size, title, header, footer
|
|||||||
if (onShown) {
|
if (onShown) {
|
||||||
modalElement.addEventListener("shown.bs.modal", onShown);
|
modalElement.addEventListener("shown.bs.modal", onShown);
|
||||||
}
|
}
|
||||||
if (onHidden) {
|
modalElement.addEventListener("hidden.bs.modal", () => {
|
||||||
modalElement.addEventListener("hidden.bs.modal", onHidden);
|
onHidden();
|
||||||
|
if (elementToFocus.current && "focus" in elementToFocus.current) {
|
||||||
|
(elementToFocus.current as HTMLElement).focus();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
return () => {
|
return () => {
|
||||||
if (onShown) {
|
if (onShown) {
|
||||||
modalElement.removeEventListener("shown.bs.modal", onShown);
|
modalElement.removeEventListener("shown.bs.modal", onShown);
|
||||||
}
|
}
|
||||||
if (onHidden) {
|
|
||||||
modalElement.removeEventListener("hidden.bs.modal", onHidden);
|
modalElement.removeEventListener("hidden.bs.modal", onHidden);
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}, [ ]);
|
}, [ ]);
|
||||||
}
|
}
|
||||||
@ -98,6 +100,7 @@ export default function Modal({ children, className, size, title, header, footer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (show) {
|
if (show) {
|
||||||
|
elementToFocus.current = document.activeElement;
|
||||||
openDialog(parentWidget.$widget, !stackable).then(($widget) => {
|
openDialog(parentWidget.$widget, !stackable).then(($widget) => {
|
||||||
modalInstanceRef.current = BootstrapModal.getOrCreateInstance($widget[0]);
|
modalInstanceRef.current = BootstrapModal.getOrCreateInstance($widget[0]);
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user