mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-28 11:46:42 +00:00
Convert ha-label-badge to lit (#2266)
* Convert ha-label-badge to lit * Add class to TypeScript map
This commit is contained in:
parent
88f0ebf75d
commit
80dd15306e
@ -1,9 +1,83 @@
|
|||||||
import { html } from "@polymer/polymer/lib/utils/html-tag";
|
import {
|
||||||
import { PolymerElement } from "@polymer/polymer/polymer-element";
|
LitElement,
|
||||||
|
PropertyDeclarations,
|
||||||
|
PropertyValues,
|
||||||
|
} from "@polymer/lit-element";
|
||||||
|
import { TemplateResult, html } from "lit-html";
|
||||||
|
import { classMap } from "lit-html/directives/classMap";
|
||||||
import "./ha-icon";
|
import "./ha-icon";
|
||||||
|
|
||||||
class HaLabelBadge extends PolymerElement {
|
class HaLabelBadge extends LitElement {
|
||||||
static get template() {
|
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()}
|
||||||
|
<div class="badge-container">
|
||||||
|
<div class="label-badge" id="badge">
|
||||||
|
<div
|
||||||
|
class="${
|
||||||
|
classMap({
|
||||||
|
value: true,
|
||||||
|
big: Boolean(this.value && this.value.length > 4),
|
||||||
|
})
|
||||||
|
}"
|
||||||
|
>
|
||||||
|
${
|
||||||
|
this.icon && !this.value && !this.image
|
||||||
|
? html`
|
||||||
|
<ha-icon .icon="${this.icon}"></ha-icon>
|
||||||
|
`
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
${
|
||||||
|
this.value && !this.image
|
||||||
|
? html`
|
||||||
|
<span>${this.value}</span>
|
||||||
|
`
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
${
|
||||||
|
this.label
|
||||||
|
? html`
|
||||||
|
<div
|
||||||
|
class="${
|
||||||
|
classMap({ label: true, big: this.label.length > 5 })
|
||||||
|
}"
|
||||||
|
>
|
||||||
|
<span>${this.label}</span>
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
${
|
||||||
|
this.description
|
||||||
|
? html`
|
||||||
|
<div class="title">${this.description}</div>
|
||||||
|
`
|
||||||
|
: ""
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected renderStyle(): TemplateResult {
|
||||||
return html`
|
return html`
|
||||||
<style>
|
<style>
|
||||||
.badge-container {
|
.badge-container {
|
||||||
@ -74,69 +148,24 @@ class HaLabelBadge extends PolymerElement {
|
|||||||
text-overflow: ellipsis;
|
text-overflow: ellipsis;
|
||||||
line-height: normal;
|
line-height: normal;
|
||||||
}
|
}
|
||||||
|
|
||||||
[hidden] {
|
|
||||||
display: none !important;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<div class="badge-container">
|
|
||||||
<div class="label-badge" id="badge">
|
|
||||||
<div class$="[[computeValueClasses(value)]]">
|
|
||||||
<ha-icon
|
|
||||||
icon="[[icon]]"
|
|
||||||
hidden$="[[computeHideIcon(icon, value, image)]]"
|
|
||||||
></ha-icon>
|
|
||||||
<span hidden$="[[computeHideValue(value, image)]]">[[value]]</span>
|
|
||||||
</div>
|
|
||||||
<div
|
|
||||||
hidden$="[[computeHideLabel(label)]]"
|
|
||||||
class$="[[computeLabelClasses(label)]]"
|
|
||||||
>
|
|
||||||
<span>[[label]]</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="title" hidden$="[[!description]]">[[description]]</div>
|
|
||||||
</div>
|
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
static get properties() {
|
protected updated(changedProperties: PropertyValues): void {
|
||||||
return {
|
if (changedProperties.has("image")) {
|
||||||
value: String,
|
this.shadowRoot!.getElementById("badge")!.style.backgroundImage = this
|
||||||
icon: String,
|
.image
|
||||||
label: String,
|
? `url(${this.image})`
|
||||||
description: String,
|
: "";
|
||||||
|
}
|
||||||
image: {
|
}
|
||||||
type: String,
|
|
||||||
observer: "imageChanged",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
computeValueClasses(value) {
|
declare global {
|
||||||
return value && value.length > 4 ? "value big" : "value";
|
interface HTMLElementTagNameMap {
|
||||||
|
"ha-label-badge": HaLabelBadge;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 + ")" : "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
customElements.define("ha-label-badge", HaLabelBadge);
|
customElements.define("ha-label-badge", HaLabelBadge);
|
Loading…
x
Reference in New Issue
Block a user