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): focus search box in portaled dropdown opened inside a modal
Elian Doran4ef35a2da5fb
diff --git a/apps/client/src/widgets/react/Dropdown.tsx b/apps/client/src/widgets/react/Dropdown.tsxindex ba89354a95..7a079fd496 100644--- a/apps/client/src/widgets/react/Dropdown.tsx+++ b/apps/client/src/widgets/react/Dropdown.tsx@@ -5,6 +5,7 @@ import { MutableRef, useCallback, useEffect, useLayoutEffect, useMemo, useRef, uimport { isMobile } from "../../services/utils";import { useTooltip, useUniqueName } from "./hooks";+import { suspendModalFocusTraps } from "./modal_focustrap";type DataAttributes = {[key: `data-${string}`]: string | number | boolean | undefined;@@ -137,6 +138,14 @@ export default function Dropdown({ id, className, buttonClassName, isStatic, chi}}, [ mobileBackdrop ]);+ // A portaled menu lives in `document.body`, outside any modal that opened it. That modal's focus-trap+ // would keep yanking focus back into the modal, so an input in the menu (e.g. the note-icon picker's+ // search box) could never hold focus. Suspend the shown modals' traps while the menu is open.+ useEffect(() => {+ if (!portalToBody || !shown) return;+ return suspendModalFocusTraps();+ }, [ portalToBody, shown ]);+useEffect(() => {if (!containerRef.current) return;if (externalContainerRef) externalContainerRef.current = containerRef.current;diff --git a/apps/client/src/widgets/react/Modal.tsx b/apps/client/src/widgets/react/Modal.tsxindex 3b8c550c17..771b69ee69 100644--- a/apps/client/src/widgets/react/Modal.tsx+++ b/apps/client/src/widgets/react/Modal.tsx@@ -10,6 +10,7 @@ import { openDialog } from "../../services/dialog";import { t } from "../../services/i18n";import { openInAppHelpFromUrl } from "../../services/utils";import { useSyncedRef } from "./hooks";+import { suspendModalFocusTraps } from "./modal_focustrap";import { ContainerVisibilityContext } from "./react_utils";interface CustomTitleBarButton {@@ -148,7 +149,7 @@ export default function Modal({ children, className, size, title, customTitleBar// 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);+ return suspendModalFocusTraps(modalRef.current);}, [ show ]);// Memoize styles to prevent recreation on every render@@ -228,48 +229,6 @@ 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>;diff --git a/apps/client/src/widgets/react/modal_focustrap.ts b/apps/client/src/widgets/react/modal_focustrap.tsnew file mode 100644index 0000000000..3a245d7894--- /dev/null+++ b/apps/client/src/widgets/react/modal_focustrap.ts@@ -0,0 +1,47 @@+import { Modal as BootstrapModal } from "bootstrap";++/** Bootstrap's private focus-trap, reachable via `Modal._focustrap` (see {@link suspendModalFocusTraps}). */+interface BootstrapFocusTrap {+ activate(): void;+ deactivate(): void;+ _isActive: boolean;+}++/**+ * Suspends the focus-traps of every currently-shown modal (optionally excluding {@link exceptElement}),+ * 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}).+ * That fights UI a modal genuinely owns but which renders outside its DOM:+ *+ * - **Stacked modals** — Bootstrap has no stacked-modal support, so an underlying modal's trap fights the+ * modal stacked on top of it (e.g. the custom-dictionary editor in the quick-edit popup that opens over+ * the Options dialog gets no cursor). Each modal, on show, calls this with itself as `exceptElement` to+ * suspend the traps of the modals below it, restoring them on close.+ * - **Portaled dropdowns** — a dropdown menu portaled to `document.body` (e.g. the note-icon picker) lives+ * outside the modal that opened it, so its search input immediately loses focus. The dropdown suspends+ * every shown modal's trap while open (no exception, since it floats above the whole stack).+ *+ * Because only currently-active traps are captured, restoring re-establishes the invariant at every stack+ * depth. Reaches into `Modal._focustrap` since Bootstrap exposes no public API for this.+ */+export function suspendModalFocusTraps(exceptElement?: HTMLElement | null): () => void {+ const suspended: BootstrapFocusTrap[] = [];+ for (const modalElement of document.querySelectorAll<HTMLElement>(".modal.show")) {+ if (modalElement === exceptElement) 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();+ }+ };+}