Skip to content

Commit

Permalink
Allow different types of heading badges (#22109)
Browse files Browse the repository at this point in the history
* Allow different type of heading item

* Update editor

* Migrate entities to items

* Rename support for string entity

* Refactor

* Rename to badges and add error state

* Update font weight

* Feedback

* Feedback
  • Loading branch information
piitaya authored Sep 27, 2024
1 parent 468660d commit a92dab4
Show file tree
Hide file tree
Showing 19 changed files with 824 additions and 394 deletions.
58 changes: 58 additions & 0 deletions src/components/ha-heading-badge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { css, CSSResultGroup, html, LitElement } from "lit";
import { customElement, property } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";

type HeadingBadgeType = "text" | "button";

@customElement("ha-heading-badge")
export class HaBadge extends LitElement {
@property() public type: HeadingBadgeType = "text";

protected render() {
return html`
<div
class="heading-badge"
role=${ifDefined(this.type === "button" ? "button" : undefined)}
tabindex=${ifDefined(this.type === "button" ? "0" : undefined)}
>
<slot name="icon"></slot>
<slot></slot>
</div>
`;
}

static get styles(): CSSResultGroup {
return css`
:host {
color: var(--secondary-text-color);
}
[role="button"] {
cursor: pointer;
}
.heading-badge {
display: flex;
flex-direction: row;
white-space: nowrap;
align-items: center;
gap: 3px;
font-family: Roboto;
font-size: 14px;
font-style: normal;
font-weight: 400;
line-height: 20px;
letter-spacing: 0.1px;
--mdc-icon-size: 14px;
}
::slotted([slot="icon"]) {
--ha-icon-display: block;
color: var(--icon-color, inherit);
}
`;
}
}

declare global {
interface HTMLElementTagNameMap {
"ha-heading-badge": HaBadge;
}
}
248 changes: 0 additions & 248 deletions src/panels/lovelace/cards/heading/hui-heading-entity.ts

This file was deleted.

31 changes: 22 additions & 9 deletions src/panels/lovelace/cards/hui-heading-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,25 @@ import { HomeAssistant } from "../../../types";
import { actionHandler } from "../common/directives/action-handler-directive";
import { handleAction } from "../common/handle-action";
import { hasAction } from "../common/has-action";
import "../heading-badges/hui-heading-badge";
import type {
LovelaceCard,
LovelaceCardEditor,
LovelaceLayoutOptions,
} from "../types";
import "./heading/hui-heading-entity";
import type { HeadingCardConfig } from "./types";

export const migrateHeadingCardConfig = (
config: HeadingCardConfig
): HeadingCardConfig => {
const newConfig = { ...config };
if (newConfig.entities) {
newConfig.badges = [...(newConfig.badges || []), ...newConfig.entities];
delete newConfig.entities;
}
return newConfig;
};

@customElement("hui-heading-card")
export class HuiHeadingCard extends LitElement implements LovelaceCard {
public static async getConfigElement(): Promise<LovelaceCardEditor> {
Expand All @@ -45,7 +56,7 @@ export class HuiHeadingCard extends LitElement implements LovelaceCard {
tap_action: {
action: "none",
},
...config,
...migrateHeadingCardConfig(config),
};
}

Expand Down Expand Up @@ -73,6 +84,8 @@ export class HuiHeadingCard extends LitElement implements LovelaceCard {

const style = this._config.heading_style || "title";

const badges = this._config.badges;

return html`
<ha-card>
<div class="container">
Expand All @@ -91,17 +104,17 @@ export class HuiHeadingCard extends LitElement implements LovelaceCard {
: nothing}
${actionable ? html`<ha-icon-next></ha-icon-next>` : nothing}
</div>
${this._config.entities?.length
${badges?.length
? html`
<div class="entities">
${this._config.entities.map(
<div class="badges">
${badges.map(
(config) => html`
<hui-heading-entity
<hui-heading-badge
.config=${config}
.hass=${this.hass}
.preview=${this.preview}
>
</hui-heading-entity>
</hui-heading-badge>
`
)}
</div>
Expand Down Expand Up @@ -150,7 +163,7 @@ export class HuiHeadingCard extends LitElement implements LovelaceCard {
.container .content:not(:has(p)) {
min-width: fit-content;
}
.container .entities {
.container .badges {
flex: 0 0;
}
.content {
Expand Down Expand Up @@ -186,7 +199,7 @@ export class HuiHeadingCard extends LitElement implements LovelaceCard {
font-weight: 500;
line-height: 20px;
}
.entities {
.badges {
display: flex;
flex-direction: row;
align-items: center;
Expand Down
Loading

0 comments on commit a92dab4

Please sign in to comment.