Skip to content

Build your personal knowledge base with Trilium Notes

fix(modal): say it again when a dialog is told to close as it opens

Elian Dorancb66d28b549f
diff --git a/apps/client/src/widgets/react/Modal.tsx b/apps/client/src/widgets/react/Modal.tsx
index 9584c3df29..651cb32dac 100644
--- a/apps/client/src/widgets/react/Modal.tsx
+++ b/apps/client/src/widgets/react/Modal.tsx
@@ -145,7 +145,28 @@ export default function Modal({ children, className, size, title, customTitleBar
modalInstanceRef.current = BootstrapModal.getOrCreateInstance($widget[0]);
});
} else {
- modalInstanceRef.current?.hide();
+ const instance = modalInstanceRef.current;
+ instance?.hide();
+
+ /*
+ * Bootstrap drops a `hide()` made while the dialog is still opening — it is guarded on
+ * an internal `_isTransitioning`, which stands for the ~300ms the opening takes — and
+ * says nothing about having dropped it. A dialog told to close within that window (a
+ * picker closing itself on the first thing tapped, say) would be left open for good,
+ * and since our own tree has already emptied it, what stays on the screen is an empty
+ * sheet and its backdrop over a page that can no longer be pressed.
+ *
+ * A `hide()` that was carried out takes the class off at once, so a dialog still
+ * wearing it is one whose closing was dropped. Say it again the moment the opening
+ * lands, which is what `shown.bs.modal` reports.
+ */
+ const modalElement = modalRef.current;
+ if (instance && modalElement?.classList.contains("show")) {
+ const hideOnceShown = () => instance.hide();
+ modalElement.addEventListener("shown.bs.modal", hideOnceShown, { once: true });
+ // Dropped in turn if the dialog is asked to open again before the first one lands.
+ return () => modalElement.removeEventListener("shown.bs.modal", hideOnceShown);
+ }
}
}, [ show, modalRef.current, noFocus, zIndex ]);
diff --git a/packages/trilium-e2e/src/note_icon.spec.ts b/packages/trilium-e2e/src/note_icon.spec.ts
new file mode 100644
index 0000000000..f2a170c0dc
--- /dev/null
+++ b/packages/trilium-e2e/src/note_icon.spec.ts
@@ -0,0 +1,47 @@
+import { expect,test } from "@playwright/test";
+
+import App from "./support/app";
+
+/**
+ * On a phone an icon is picked from a modal of its own rather than from a menu (see IconPicker), and
+ * picking one closes that modal — which is asked for as soon as the first icon is tapped, sometimes
+ * while the modal is still opening. Bootstrap ignores a dialog told to close mid-opening, so what
+ * used to be left behind was an emptied dialog and its backdrop: a sheet of nothing over the whole
+ * page, swallowing every press on it.
+ */
+test("An icon picked while its modal is still opening takes the modal away with it", async ({ page, context }) => {
+ await page.setViewportSize({ width: 412, height: 900 });
+ const app = new App(page, context);
+ // A note of the shared database, straight from its address: a phone opens on no note at all, and
+ // the icon of no note is not there to be picked (see NoteIcon).
+ await app.goto({ isMobile: true, url: "/#root/qlLRRwU3qlkR" });
+
+ // The opening lasts a fraction of a second, which a test cannot reliably tap inside of; slow it
+ // right down, so that the picker's own closing always lands within it.
+ const slowedOpening = await page.addStyleTag({
+ content: ".modal.fade .modal-dialog { transition-duration: 4s !important; }"
+ });
+
+ const noteIcon = app.currentNoteSplit.locator(".note-icon-widget button.note-icon").first();
+ const modal = page.locator(".modal.icon-switcher.show");
+ // Enabled once the note is open: the icon of no note cannot be picked (see NoteIcon).
+ await expect(noteIcon).toBeEnabled();
+ await noteIcon.click();
+ await modal.locator(".icon-list span.tn-icon").first().click();
+
+ // Nothing of the dialog may outlive it.
+ await expect(modal).toBeHidden({ timeout: 15000 });
+ await expect(page.locator(".modal-backdrop")).toHaveCount(0);
+ await expect(page.locator("body.modal-open")).toHaveCount(0);
+ // What is under it can be pressed again, which is what the leftover backdrop took away.
+ await expect(noteIcon).toBeVisible();
+
+ // Leave the note wearing the icon it was found in, the database being shared with every other
+ // test here. The dialog opens at its own pace again for it.
+ await slowedOpening.evaluate((style) => style.parentNode?.removeChild(style));
+ await noteIcon.click();
+ await expect(modal).toBeVisible();
+ await modal.locator(".filter-row button.bx-dots-vertical-rounded").click();
+ await page.locator(".dropdown-menu.show .dropdown-item").first().click();
+ await expect(modal).toBeHidden();
+});