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(dialogs): suspend the focus-traps below a dialog before opening it
Elian Doranb6764a71b247
diff --git a/apps/client/src/widgets/react/Modal.spec.tsx b/apps/client/src/widgets/react/Modal.spec.tsxindex c0cbebe1f9..c92c2eb696 100644--- a/apps/client/src/widgets/react/Modal.spec.tsx+++ b/apps/client/src/widgets/react/Modal.spec.tsx@@ -3,10 +3,23 @@ import { useState } from "preact/hooks";import { act } from "preact/test-utils";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-const mocks = vi.hoisted(() => ({- openDialog: vi.fn(async (dialog: JQuery<HTMLElement>) => dialog),- hide: vi.fn()-}));+const mocks = vi.hoisted(() => {+ // What a dialog did on its way up, in the order it did it (see "suspends the focus-traps ...").+ const raising: string[] = [];++ return {+ raising,+ openDialog: vi.fn(async (dialog: JQuery<HTMLElement>) => {+ raising.push("openDialog");+ return dialog;+ }),+ hide: vi.fn(),+ suspendModalFocusTraps: vi.fn(() => {+ raising.push("suspendModalFocusTraps");+ return () => {};+ })+ };+});vi.mock("../../services/dialog", () => ({openDialog: mocks.openDialog@@ -30,7 +43,7 @@ vi.mock("../../components/app_context", () => ({}));vi.mock("./modal_focustrap", () => ({- suspendModalFocusTraps: () => () => {}+ suspendModalFocusTraps: mocks.suspendModalFocusTraps}));import Modal from "./Modal";@@ -42,6 +55,8 @@ beforeEach(() => {document.body.appendChild(container);mocks.openDialog.mockClear();mocks.hide.mockClear();+ mocks.suspendModalFocusTraps.mockClear();+ mocks.raising.length = 0;});afterEach(() => {@@ -142,6 +157,21 @@ describe("Modal", () => {expect(dialog?.classList.contains("show")).toBe(true);});+ /**+ * `openDialog` moves focus onto the new backdrop synchronously, and a focus-trap still active on+ * the dialog underneath pulls it straight back in. Over the Options dialog that lands in the+ * settings search field, whose focus handler shows the search results in place of the page being+ * configured — unmounting the dialog just opened and leaving its portaled markup on screen with+ * nothing behind it. Every card in the "Add AI provider" wizard answered a click with nothing.+ */+ it("suspends the underlying focus-traps before opening", async () => {+ await act(async () => {+ render(<DialogWithTypedInto show onHidden={() => {}} />, container);+ });++ expect(mocks.raising).toEqual([ "suspendModalFocusTraps", "openDialog" ]);+ });+it("opens a dialog that starts closed only once it is asked to show", async () => {const onHidden = vi.fn();diff --git a/apps/client/src/widgets/react/Modal.tsx b/apps/client/src/widgets/react/Modal.tsxindex 5d1cc70b4e..88dc3336d9 100644--- a/apps/client/src/widgets/react/Modal.tsx+++ b/apps/client/src/widgets/react/Modal.tsx@@ -172,6 +172,18 @@ export default function Modal({ children, className, size, title, customTitleBar};}, [ onShown, onHidden ]);+ // 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.+ //+ // Must run before the effect that opens the dialog: `openDialog` focuses the new backdrop, and+ // a trap still active at that point pulls focus into the modal underneath.+ useEffect(() => {+ if (!show || !modalRef.current) return;+ return suspendModalFocusTraps(modalRef.current);+ }, [ show ]);+useEffect(() => {if (show && modalRef.current) {elementToFocus.current = document.activeElement;@@ -237,15 +249,6 @@ export default function Modal({ children, className, size, title, customTitleBarmodalInstanceRef.current?.hide();}, []);- // 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 suspendModalFocusTraps(modalRef.current);- }, [ show ]);-// Memoize styles to prevent recreation on every renderconst dialogStyle = useMemo<CSSProperties>(() => {const style: CSSProperties = {};