Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(client): no cursor when editing custom dictionary in stacked modal
Elian Dorane605aa7ce7e3
diff --git a/apps/client/src/widgets/react/Modal.tsx b/apps/client/src/widgets/react/Modal.tsxindex 1d0fa140fa..3b8c550c17 100644--- a/apps/client/src/widgets/react/Modal.tsx+++ b/apps/client/src/widgets/react/Modal.tsx@@ -142,6 +142,15 @@ export default function Modal({ children, className, size, title, customTitleBar}}, [ show, modalRef.current, noFocus, zIndex ]);+ // While this modal is shown, ensure it is the only modal trapping focus. Bootstrap has no stacked+ // modal support: every underlying modal keeps its own focus-trap active and steals focus from inputs+ // in the modal on top (e.g. the custom-dictionary editor in the quick-edit popup that opens over the+ // Options dialog gets no cursor). Suspend the other modals' traps here and restore them on close.+ useEffect(() => {+ if (!show || !modalRef.current) return;+ return suspendOtherModalFocusTraps(modalRef.current);+ }, [ show ]);+// Memoize styles to prevent recreation on every renderconst dialogStyle = useMemo<CSSProperties>(() => {const style: CSSProperties = {};@@ -219,6 +228,48 @@ export default function Modal({ children, className, size, title, customTitleBar);}+/** Bootstrap's private focus-trap, reachable via `Modal._focustrap` (see {@link suspendOtherModalFocusTraps}). */+interface BootstrapFocusTrap {+ activate(): void;+ deactivate(): void;+ _isActive: boolean;+}++/**+ * Suspends the focus-traps of every currently-shown modal other than {@link ownElement}, returning a+ * function that reactivates exactly those that were suspended.+ *+ * Bootstrap activates a document-wide `focusin` focus-trap on each modal opened with `focus: true`, and+ * that trap yanks focus back to its own modal whenever focus lands elsewhere ({@link https://github.com/twbs/bootstrap/blob/main/js/src/util/focustrap.js focustrap.js}).+ * Bootstrap does not support stacked modals, so an underlying modal's trap fights the modal stacked on+ * top of it — e.g. clicking into the custom-dictionary CKEditor in the quick-edit popup (which opens over+ * the Options dialog) immediately loses focus, so there is no cursor and typing does nothing.+ *+ * Enforcing "only the topmost modal traps focus" fixes this: each modal, on show, suspends the traps of+ * the modals below it and, on close, restores precisely those it suspended. Because only currently-active+ * traps are captured, restoring in reverse open order re-establishes the invariant at every stack depth.+ * Reaches into `Modal._focustrap` since Bootstrap exposes no public API for this.+ */+function suspendOtherModalFocusTraps(ownElement: HTMLElement): () => void {+ const suspended: BootstrapFocusTrap[] = [];+ for (const modalElement of document.querySelectorAll<HTMLElement>(".modal.show")) {+ if (modalElement === ownElement) continue;++ const instance = BootstrapModal.getInstance(modalElement) as (BootstrapModal & { _focustrap?: BootstrapFocusTrap }) | null;+ const focustrap = instance?._focustrap;+ if (focustrap?._isActive) {+ focustrap.deactivate();+ suspended.push(focustrap);+ }+ }++ return () => {+ for (const focustrap of suspended) {+ focustrap.activate();+ }+ };+}+function ModalMain({ sidebar, children }: { sidebar: boolean; children: ComponentChildren }) {if (sidebar) {return <div className="modal-main">{children}</div>;