Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): keep a dialog's own z-index when opening it, closes #10586

Elian Doran95c837b7ad72
diff --git a/apps/client/src/services/dialog.spec.ts b/apps/client/src/services/dialog.spec.ts
index 80237f3085..9f8cb332c1 100644
--- a/apps/client/src/services/dialog.spec.ts
+++ b/apps/client/src/services/dialog.spec.ts
@@ -233,14 +233,60 @@ describe("dialog service", () => {
expect($popup[0].style.zIndex).toBe("");
});
- it("clears a stale inline z-index when reopened without a stacked popup", async () => {
+ it("reverts its own lift when the dialog is reopened without a stacked popup", async () => {
const $dialog = makeDialog().appendTo(document.body);
- $dialog[0].style.zIndex = "1110"; // leftover from a previous stacked open
+ document.body.classList.add("tree-popup-stacked");
+ openStackedPopup("1100");
+ await openDialog($dialog, true);
+ expect($dialog[0].style.zIndex).toBe("1110");
+
+ // Reopened once the popup is gone: back to the default layer.
+ document.body.className = "";
+ document.body.innerHTML = "";
+ $dialog.appendTo(document.body);
await openDialog($dialog, true);
expect($dialog[0].style.zIndex).toBe("");
});
+
+ it("restores a dialog's own declared z-index instead of clearing it", async () => {
+ // `Modal` passes its `zIndex` prop through (note type chooser: 1100, confirm/prompt:
+ // 2000). Opening the dialog must not strip that layer.
+ const $dialog = makeDialog().appendTo(document.body);
+
+ await openDialog($dialog, true, undefined, 1100); // no stacked popup
+
+ expect($dialog[0].style.zIndex).toBe("1100");
+ });
+
+ it("keeps a declared z-index that already clears the stacked popup", async () => {
+ document.body.classList.add("tree-popup-stacked");
+ openStackedPopup("1100");
+
+ const $dialog = makeDialog().appendTo(document.body);
+
+ await openDialog($dialog, true, undefined, 2000); // e.g. the confirm/prompt layer
+
+ // Lifting only ever raises: 2000 already beats max(1100) + 10.
+ expect($dialog[0].style.zIndex).toBe("2000");
+ });
+
+ it("lifts a declared z-index that is below the stacked popup, then restores it", async () => {
+ const $dialog = makeDialog().appendTo(document.body);
+
+ document.body.classList.add("tree-popup-stacked");
+ openStackedPopup("1200");
+ await openDialog($dialog, true, undefined, 1100);
+ expect($dialog[0].style.zIndex).toBe("1210");
+
+ document.body.className = "";
+ document.body.innerHTML = "";
+ $dialog.appendTo(document.body);
+ await openDialog($dialog, true, undefined, 1100);
+
+ expect($dialog[0].style.zIndex).toBe("1100");
+ });
});
describe("closeActiveDialog", () => {
diff --git a/apps/client/src/services/dialog.ts b/apps/client/src/services/dialog.ts
index 8b97a9c08a..7f85b74027 100644
--- a/apps/client/src/services/dialog.ts
+++ b/apps/client/src/services/dialog.ts
@@ -7,7 +7,10 @@ import type { PromptDialogOptions } from "../widgets/dialogs/prompt.js";
import { focusSavedElement, saveFocusedElement } from "./focus.js";
import keyboardActionsService from "./keyboard_actions.js";
-export async function openDialog($dialog: JQuery<HTMLElement>, closeActDialog = true, config?: Partial<Modal.Options>) {
+/**
+ * @param declaredZIndex the z-index the dialog defines for itself, if any (see {@link raiseAboveStackedPopup}).
+ */
+export async function openDialog($dialog: JQuery<HTMLElement>, closeActDialog = true, config?: Partial<Modal.Options>, declaredZIndex?: number) {
if (closeActDialog) {
closeActiveDialog();
glob.activeDialog = $dialog;
@@ -16,7 +19,7 @@ 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]);
+ const bumpedZIndex = raiseAboveStackedPopup($dialog[0], declaredZIndex);
Modal.getOrCreateInstance($dialog[0], config).show();
@@ -66,17 +69,19 @@ const SELF_MANAGED_POPUP_SELECTOR = ".popup-editor-dialog, .tree-popup-editor-di
* 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.
+ * The dialog's inline z-index is rewritten from scratch on every open, so a lift never outlives the
+ * stacked context that warranted it. `declaredZIndex` is the layer the dialog defines for itself
+ * (`Modal`'s `zIndex` prop, e.g. 1100 for the note type chooser or 2000 for confirm/prompt) and is
+ * what it falls back to — clearing the property outright would strip that.
+ *
+ * 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 = "";
-
+function raiseAboveStackedPopup(dialogEl: HTMLElement, declaredZIndex?: number): number | null {
const hasStackedPopup = document.body.classList.contains("popup-editor-stacked")
|| document.body.classList.contains("tree-popup-stacked");
if (!hasStackedPopup || dialogEl.matches(SELF_MANAGED_POPUP_SELECTOR)) {
+ dialogEl.style.zIndex = declaredZIndex ? String(declaredZIndex) : "";
return null;
}
@@ -84,7 +89,8 @@ function raiseAboveStackedPopup(dialogEl: HTMLElement): number | null {
.filter((modal) => modal !== dialogEl);
const maxZIndex = others.reduce((max, modal) => Math.max(max, parseInt(getComputedStyle(modal).zIndex, 10) || 0), 0);
- const zIndex = maxZIndex + 10;
+ // A dialog that already declares a layer above the popup keeps it; lifting only ever raises.
+ const zIndex = Math.max(maxZIndex + 10, declaredZIndex ?? 0);
dialogEl.style.zIndex = String(zIndex);
return zIndex;
}
diff --git a/apps/client/src/widgets/react/Modal.tsx b/apps/client/src/widgets/react/Modal.tsx
index 651b0e9efa..2a995ba651 100644
--- a/apps/client/src/widgets/react/Modal.tsx
+++ b/apps/client/src/widgets/react/Modal.tsx
@@ -132,13 +132,13 @@ export default function Modal({ children, className, size, title, customTitleBar
elementToFocus.current = document.activeElement;
openDialog($(modalRef.current), !stackable, {
focus: !noFocus
- }).then(($widget) => {
+ }, zIndex).then(($widget) => {
modalInstanceRef.current = BootstrapModal.getOrCreateInstance($widget[0]);
});
} else {
modalInstanceRef.current?.hide();
}
- }, [ show, modalRef.current, noFocus ]);
+ }, [ show, modalRef.current, noFocus, zIndex ]);
// Memoize styles to prevent recreation on every render
const dialogStyle = useMemo<CSSProperties>(() => {