fix: Allow icon styles to be overridden (#1794)
This commit is contained in:
+10
-3
@@ -1,6 +1,7 @@
|
|||||||
import { HomeAssistant } from '@dermotduffy/custom-card-helpers';
|
import { HomeAssistant } from '@dermotduffy/custom-card-helpers';
|
||||||
import { CSSResultGroup, html, LitElement, TemplateResult, unsafeCSS } from 'lit';
|
import { CSSResultGroup, html, LitElement, TemplateResult, unsafeCSS } from 'lit';
|
||||||
import { customElement, property } from 'lit/decorators.js';
|
import { customElement, property } from 'lit/decorators.js';
|
||||||
|
import { styleMap } from 'lit/directives/style-map.js';
|
||||||
import { IconController } from '../components-lib/icon-controller';
|
import { IconController } from '../components-lib/icon-controller';
|
||||||
import iconStyle from '../scss/icon.scss';
|
import iconStyle from '../scss/icon.scss';
|
||||||
import { Icon } from '../types';
|
import { Icon } from '../types';
|
||||||
@@ -17,11 +18,13 @@ export class FrigateCardIcon extends LitElement {
|
|||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
const customIconURL = this._controller.getCustomIcon(this.icon);
|
const customIconURL = this._controller.getCustomIcon(this.icon);
|
||||||
|
const style = styleMap(this.icon?.style || {});
|
||||||
|
|
||||||
if (customIconURL) {
|
if (customIconURL) {
|
||||||
return html`<img src="${customIconURL}" />`;
|
return html`<img style="${style}" src="${customIconURL}" />`;
|
||||||
}
|
}
|
||||||
if (this.icon?.icon) {
|
if (this.icon?.icon) {
|
||||||
return html`<ha-icon icon="${this.icon.icon}"></ha-icon>`;
|
return html`<ha-icon style="${style}" icon="${this.icon.icon}"></ha-icon>`;
|
||||||
}
|
}
|
||||||
if (this.hass && this.icon?.entity) {
|
if (this.hass && this.icon?.entity) {
|
||||||
const stateObj = this._controller.createStateObjectForStateBadge(
|
const stateObj = this._controller.createStateObjectForStateBadge(
|
||||||
@@ -29,7 +32,11 @@ export class FrigateCardIcon extends LitElement {
|
|||||||
this.icon.entity,
|
this.icon.entity,
|
||||||
);
|
);
|
||||||
if (stateObj) {
|
if (stateObj) {
|
||||||
|
// As a special case, need to pass in a color in order for the state
|
||||||
|
// color to be overridden.
|
||||||
return html`<state-badge
|
return html`<state-badge
|
||||||
|
style="${style}"
|
||||||
|
.color="${this.icon.style?.color ?? undefined}"
|
||||||
.stateColor=${this.icon.stateColor ?? true}
|
.stateColor=${this.icon.stateColor ?? true}
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.stateObj=${stateObj}
|
.stateObj=${stateObj}
|
||||||
@@ -37,7 +44,7 @@ export class FrigateCardIcon extends LitElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.icon?.fallback) {
|
if (this.icon?.fallback) {
|
||||||
return html`<ha-icon icon="${this.icon.fallback}"></ha-icon>`;
|
return html`<ha-icon style="${style}" icon="${this.icon.fallback}"></ha-icon>`;
|
||||||
}
|
}
|
||||||
return html``;
|
return html``;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ export class FrigateCardMenu extends LitElement {
|
|||||||
icon: button.icon,
|
icon: button.icon,
|
||||||
entity: button.entity,
|
entity: button.entity,
|
||||||
stateColor: button.state_color,
|
stateColor: button.state_color,
|
||||||
|
style: button.style,
|
||||||
fallback: 'mdi:gesture-tap-button',
|
fallback: 'mdi:gesture-tap-button',
|
||||||
}}
|
}}
|
||||||
></frigate-card-icon>
|
></frigate-card-icon>
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
} from 'lit';
|
} from 'lit';
|
||||||
import { customElement, property, state } from 'lit/decorators.js';
|
import { customElement, property, state } from 'lit/decorators.js';
|
||||||
import { ifDefined } from 'lit/directives/if-defined.js';
|
import { ifDefined } from 'lit/directives/if-defined.js';
|
||||||
import { StyleInfo, styleMap } from 'lit/directives/style-map.js';
|
import { styleMap } from 'lit/directives/style-map.js';
|
||||||
import { actionHandler } from '../action-handler-directive.js';
|
import { actionHandler } from '../action-handler-directive.js';
|
||||||
import { MenuSubmenu, MenuSubmenuItem, MenuSubmenuSelect } from '../config/types.js';
|
import { MenuSubmenu, MenuSubmenuItem, MenuSubmenuSelect } from '../config/types.js';
|
||||||
import submenuStyle from '../scss/submenu.scss';
|
import submenuStyle from '../scss/submenu.scss';
|
||||||
@@ -69,6 +69,7 @@ export class FrigateCardSubmenu extends LitElement {
|
|||||||
.icon=${{
|
.icon=${{
|
||||||
icon: item.icon,
|
icon: item.icon,
|
||||||
entity: item.entity,
|
entity: item.entity,
|
||||||
|
style: item.style,
|
||||||
}}
|
}}
|
||||||
style="${styleMap(item.style || {})}"
|
style="${styleMap(item.style || {})}"
|
||||||
></frigate-card-icon>
|
></frigate-card-icon>
|
||||||
@@ -81,6 +82,7 @@ export class FrigateCardSubmenu extends LitElement {
|
|||||||
return html``;
|
return html``;
|
||||||
}
|
}
|
||||||
const items = this.submenu.items as MenuSubmenuItem[];
|
const items = this.submenu.items as MenuSubmenuItem[];
|
||||||
|
const style = styleMap(this.submenu.style || {});
|
||||||
return html`
|
return html`
|
||||||
<ha-button-menu
|
<ha-button-menu
|
||||||
corner=${'BOTTOM_LEFT'}
|
corner=${'BOTTOM_LEFT'}
|
||||||
@@ -93,7 +95,7 @@ export class FrigateCardSubmenu extends LitElement {
|
|||||||
@click=${(ev) => stopEventFromActivatingCardWideActions(ev)}
|
@click=${(ev) => stopEventFromActivatingCardWideActions(ev)}
|
||||||
>
|
>
|
||||||
<ha-icon-button
|
<ha-icon-button
|
||||||
style="${styleMap((this.submenu.style as StyleInfo) || {})}"
|
style="${style}"
|
||||||
class="button"
|
class="button"
|
||||||
slot="trigger"
|
slot="trigger"
|
||||||
.label=${this.submenu.title || ''}
|
.label=${this.submenu.title || ''}
|
||||||
@@ -108,6 +110,7 @@ export class FrigateCardSubmenu extends LitElement {
|
|||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
<frigate-card-icon
|
<frigate-card-icon
|
||||||
|
style="${style}"
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.icon=${typeof this.submenu.icon === 'string'
|
.icon=${typeof this.submenu.icon === 'string'
|
||||||
? {
|
? {
|
||||||
@@ -214,6 +217,7 @@ export class FrigateCardSubmenuSelect extends LitElement {
|
|||||||
icon: this.submenuSelect.icon,
|
icon: this.submenuSelect.icon,
|
||||||
entity: entityID,
|
entity: entityID,
|
||||||
fallback: 'mdi:format-list-bulleted',
|
fallback: 'mdi:format-list-bulleted',
|
||||||
|
style: this.submenuSelect.style,
|
||||||
},
|
},
|
||||||
|
|
||||||
type: 'custom:frigate-card-menu-submenu',
|
type: 'custom:frigate-card-menu-submenu',
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
LovelaceCardEditor,
|
LovelaceCardEditor,
|
||||||
Themes,
|
Themes,
|
||||||
} from '@dermotduffy/custom-card-helpers';
|
} from '@dermotduffy/custom-card-helpers';
|
||||||
|
import { StyleInfo } from 'lit/directives/style-map';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
export type ClipsOrSnapshots = 'clips' | 'snapshots';
|
export type ClipsOrSnapshots = 'clips' | 'snapshots';
|
||||||
@@ -145,6 +146,9 @@ export interface Icon {
|
|||||||
|
|
||||||
// If an icon is not otherwise resolved / available, this will be used instead.
|
// If an icon is not otherwise resolved / available, this will be used instead.
|
||||||
fallback?: string;
|
fallback?: string;
|
||||||
|
|
||||||
|
// Styling to apply to the icon.
|
||||||
|
style?: StyleInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
// *************************************************************************
|
// *************************************************************************
|
||||||
|
|||||||
Reference in New Issue
Block a user