Restore focus when returning to tab (#6276)

This commit is contained in:
Bram Kragten 2020-06-29 23:52:04 +02:00 committed by GitHub
parent 71faaf2ab1
commit 43fbf97e10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 2 deletions

View File

@ -0,0 +1,8 @@
export const deepActiveElement = (
root: DocumentOrShadowRoot = document
): Element | null => {
if (root.activeElement?.shadowRoot?.activeElement) {
return deepActiveElement(root.activeElement.shadowRoot);
}
return root.activeElement;
};

View File

@ -14,6 +14,7 @@ import {
STATE_RUNNING, STATE_RUNNING,
} from "home-assistant-js-websocket"; } from "home-assistant-js-websocket";
import { CustomPanelInfo } from "../data/panel_custom"; import { CustomPanelInfo } from "../data/panel_custom";
import { deepActiveElement } from "../common/dom/deep-active-element";
const CACHE_URL_PATHS = ["lovelace", "developer-tools"]; const CACHE_URL_PATHS = ["lovelace", "developer-tools"];
const COMPONENTS = { const COMPONENTS = {
@ -92,7 +93,9 @@ class PartialPanelResolver extends HassRouterPage {
private _waitForStart = false; private _waitForStart = false;
private _disconnectedPanel?: ChildNode; private _disconnectedPanel?: HTMLElement;
private _disconnectedActiveElement?: HTMLElement;
private _hiddenTimeout?: number; private _hiddenTimeout?: number;
@ -185,7 +188,13 @@ class PartialPanelResolver extends HassRouterPage {
!(curPanel.config as CustomPanelInfo).config._panel_custom !(curPanel.config as CustomPanelInfo).config._panel_custom
.embed_iframe) .embed_iframe)
) { ) {
this._disconnectedPanel = this.lastChild; this._disconnectedPanel = this.lastChild as HTMLElement;
const activeEl = deepActiveElement(
this._disconnectedPanel.shadowRoot || undefined
);
if (activeEl instanceof HTMLElement) {
this._disconnectedActiveElement = activeEl;
}
this.removeChild(this.lastChild); this.removeChild(this.lastChild);
} }
}, 300000); }, 300000);
@ -201,6 +210,10 @@ class PartialPanelResolver extends HassRouterPage {
this.appendChild(this._disconnectedPanel); this.appendChild(this._disconnectedPanel);
this._disconnectedPanel = undefined; this._disconnectedPanel = undefined;
} }
if (this._disconnectedActiveElement) {
this._disconnectedActiveElement.focus();
this._disconnectedActiveElement = undefined;
}
} }
private async _updateRoutes(oldPanels?: HomeAssistant["panels"]) { private async _updateRoutes(oldPanels?: HomeAssistant["panels"]) {