Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

UI bugfix where dialog is not closed on window navigation (#1475)

Martin Peetersa15bc00c8191
diff --git a/ui/component/or-mwc-components/src/or-mwc-dialog.ts b/ui/component/or-mwc-components/src/or-mwc-dialog.ts
index b1ebf463e..4bc99f7b7 100644
--- a/ui/component/or-mwc-components/src/or-mwc-dialog.ts
+++ b/ui/component/or-mwc-components/src/or-mwc-dialog.ts
@@ -222,6 +222,9 @@ export class OrMwcDialog extends LitElement {
protected _mdcComponent?: MDCDialog;
+ // Bind for the 'popstate' event that tracks changes in the browser history.
+ protected _popstateEventBind = (ev: PopStateEvent) => this._onBrowserNavigate(ev);
+
public get isOpen() {
return this._mdcComponent ? this._mdcComponent.isOpen : false;
}
@@ -277,8 +280,14 @@ export class OrMwcDialog extends LitElement {
}
}
+ connectedCallback() {
+ super.connectedCallback();
+ window.addEventListener("popstate", this._popstateEventBind);
+ }
+
disconnectedCallback(): void {
super.disconnectedCallback();
+ window.removeEventListener("popstate", this._popstateEventBind);
if (this._mdcComponent) {
this._mdcComponent.destroy();
this._mdcComponent = undefined;
@@ -354,4 +363,12 @@ export class OrMwcDialog extends LitElement {
}
this.dispatchEvent(new OrMwcDialogClosedEvent(action));
}
+
+ /**
+ * Function callback for when the browser window navigates to another place.
+ * We attempt to close the dialog when this occurs.
+ */
+ protected _onBrowserNavigate(ev: PopStateEvent) {
+ this.close("close");
+ }
}