mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 19:26:36 +00:00
Use event for section visibility instead of mutation observer (#20967)
This commit is contained in:
parent
c08d9a9166
commit
0add65feff
@ -26,6 +26,13 @@ import { parseLovelaceCardPath } from "../editor/lovelace-path";
|
||||
import { generateLovelaceSectionStrategy } from "../strategies/get-strategy";
|
||||
import type { Lovelace } from "../types";
|
||||
import { DEFAULT_SECTION_LAYOUT } from "./const";
|
||||
import { fireEvent } from "../../../common/dom/fire_event";
|
||||
|
||||
declare global {
|
||||
interface HASSDomEvents {
|
||||
"section-visibility-changed": { value: boolean };
|
||||
}
|
||||
}
|
||||
|
||||
@customElement("hui-section")
|
||||
export class HuiSection extends ReactiveElement {
|
||||
@ -219,8 +226,12 @@ export class HuiSection extends ReactiveElement {
|
||||
!this.config.visibility ||
|
||||
checkConditionsMet(this.config.visibility, this.hass);
|
||||
|
||||
this.style.setProperty("display", visible ? "" : "none");
|
||||
this.toggleAttribute("hidden", !visible);
|
||||
if (this.hidden !== !visible) {
|
||||
this.style.setProperty("display", visible ? "" : "none");
|
||||
this.toggleAttribute("hidden", !visible);
|
||||
fireEvent(this, "section-visibility-changed", { value: visible });
|
||||
}
|
||||
|
||||
if (!visible && this._layoutElement.parentElement) {
|
||||
this.removeChild(this._layoutElement);
|
||||
} else if (visible && !this._layoutElement.parentElement) {
|
||||
|
@ -54,29 +54,22 @@ export class SectionsView extends LitElement implements LovelaceViewElement {
|
||||
return this._sectionConfigKeys.get(sectionConfig)!;
|
||||
}
|
||||
|
||||
private _sectionObserver?: MutationObserver;
|
||||
|
||||
private _computeSectionsCount() {
|
||||
this._sectionCount = this.sections.filter(
|
||||
(section) => !section.hidden
|
||||
).length;
|
||||
}
|
||||
|
||||
connectedCallback(): void {
|
||||
super.connectedCallback();
|
||||
this.addEventListener("section-visibility-changed", () => {
|
||||
this._computeSectionsCount();
|
||||
});
|
||||
}
|
||||
|
||||
willUpdate(changedProperties: PropertyValues<typeof this>): void {
|
||||
if (!this._sectionObserver) {
|
||||
this._sectionObserver = new MutationObserver(() => {
|
||||
this._computeSectionsCount();
|
||||
});
|
||||
}
|
||||
if (changedProperties.has("sections")) {
|
||||
this._computeSectionsCount();
|
||||
this._sectionObserver.disconnect();
|
||||
this.sections.forEach((section) => {
|
||||
this._sectionObserver!.observe(section, {
|
||||
attributes: true,
|
||||
attributeFilter: ["hidden"],
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user