Compare commits

...
Author SHA1 Message Date
WendelinandClaude Opus 5.5 5a3486e945 Keep the covered dialog level with more dialogs over it
Back over two dialogs, showing the same dialog again, a dialog that
refuses Back, and a follow-up dialog opened before the previous one
finished closing lost the covered level or the history position.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-12-04 15:52:42 +01:00
WendelinandClaude Opus 5.5 c2ebec32d9 Mock isDialogHistoryEntry in the prevent unsaved mixin test
navigate() now asks the dialog manager whether the current entry belongs
to an open dialog.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-12-04 15:45:26 +01:00
WendelinandClaude Opus 5.5 9b421dc0a8 Keep dialog history levels under other dialogs and navigation
A dialog that opens over a level of a DialogHistoryMixin dialog now
replaces that entry like it replaces a dialog's own one, and gives the
level back when it closes or Back leaves it, also from the first level,
where Back lands on the lower dialog's own entry. navigate() waits for
such a dialog to go back past its levels, and the mixin keeps its depth
itself, so it still unwinds when a dialog over it took over the current
entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-12-04 15:45:26 +01:00
8 changed files with 228 additions and 22 deletions

No files matched your search

+5 -3
View File
@@ -1,6 +1,7 @@
import {
closeAllDialogs,
DIALOG_WAIT_TIMEOUT,
isDialogHistoryEntry,
updateClosedDialogSearch,
} from "../dialogs/make-dialog-manager";
import { fireEvent } from "./dom/fire_event";
@@ -69,9 +70,10 @@ export const setRefreshUrl = (path: string) => {
* Returns true if navigation can proceed, false if a dialog refused to close.
*/
const ensureDialogsClosed = async (timestamp: number): Promise<boolean> => {
const { history } = mainWindow;
if (!history.state?.dialog || Date.now() - timestamp >= DIALOG_WAIT_TIMEOUT) {
if (
!isDialogHistoryEntry() ||
Date.now() - timestamp >= DIALOG_WAIT_TIMEOUT
) {
return true;
}
+26 -10
View File
@@ -40,6 +40,10 @@ export const DialogHistoryMixin = <
private _hasManagerHistoryEntry = false;
// Depth of the current level, also while a dialog opened over this one
// has replaced its history entry
private _historyDepth = 0;
private _historyPending = false;
private _closedWhileHistoryPending = false;
@@ -62,12 +66,14 @@ export const DialogHistoryMixin = <
}
// Navigating back in history goes up a level instead of closing
if (
!this._historyClosing &&
historyState &&
this.restoreHistoryLevel(this._ownHistoryData(historyState))
) {
return false;
if (!this._historyClosing && historyState) {
const level = this._ownHistoryData(historyState);
if (this.restoreHistoryLevel(level)) {
if (level) {
this._historyDepth = level.depth;
}
return false;
}
}
if (!this._historyClosing) {
@@ -93,17 +99,21 @@ export const DialogHistoryMixin = <
}
protected pushHistoryLevel(level: L) {
const current = this._ownHistoryData(mainWindow.history.state);
if (!current) {
// Code that runs on after closing may still pick a level
if (this._historyClosing || !this.isConnected) {
return;
}
if (!this._historyDepth) {
this._hasManagerHistoryEntry =
mainWindow.history.state?.dialog === this.localName;
}
this._historyDepth++;
mainWindow.history.pushState(
{
dialogData: {
...level,
dialogId: this._historyId,
depth: (current?.depth ?? 0) + 1,
depth: this._historyDepth,
},
},
""
@@ -124,7 +134,13 @@ export const DialogHistoryMixin = <
// Goes back to the opener entry, where the dialog manager closes this
// dialog as the top one, so a dialog below it stays open.
private _unwindHistory(): boolean {
const depth = this._ownHistoryData(mainWindow.history.state)?.depth;
const { state } = mainWindow.history;
const depth =
this._ownHistoryData(state)?.depth ??
// A dialog opened over this one replaced the current level's entry
(state?.dialog && state.dialog !== this.localName
? this._historyDepth
: undefined);
if (!depth) {
return false;
}
+69 -9
View File
@@ -53,6 +53,9 @@ export interface DialogState {
dialogParams: unknown;
dialogImport?: () => Promise<unknown>;
addHistory?: boolean;
// The history entry of a level of the dialog below (see
// DialogHistoryMixin), which the entry of this dialog replaced
replacedLevel?: unknown;
}
interface LoadedDialogInfo {
@@ -90,6 +93,41 @@ const _removeOpenDialogTag = (dialogTag: string) => {
}
};
// An entry a dialog pushed for one of its levels (see DialogHistoryMixin),
// while that dialog is still open
const isLevelOfOpenDialog = (historyState: any): boolean =>
Boolean(historyState?.dialogData && OPEN_DIALOG_STACK.length);
/**
* Whether Back went from a dialog to the entry of a dialog below it, which
* only has its own entry under its first level (see DialogHistoryMixin).
*/
export const isEntryOfDialogBelow = (historyState: any): boolean =>
OPEN_DIALOG_STACK.slice(0, -1).some(
(dialog) => dialog.dialogTag === historyState?.dialog
);
// Whether Back went from a dialog over a level to the dialog below, not to
// a level of its own
const wentToDialogBelow = (dialog: DialogState, historyState: any) => {
const level = dialog.replacedLevel as
{ dialogData: { dialogId: string } } | undefined;
return Boolean(
level &&
(historyState?.dialog ||
historyState?.dialogData?.dialogId === level.dialogData.dialogId)
);
};
/**
* Whether the current history entry belongs to an open dialog, so leaving
* the page must wait until the dialogs closed and went back past it.
*/
export const isDialogHistoryEntry = (): boolean => {
const { state } = mainWindow.history;
return Boolean(state?.dialog) || isLevelOfOpenDialog(state);
};
/**
* Returns the tag name of the topmost open dialog, or `undefined` when no
* dialog is open.
@@ -191,18 +229,23 @@ export const showDialog = async (
const dialogIndex = OPEN_DIALOG_STACK.findIndex(
(state) => state.dialogTag === dialogTag
);
if (dialogIndex !== -1) {
OPEN_DIALOG_STACK.splice(dialogIndex, 1);
}
const shownState =
dialogIndex !== -1
? OPEN_DIALOG_STACK.splice(dialogIndex, 1)[0]
: undefined;
const replacedLevel = isLevelOfOpenDialog(history.state)
? history.state
: shownState?.replacedLevel;
OPEN_DIALOG_STACK.push({
element,
dialogTag,
dialogParams,
dialogImport,
addHistory,
replacedLevel,
});
const newState = { dialog: dialogTag };
if (history.state?.dialog) {
if (history.state?.dialog || replacedLevel) {
// if a dialog is already open, replace the name
history.replaceState(newState, "");
} else {
@@ -281,9 +324,17 @@ export const closeLastDialog = async (historyState?: any) => {
if (!closed) {
// if the dialog was not closed, put it back on the stack
OPEN_DIALOG_STACK.push(lastDialog);
if (wentToDialogBelow(lastDialog, historyState)) {
mainWindow.history.pushState({ dialog: lastDialog.dialogTag }, "");
}
} else if (lastDialog.replacedLevel && historyState) {
// Back went past the level the dialog below is still on
mainWindow.history.pushState(lastDialog.replacedLevel, "");
} else if (
OPEN_DIALOG_STACK.length &&
mainWindow.history.state?.opensDialog
(mainWindow.history.state?.opensDialog ||
// Back went past the level a dialog below covers
OPEN_DIALOG_STACK.some((dialog) => dialog.replacedLevel))
) {
// if there are more dialogs open, push a new state so back() will close the next top dialog
mainWindow.history.pushState(
@@ -360,14 +411,17 @@ const _handleClosed = (ev: HASSDomEvent<DialogClosedParams>) => {
const dialogIndex = OPEN_DIALOG_STACK.findIndex(
(state) => state.dialogTag === ev.detail.dialog
);
if (dialogIndex !== -1) {
OPEN_DIALOG_STACK.splice(dialogIndex, 1);
}
const closedState =
dialogIndex !== -1
? OPEN_DIALOG_STACK.splice(dialogIndex, 1)[0]
: undefined;
if (mainWindow.history.state?.dialog === ev.detail.dialog) {
if (OPEN_DIALOG_STACK.length) {
// if there are more dialogs open, set the top one in history
mainWindow.history.replaceState(
{ dialog: OPEN_DIALOG_STACK[OPEN_DIALOG_STACK.length - 1].dialogTag },
closedState?.replacedLevel ?? {
dialog: OPEN_DIALOG_STACK[OPEN_DIALOG_STACK.length - 1].dialogTag,
},
""
);
} else if (dialogIndex !== -1) {
@@ -376,6 +430,12 @@ const _handleClosed = (ev: HASSDomEvent<DialogClosedParams>) => {
closedDialogUrl = { pathname, search };
mainWindow.history.back();
}
} else if (
closedState?.replacedLevel &&
dialogIndex < OPEN_DIALOG_STACK.length
) {
// The dialog above took over the entry that covers the level
OPEN_DIALOG_STACK[dialogIndex].replacedLevel ??= closedState.replacedLevel;
}
const loaded = ev.detail.dialog ? LOADED[ev.detail.dialog] : undefined;
+3
View File
@@ -5,6 +5,7 @@ import { mainWindow } from "../common/dom/get_main_window";
import { updateHistoryState } from "../common/navigate";
import {
closeLastDialog,
isEntryOfDialogBelow,
restoreClosedDialogSearch,
} from "../dialogs/make-dialog-manager";
import type { ProvideHassElement } from "../mixins/provide-hass-lit-mixin";
@@ -78,6 +79,8 @@ export const urlSyncMixin = <
// if we have dialog data we are closing a dialog with appended state
// so dialog has the change to navigate back to the previous state
closeLastDialog(ev.state);
} else if (isEntryOfDialogBelow(ev.state)) {
closeLastDialog(ev.state);
} else if ("dialog" in ev.state) {
// coming to a dialog
// the dialog stack must be empty in this case so this state should be cleaned up
+1
View File
@@ -22,6 +22,7 @@ vi.mock("../../src/dialogs/make-dialog-manager", () => ({
window.history.replaceState(null, "");
return true;
}),
isDialogHistoryEntry: () => Boolean(window.history.state?.dialog),
}));
// Fabricates a raw entry like a document load, which navigate() never produces.
@@ -84,6 +84,8 @@ describe("DialogHistoryMixin", () => {
expect(dialog.level).toBe(1);
expect(dialog.isConnected).toBe(true);
// A dialog opened over this one takes over the entry of the level
window.history.replaceState({ dialog: "test-other-dialog" }, "");
const closed = dialog.closeDialog();
expect(dialog.isConnected).toBe(true);
expect(await closed).toBe(true);
+121
View File
@@ -4,8 +4,11 @@ import { deepActiveElement } from "../../src/common/dom/deep-active-element";
import { fireEvent } from "../../src/common/dom/fire_event";
import { nextRender } from "../../src/common/util/render-status";
import {
closeLastDialog,
DIALOG_WAIT_TIMEOUT,
getTopOpenDialogTag,
isDialogHistoryEntry,
isEntryOfDialogBelow,
showDialog,
} from "../../src/dialogs/make-dialog-manager";
import type { ProvideHassElement } from "../../src/mixins/provide-hass-lit-mixin";
@@ -46,11 +49,22 @@ class TestFlowDialog extends TestNextDialog {}
class TestHistoryDialog extends TestNextDialog {}
class TestTopDialog extends TestNextDialog {}
// Like a confirmation, which only closes from its buttons
class TestRefusingDialog extends TestNextDialog {
public closeDialog() {
return false;
}
}
customElements.define("test-manager-dialog", TestDialog);
customElements.define("test-manager-nested-dialog", TestNestedDialog);
customElements.define("test-manager-next-dialog", TestNextDialog);
customElements.define("test-manager-flow-dialog", TestFlowDialog);
customElements.define("test-manager-history-dialog", TestHistoryDialog);
customElements.define("test-manager-top-dialog", TestTopDialog);
customElements.define("test-manager-refusing-dialog", TestRefusingDialog);
declare global {
interface HTMLElementTagNameMap {
@@ -59,6 +73,8 @@ declare global {
"test-manager-next-dialog": TestNextDialog;
"test-manager-flow-dialog": TestFlowDialog;
"test-manager-history-dialog": TestHistoryDialog;
"test-manager-top-dialog": TestTopDialog;
"test-manager-refusing-dialog": TestRefusingDialog;
}
}
@@ -229,3 +245,108 @@ describe("dialog manager history wait", () => {
}
});
});
// A dialog opened over a level of another dialog (see DialogHistoryMixin)
// must keep the number of history entries and give the level back, or Back
// and closing the lower dialog land on the wrong entry.
describe("dialog manager over a dialog level", () => {
it("works", async () => {
const element = host();
const showWithHistory = async (dialogTag: string) => {
await showDialog(element, dialogTag, {}, () => Promise.resolve());
return element.shadowRoot!.querySelector(dialogTag) as TestNextDialog;
};
const popstate = () =>
new Promise((resolve) => {
window.addEventListener("popstate", resolve, { once: true });
});
const level = (depth: number) => ({
dialogData: { dialogId: "lower", depth },
});
try {
window.history.replaceState({ opener: true }, "");
const lower = await showWithHistory("test-manager-next-dialog");
window.history.pushState(level(1), "");
window.history.pushState(level(2), "");
const { length } = window.history;
expect(isDialogHistoryEntry()).toBe(true);
let upper = await showWithHistory("test-manager-flow-dialog");
expect(window.history.state).toEqual({
dialog: "test-manager-flow-dialog",
});
expect(window.history.length).toBe(length);
upper.close();
expect(window.history.state).toEqual(level(2));
upper = await showWithHistory("test-manager-flow-dialog");
window.history.back();
await popstate();
// Like the url-sync mixin
await closeLastDialog(window.history.state);
expect(window.history.state).toEqual(level(2));
upper.close();
// Three dialogs over the level, Back closes one at a time
upper = await showWithHistory("test-manager-flow-dialog");
let top = await showWithHistory("test-manager-top-dialog");
const third = await showWithHistory("test-manager-history-dialog");
window.history.back();
await popstate();
await closeLastDialog(window.history.state);
third.close();
expect(window.history.state).toEqual({
dialog: "test-manager-top-dialog",
});
window.history.back();
await popstate();
await closeLastDialog(window.history.state);
top.close();
expect(window.history.state).toEqual({
dialog: "test-manager-flow-dialog",
});
window.history.back();
await popstate();
await closeLastDialog(window.history.state);
upper.close();
expect(window.history.state).toEqual(level(2));
// Shown again, then the next dialog takes over the entry
upper = await showWithHistory("test-manager-flow-dialog");
upper = await showWithHistory("test-manager-flow-dialog");
top = await showWithHistory("test-manager-top-dialog");
upper.close();
top.close();
expect(window.history.state).toEqual(level(2));
// Refusing Back keeps the dialog's entry
const refusing = await showWithHistory("test-manager-refusing-dialog");
window.history.back();
await popstate();
expect(await closeLastDialog(window.history.state)).toBe(false);
expect(window.history.state).toEqual({
dialog: "test-manager-refusing-dialog",
});
refusing.close();
expect(window.history.state).toEqual(level(2));
expect(window.history.length).toBe(length);
// Over the first level, Back lands on the lower dialog's own entry
window.history.back();
await popstate();
upper = await showWithHistory("test-manager-flow-dialog");
window.history.back();
await popstate();
expect(isEntryOfDialogBelow(window.history.state)).toBe(true);
await closeLastDialog(window.history.state);
expect(window.history.state).toEqual(level(1));
upper.close();
lower.close();
// Left behind, like after a page reload
expect(isDialogHistoryEntry()).toBe(false);
} finally {
window.history.replaceState(null, "");
}
});
});
@@ -7,6 +7,7 @@ import { PreventUnsavedMixin } from "../../src/mixins/prevent-unsaved-mixin";
// navigate() closes open dialogs before touching history.
vi.mock("../../src/dialogs/make-dialog-manager", () => ({
closeAllDialogs: vi.fn(async () => true),
isDialogHistoryEntry: () => Boolean(window.history.state?.dialog),
}));
interface TestState {