Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): stop a dialog raised already open from closing itself

Elian Doran6de6b6d0d43e
diff --git a/apps/client/src/widgets/react/Modal.spec.tsx b/apps/client/src/widgets/react/Modal.spec.tsx
new file mode 100644
index 0000000000..5cbd386186
--- /dev/null
+++ b/apps/client/src/widgets/react/Modal.spec.tsx
@@ -0,0 +1,125 @@
+import { render } from "preact";
+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()
+}));
+
+vi.mock("../../services/dialog", () => ({
+ openDialog: mocks.openDialog
+}));
+
+// Only what is reached while a dialog is raised: its own instance, and the tooltip class the
+// shared hooks patch as they load (see hooks.tsx).
+vi.mock("bootstrap", () => ({
+ Modal: { getOrCreateInstance: () => ({ hide: mocks.hide }) },
+ Tooltip: class {
+ static getInstance() { return null; }
+ dispose() {}
+ },
+ Dropdown: class {}
+}));
+
+vi.mock("../../services/i18n", () => ({ t: (key: string) => key }));
+
+vi.mock("../../components/app_context", () => ({
+ default: { triggerCommand: vi.fn() }
+}));
+
+vi.mock("./modal_focustrap", () => ({
+ suspendModalFocusTraps: () => () => {}
+}));
+
+import Modal from "./Modal";
+
+let container: HTMLDivElement;
+
+beforeEach(() => {
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ mocks.openDialog.mockClear();
+ mocks.hide.mockClear();
+});
+
+afterEach(() => {
+ render(null, container);
+ container.remove();
+});
+
+/**
+ * A dialog holding something that changes, so that a test can make it render again without
+ * touching the dialog's own props — a keystroke into what it holds, as far as the dialog knows.
+ */
+function DialogWithTypedInto({ show, onHidden }: { show: boolean; onHidden(): void }) {
+ const [ typed, setTyped ] = useState("");
+
+ return (
+ <Modal className="test-dialog" size="md" show={show} onHidden={onHidden}>
+ {/* Named, the dialog bringing a close button of its own that stands before it. */}
+ <button className="typed-into" onClick={() => setTyped(`${typed}x`)}>{typed}</button>
+ </Modal>
+ );
+}
+
+describe("Modal", () => {
+ /**
+ * The bug this pins: `modalRef.current` stood among the effect's dependencies, and a ref holds
+ * nothing while the render that names them runs and the element by the time the effect does —
+ * so the first re-render after mounting read as a change and opened the dialog a second time.
+ * Opening closes whichever dialog is active, which by then is this one, and the dialog put
+ * itself away on the first keystroke typed into it (the calendar's ghost, where it was found).
+ */
+ it("opens once, however often what it holds is typed into", async () => {
+ const onHidden = vi.fn();
+
+ await act(async () => {
+ render(<DialogWithTypedInto show onHidden={onHidden} />, container);
+ });
+ expect(mocks.openDialog).toHaveBeenCalledTimes(1);
+
+ // Twice over: the first re-render is the one the ref's settling used to spoil, and a second
+ // says the dependencies have come to rest rather than merely changed the once.
+ for (let press = 0; press < 2; press++) {
+ await act(async () => {
+ container.querySelector("button.typed-into")?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ });
+ // The press must actually have re-rendered the dialog, or the test proves nothing.
+ expect(container.querySelector("button.typed-into")?.textContent).toBe("x".repeat(press + 1));
+ expect(mocks.openDialog).toHaveBeenCalledTimes(1);
+ }
+
+ expect(mocks.hide).not.toHaveBeenCalled();
+ });
+
+ /**
+ * A dialog taken out of the tree while still up leaves Bootstrap's backdrop on the body, there
+ * being nothing left to take it away — a dimmed page that cannot be pressed. The calendar's
+ * ghost goes exactly that way once its event is made.
+ */
+ it("puts itself down when it is taken out of the tree while still up", async () => {
+ await act(async () => {
+ render(<DialogWithTypedInto show onHidden={() => {}} />, container);
+ });
+ expect(mocks.hide).not.toHaveBeenCalled();
+
+ await act(async () => { render(null, container); });
+ expect(mocks.hide).toHaveBeenCalled();
+ });
+
+ it("opens a dialog that starts closed only once it is asked to show", async () => {
+ const onHidden = vi.fn();
+
+ await act(async () => {
+ render(<DialogWithTypedInto show={false} onHidden={onHidden} />, container);
+ });
+ expect(mocks.openDialog).not.toHaveBeenCalled();
+
+ await act(async () => {
+ render(<DialogWithTypedInto show onHidden={onHidden} />, container);
+ });
+ expect(mocks.openDialog).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/apps/client/src/widgets/react/Modal.tsx b/apps/client/src/widgets/react/Modal.tsx
index 651cb32dac..2ce40a6f77 100644
--- a/apps/client/src/widgets/react/Modal.tsx
+++ b/apps/client/src/widgets/react/Modal.tsx
@@ -168,7 +168,38 @@ export default function Modal({ children, className, size, title, customTitleBar
return () => modalElement.removeEventListener("shown.bs.modal", hideOnceShown);
}
}
- }, [ show, modalRef.current, noFocus, zIndex ]);
+ /*
+ * Not on `modalRef.current`, though the effect reads it. A ref holds nothing while the
+ * render that names the dependencies is running and the element by the time the effect
+ * runs, so listing it makes the first re-render after mounting look like a change and open
+ * the dialog a second time — and opening closes whatever dialog is active, which by then is
+ * this one (see openDialog). The dialog would put itself away on the first keystroke typed
+ * into it, telling its host it had been dismissed.
+ *
+ * A dialog that starts closed never showed it: `show` was false for those first renders, so
+ * the spurious run took the closing arm and did nothing, and by the time it opened the ref
+ * had long settled. Only one raised already open — the calendar's ghost — met it.
+ *
+ * Nothing is lost by leaving it out: the element the ref names is rendered unconditionally
+ * (see below), so it is always there by the time an effect could read it.
+ */
+ }, [ show, noFocus, zIndex ]);
+
+ /*
+ * A dialog may be taken out of the tree while it is still up — a host that closes it by ceasing
+ * to draw it rather than by turning `show` off, which is what a dialog raised for something
+ * that then goes does (the calendar's ghost, once its event is made). Bootstrap keeps the
+ * backdrop on the body rather than within the dialog, and the class that stills the page behind
+ * it likewise, so nothing would take either away: what is left is a dimmed page that cannot be
+ * pressed or scrolled, with no dialog on it to explain itself.
+ *
+ * Told to hide as it goes, Bootstrap sees its own teardown through even with the element
+ * detached — the transition it waits on has a timer behind it — and a dialog that was already
+ * hidden is told nothing, `hide()` being a no-op once it is down.
+ */
+ useEffect(() => () => {
+ modalInstanceRef.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