Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): clear stale lifted-dialog backdrop z-index on reuse

Elian Doranaa4fd5ed15fb
diff --git a/apps/client/src/services/dialog.spec.ts b/apps/client/src/services/dialog.spec.ts
index de0a49984b..922a59879d 100644
--- a/apps/client/src/services/dialog.spec.ts
+++ b/apps/client/src/services/dialog.spec.ts
@@ -174,6 +174,34 @@ describe("dialog service", () => {
expect(backdrop.style.zIndex).toBe("1109");
});
+ it("clears a stale backdrop z-index left by a prior lift when reopened without a popup", async () => {
+ // Bootstrap reuses the same backdrop element across shows; simulate one carrying the inline
+ // z-index a previous lift assigned.
+ const backdrop = document.createElement("div");
+ backdrop.className = "modal-backdrop";
+ backdrop.style.zIndex = "1109";
+ document.body.appendChild(backdrop);
+
+ const $dialog = makeDialog().appendTo(document.body);
+ await openDialog($dialog, true); // no stacked popup this time
+
+ expect($dialog[0].style.zIndex).toBe("");
+ expect(backdrop.style.zIndex).toBe("");
+ });
+
+ it("does not touch backdrops when the dialog itself has none", async () => {
+ const backdrop = document.createElement("div");
+ backdrop.className = "modal-backdrop";
+ backdrop.style.zIndex = "1109";
+ document.body.appendChild(backdrop);
+
+ const $dialog = makeDialog().appendTo(document.body);
+ await openDialog($dialog, true, { backdrop: false });
+
+ // Another modal's backdrop must be left alone.
+ expect(backdrop.style.zIndex).toBe("1109");
+ });
+
it("does not lift when no popup is stacked", async () => {
openStackedPopup("999"); // popup present but body lacks the -stacked class (non-stacked case)
diff --git a/apps/client/src/services/dialog.ts b/apps/client/src/services/dialog.ts
index ec117ed08a..8b97a9c08a 100644
--- a/apps/client/src/services/dialog.ts
+++ b/apps/client/src/services/dialog.ts
@@ -20,13 +20,17 @@ export async function openDialog($dialog: JQuery<HTMLElement>, closeActDialog =
Modal.getOrCreateInstance($dialog[0], config).show();
- // Bootstrap appends the backdrop during show(); raise it alongside the lifted dialog so the popup
- // behind is dimmed and click-blocked rather than remaining interactive.
- if (bumpedZIndex !== null) {
+ // Normalise the just-shown dialog's backdrop z-index. Bootstrap appends the backdrop during
+ // show(), and reuses the *same* element across shows of a kept-in-DOM modal — so a lift applied on
+ // a previous open would otherwise persist as a stale inline z-index and leave the backdrop
+ // floating above unrelated content on a later, non-lifted open. Always set it: raised alongside a
+ // lifted dialog (so the popup behind is dimmed and click-blocked), or cleared back to the default
+ // layer otherwise. Skipped when this dialog has no backdrop, so we never touch another modal's.
+ if (config?.backdrop !== false) {
const backdrops = document.querySelectorAll<HTMLElement>(".modal-backdrop");
const ownBackdrop = backdrops[backdrops.length - 1];
if (ownBackdrop) {
- ownBackdrop.style.zIndex = String(bumpedZIndex - 1);
+ ownBackdrop.style.zIndex = bumpedZIndex !== null ? String(bumpedZIndex - 1) : "";
}
}