diff --git a/src/components/ha-label-badge.js b/src/components/ha-label-badge.ts similarity index 58% rename from src/components/ha-label-badge.js rename to src/components/ha-label-badge.ts index 0aafbd8300..8a33d8eb56 100644 --- a/src/components/ha-label-badge.js +++ b/src/components/ha-label-badge.ts @@ -1,9 +1,83 @@ -import { html } from "@polymer/polymer/lib/utils/html-tag"; -import { PolymerElement } from "@polymer/polymer/polymer-element"; +import { + LitElement, + PropertyDeclarations, + PropertyValues, +} from "@polymer/lit-element"; +import { TemplateResult, html } from "lit-html"; +import { classMap } from "lit-html/directives/classMap"; import "./ha-icon"; -class HaLabelBadge extends PolymerElement { - static get template() { +class HaLabelBadge extends LitElement { + public value?: string; + public icon?: string; + public label?: string; + public description?: string; + public image?: string; + + static get properties(): PropertyDeclarations { + return { + value: {}, + icon: {}, + label: {}, + description: {}, + image: {}, + }; + } + + protected render(): TemplateResult { + return html` + ${this.renderStyle()} +
+
+
+ ${ + this.icon && !this.value && !this.image + ? html` + + ` + : "" + } + ${ + this.value && !this.image + ? html` + ${this.value} + ` + : "" + } +
+ ${ + this.label + ? html` +
+ ${this.label} +
+ ` + : "" + } +
+ ${ + this.description + ? html` +
${this.description}
+ ` + : "" + } +
+ `; + } + + protected renderStyle(): TemplateResult { return html` - -
-
-
- - [[value]] -
-
- [[label]] -
-
-
[[description]]
-
`; } - static get properties() { - return { - value: String, - icon: String, - label: String, - description: String, - - image: { - type: String, - observer: "imageChanged", - }, - }; - } - - computeValueClasses(value) { - return value && value.length > 4 ? "value big" : "value"; - } - - computeLabelClasses(label) { - return label && label.length > 5 ? "label big" : "label"; - } - - computeHideLabel(label) { - return !label || !label.trim(); - } - - computeHideIcon(icon, value, image) { - return !icon || value || image; - } - - computeHideValue(value, image) { - return !value || image; - } - - imageChanged(newVal) { - this.$.badge.style.backgroundImage = newVal ? "url(" + newVal + ")" : ""; + protected updated(changedProperties: PropertyValues): void { + if (changedProperties.has("image")) { + this.shadowRoot!.getElementById("badge")!.style.backgroundImage = this + .image + ? `url(${this.image})` + : ""; + } } } + +declare global { + interface HTMLElementTagNameMap { + "ha-label-badge": HaLabelBadge; + } +} + customElements.define("ha-label-badge", HaLabelBadge);