Skip to content

Commit

Permalink
Don't show badge container if all badges are hidden (#21449)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored Jul 22, 2024
1 parent d96ddf9 commit dd22ae4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/panels/lovelace/badges/hui-badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type { LovelaceBadge } from "../types";

declare global {
interface HASSDomEvents {
"badge-visibility-changed": { value: boolean };
"badge-updated": undefined;
}
}
Expand Down Expand Up @@ -183,6 +184,7 @@ export class HuiBadge extends ReactiveElement {
if (this.hidden !== !visible) {
this.style.setProperty("display", visible ? "" : "none");
this.toggleAttribute("hidden", !visible);
fireEvent(this, "badge-visibility-changed", { value: visible });
}

if (!visible && this._element.parentElement) {
Expand Down
45 changes: 44 additions & 1 deletion src/panels/lovelace/badges/hui-view-badges.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { mdiPlus } from "@mdi/js";
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
import {
css,
CSSResultGroup,
html,
LitElement,
nothing,
PropertyValues,
} from "lit";
import { customElement, property, state } from "lit/decorators";
import { repeat } from "lit/directives/repeat";
import { fireEvent } from "../../../common/dom/fire_event";
Expand Down Expand Up @@ -33,6 +40,38 @@ export class HuiViewBadges extends LitElement {

private _badgeConfigKeys = new WeakMap<HuiBadge, string>();

private _checkAllHidden() {
const allHidden =
!this.lovelace.editMode && this.badges.every((section) => section.hidden);
this.toggleAttribute("hidden", allHidden);
}

private _badgeVisibilityChanged = () => {
this._checkAllHidden();
};

connectedCallback(): void {
super.connectedCallback();
this.addEventListener(
"badge-visibility-changed",
this._badgeVisibilityChanged
);
}

disconnectedCallback(): void {
super.disconnectedCallback();
this.removeEventListener(
"badge-visibility-changed",
this._badgeVisibilityChanged
);
}

willUpdate(changedProperties: PropertyValues<typeof this>): void {
if (changedProperties.has("badges") || changedProperties.has("lovelace")) {
this._checkAllHidden();
}
}

private _getBadgeKey(badge: HuiBadge) {
if (!this._badgeConfigKeys.has(badge)) {
this._badgeConfigKeys.set(badge, Math.random().toString());
Expand Down Expand Up @@ -130,6 +169,10 @@ export class HuiViewBadges extends LitElement {

static get styles(): CSSResultGroup {
return css`
:host([hidden]) {
display: none !important;
}
.badges {
display: flex;
align-items: flex-start;
Expand Down
19 changes: 16 additions & 3 deletions src/panels/lovelace/views/hui-sections-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,24 @@ export class SectionsView extends LitElement implements LovelaceViewElement {
).length;
}

private _sectionVisibilityChanged = () => {
this._computeSectionsCount();
};

connectedCallback(): void {
super.connectedCallback();
this.addEventListener("section-visibility-changed", () => {
this._computeSectionsCount();
});
this.addEventListener(
"section-visibility-changed",
this._sectionVisibilityChanged
);
}

disconnectedCallback(): void {
super.disconnectedCallback();
this.removeEventListener(
"section-visibility-changed",
this._sectionVisibilityChanged
);
}

willUpdate(changedProperties: PropertyValues<typeof this>): void {
Expand Down

0 comments on commit dd22ae4

Please sign in to comment.