Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): show dialogs opened from within the tree popup editor

Elian Doran6cd448942803
diff --git a/apps/client/src/services/dialog.spec.ts b/apps/client/src/services/dialog.spec.ts
index 3248b0ffa1..de0a49984b 100644
--- a/apps/client/src/services/dialog.spec.ts
+++ b/apps/client/src/services/dialog.spec.ts
@@ -1,4 +1,4 @@
-import { beforeEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import $ from "jquery";
// --- Mocks (hoisted above imports) ---
@@ -145,6 +145,64 @@ describe("dialog service", () => {
});
});
+ describe("lifting dialogs above a stacked popup", () => {
+ afterEach(() => {
+ document.body.className = "";
+ document.body.innerHTML = "";
+ });
+
+ function openStackedPopup(zIndex = "1100") {
+ const popup = document.createElement("div");
+ popup.className = "modal show tree-popup-editor-dialog";
+ popup.style.zIndex = zIndex;
+ document.body.appendChild(popup);
+ return popup;
+ }
+
+ it("lifts the dialog above the stacked popup and raises its backdrop just below it", async () => {
+ document.body.classList.add("tree-popup-stacked");
+ openStackedPopup("1100");
+ // The backdrop Bootstrap would append during show() (show is mocked, so pre-place it).
+ const backdrop = document.createElement("div");
+ backdrop.className = "modal-backdrop";
+ document.body.appendChild(backdrop);
+
+ const $dialog = makeDialog().appendTo(document.body);
+ await openDialog($dialog, true);
+
+ expect($dialog[0].style.zIndex).toBe("1110");
+ 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)
+
+ const $dialog = makeDialog().appendTo(document.body);
+ await openDialog($dialog, true);
+
+ expect($dialog[0].style.zIndex).toBe("");
+ });
+
+ it("never lifts a self-managed popup dialog, even while another popup is stacked", async () => {
+ document.body.classList.add("tree-popup-stacked");
+ openStackedPopup("1100");
+
+ const $popup = $("<div class='modal tree-popup-editor-dialog'></div>").appendTo(document.body);
+ await openDialog($popup, false);
+
+ expect($popup[0].style.zIndex).toBe("");
+ });
+
+ it("clears a stale inline z-index when reopened without a stacked popup", async () => {
+ const $dialog = makeDialog().appendTo(document.body);
+ $dialog[0].style.zIndex = "1110"; // leftover from a previous stacked open
+
+ await openDialog($dialog, true);
+
+ expect($dialog[0].style.zIndex).toBe("");
+ });
+ });
+
describe("closeActiveDialog", () => {
it("hides the active dialog and clears the reference", () => {
const $dialog = makeDialog();
diff --git a/apps/client/src/services/dialog.ts b/apps/client/src/services/dialog.ts
index 6888870fc9..ec117ed08a 100644
--- a/apps/client/src/services/dialog.ts
+++ b/apps/client/src/services/dialog.ts
@@ -14,8 +14,22 @@ export async function openDialog($dialog: JQuery<HTMLElement>, closeActDialog =
}
saveFocusedElement();
+
+ // Lift this dialog above a stacked quick-edit / tree popup if one is open (see raiseAboveStackedPopup).
+ const bumpedZIndex = raiseAboveStackedPopup($dialog[0]);
+
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) {
+ const backdrops = document.querySelectorAll<HTMLElement>(".modal-backdrop");
+ const ownBackdrop = backdrops[backdrops.length - 1];
+ if (ownBackdrop) {
+ ownBackdrop.style.zIndex = String(bumpedZIndex - 1);
+ }
+ }
+
$dialog.on("hidden.bs.modal", () => {
const $autocompleteEl = $(".aa-input");
if ("autocomplete" in $autocompleteEl) {
@@ -39,6 +53,38 @@ export function closeActiveDialog() {
}
}
+/** Self-managing popups (quick-edit, tree popup) set their own z-index via CSS; never lift them. */
+const SELF_MANAGED_POPUP_SELECTOR = ".popup-editor-dialog, .tree-popup-editor-dialog";
+
+/**
+ * When a quick-edit / tree popup is stacked on top of another modal it sits at z-index 1100 — above
+ * the standard dialog layer (1055). A dialog opened from within it (delete/confirm/prompt/…) would
+ * then render *behind* the popup. Detect that case and give the incoming dialog an inline z-index
+ * just above the current top-most modal so it clears the popup.
+ *
+ * Always clears any prior inline z-index first, so a dialog reused later in a non-stacked context
+ * returns to the default layer. Returns the assigned z-index (for the caller to match the backdrop),
+ * or `null` when no lift was applied.
+ */
+function raiseAboveStackedPopup(dialogEl: HTMLElement): number | null {
+ // Reset any bump left over from a previous stacked open.
+ dialogEl.style.zIndex = "";
+
+ const hasStackedPopup = document.body.classList.contains("popup-editor-stacked")
+ || document.body.classList.contains("tree-popup-stacked");
+ if (!hasStackedPopup || dialogEl.matches(SELF_MANAGED_POPUP_SELECTOR)) {
+ return null;
+ }
+
+ const others = Array.from(document.querySelectorAll<HTMLElement>(".modal.show"))
+ .filter((modal) => modal !== dialogEl);
+ const maxZIndex = others.reduce((max, modal) => Math.max(max, parseInt(getComputedStyle(modal).zIndex, 10) || 0), 0);
+
+ const zIndex = maxZIndex + 10;
+ dialogEl.style.zIndex = String(zIndex);
+ return zIndex;
+}
+
async function info(message: MessageType, extraProps?: InfoExtraProps) {
return new Promise((res) => appContext.triggerCommand("showInfoDialog", { ...extraProps, message, callback: res }));
}
diff --git a/apps/client/src/widgets/dialogs/TreePopupEditor.css b/apps/client/src/widgets/dialogs/TreePopupEditor.css
index 8aa936f252..c3aac63812 100644
--- a/apps/client/src/widgets/dialogs/TreePopupEditor.css
+++ b/apps/client/src/widgets/dialogs/TreePopupEditor.css
@@ -1,3 +1,14 @@
+/** Mirror the quick-edit popup's z-index scheme (see PopupEditor.css). Reduce the popup below the
+ standard dialog layer so dialogs (delete/confirm) and ckeditor popups opened from within it show
+ on top instead of tying with it at Bootstrap's default 1055. */
+body.tree-popup-open:not(.tree-popup-stacked) > .modal-backdrop { z-index: 998; }
+body.tree-popup-open:not(.tree-popup-stacked) .tree-popup-editor-dialog { z-index: 999; }
+
+/** When stacked on top of another modal (e.g. opened from the Options dialog's Related Settings),
+ raise the popup and its own backdrop above the underlying modal. */
+body.tree-popup-stacked .tree-popup-editor-dialog { z-index: 1100; }
+.modal-backdrop.tree-popup-backdrop { z-index: 1099; }
+
.modal.tree-popup-editor-dialog .modal-dialog {
width: 90vw;
max-width: 1100px;
diff --git a/apps/client/src/widgets/dialogs/TreePopupEditor.tsx b/apps/client/src/widgets/dialogs/TreePopupEditor.tsx
index c4fd71aeed..34dc52b3f3 100644
--- a/apps/client/src/widgets/dialogs/TreePopupEditor.tsx
+++ b/apps/client/src/widgets/dialogs/TreePopupEditor.tsx
@@ -1,6 +1,6 @@
import "./TreePopupEditor.css";
-import { useCallback, useContext, useRef, useState } from "preact/hooks";
+import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooks";
import appContext from "../../components/app_context";
import NoteContext from "../../components/note_context";
@@ -21,6 +21,7 @@ import { TitleRow } from "./PopupEditor";
*/
export default function TreePopupEditor() {
const [ shown, setShown ] = useState(false);
+ const [ stacked, setStacked ] = useState(false);
const parentComponent = useContext(ParentComponent);
const [ noteContext, setNoteContext ] = useState(() => new NoteContext("_tree-popup"));
const modalRef = useRef<HTMLDivElement>(null);
@@ -28,6 +29,7 @@ export default function TreePopupEditor() {
useTriliumEvent("openInTreePopup", async ({ noteIdOrPath, hoistedNoteId }) => {
// Fresh context per open so the sidebar tree is hoisted to the requested subtree.
const newContext = new NoteContext("_tree-popup", hoistedNoteId);
+ setStacked(!!document.querySelector(".modal.show"));
await newContext.setNote(noteIdOrPath, { keepActiveDialog: true });
// Events triggered at note-context level (e.g. the save indicator) would not work since this
@@ -38,6 +40,28 @@ export default function TreePopupEditor() {
setShown(true);
});
+ // Mirror the quick-edit popup's stacking scheme (see PopupEditor): lower the popup below the
+ // standard dialog layer so dialogs opened from within it (delete/confirm, ckeditor popups)
+ // render on top, and raise it above an underlying modal when stacked (e.g. opened from the
+ // Options dialog's Related Settings). Without this the popup sits at Bootstrap's default 1055
+ // and ties with those dialogs, hiding them behind it.
+ useEffect(() => {
+ document.body.classList.toggle("tree-popup-open", shown);
+ document.body.classList.toggle("tree-popup-stacked", shown && stacked);
+ }, [ shown, stacked ]);
+
+ // When stacked, raise this popup's own backdrop above the underlying modal. Bootstrap does not
+ // auto-increment z-index for stacked modals, and the appended `.modal-backdrop` is not
+ // individually addressable.
+ useEffect(() => {
+ if (!shown || !stacked) return;
+ const backdrops = document.querySelectorAll(".modal-backdrop");
+ const popupBackdrop = backdrops[backdrops.length - 1] as HTMLElement | undefined;
+ if (!popupBackdrop) return;
+ popupBackdrop.classList.add("tree-popup-backdrop");
+ return () => popupBackdrop.classList.remove("tree-popup-backdrop");
+ }, [ shown, stacked ]);
+
// Keep navigation that follows internal links inside the popup, rather than letting the global
// link handler open the target in the background tab. Links that stay within the hoisted subtree
// navigate the modal's own context; links pointing outside it can't be shown in the hoisted