Add multi-path icon support and path attributes (#18189)

This commit is contained in:
Simon Vallières 2023-10-19 09:35:50 -04:00 committed by GitHub
parent feb371839c
commit 49f88a98a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 9 deletions

View File

@ -1,9 +1,9 @@
import { import {
css,
CSSResultGroup, CSSResultGroup,
html,
LitElement, LitElement,
PropertyValues, PropertyValues,
css,
html,
nothing, nothing,
} from "lit"; } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
@ -11,12 +11,12 @@ import { fireEvent } from "../common/dom/fire_event";
import { debounce } from "../common/util/debounce"; import { debounce } from "../common/util/debounce";
import { CustomIcon, customIcons } from "../data/custom_icons"; import { CustomIcon, customIcons } from "../data/custom_icons";
import { import {
checkCacheVersion,
Chunks, Chunks,
findIconChunk,
getIcon,
Icons, Icons,
MDI_PREFIXES, MDI_PREFIXES,
checkCacheVersion,
findIconChunk,
getIcon,
writeCache, writeCache,
} from "../data/iconsets"; } from "../data/iconsets";
import "./ha-svg-icon"; import "./ha-svg-icon";
@ -47,6 +47,8 @@ export class HaIcon extends LitElement {
@state() private _path?: string; @state() private _path?: string;
@state() private _secondaryPath?: string;
@state() private _viewBox?: string; @state() private _viewBox?: string;
@state() private _legacy = false; @state() private _legacy = false;
@ -55,6 +57,7 @@ export class HaIcon extends LitElement {
super.willUpdate(changedProps); super.willUpdate(changedProps);
if (changedProps.has("icon")) { if (changedProps.has("icon")) {
this._path = undefined; this._path = undefined;
this._secondaryPath = undefined;
this._viewBox = undefined; this._viewBox = undefined;
this._loadIcon(); this._loadIcon();
} }
@ -70,6 +73,7 @@ export class HaIcon extends LitElement {
} }
return html`<ha-svg-icon return html`<ha-svg-icon
.path=${this._path} .path=${this._path}
.secondaryPath=${this._secondaryPath}
.viewBox=${this._viewBox} .viewBox=${this._viewBox}
></ha-svg-icon>`; ></ha-svg-icon>`;
} }
@ -175,6 +179,7 @@ export class HaIcon extends LitElement {
return; return;
} }
this._path = icon.path; this._path = icon.path;
this._secondaryPath = icon.secondaryPath;
this._viewBox = icon.viewBox; this._viewBox = icon.viewBox;
} }

View File

@ -1,10 +1,19 @@
import { css, CSSResultGroup, LitElement, svg, SVGTemplateResult } from "lit"; import {
css,
CSSResultGroup,
LitElement,
nothing,
svg,
SVGTemplateResult,
} from "lit";
import { customElement, property } from "lit/decorators"; import { customElement, property } from "lit/decorators";
@customElement("ha-svg-icon") @customElement("ha-svg-icon")
export class HaSvgIcon extends LitElement { export class HaSvgIcon extends LitElement {
@property() public path?: string; @property() public path?: string;
@property() public secondaryPath?: string;
@property() public viewBox?: string; @property() public viewBox?: string;
protected render(): SVGTemplateResult { protected render(): SVGTemplateResult {
@ -13,11 +22,20 @@ export class HaSvgIcon extends LitElement {
viewBox=${this.viewBox || "0 0 24 24"} viewBox=${this.viewBox || "0 0 24 24"}
preserveAspectRatio="xMidYMid meet" preserveAspectRatio="xMidYMid meet"
focusable="false" focusable="false"
role="img" role="img"
aria-hidden="true" aria-hidden="true"
> >
<g> <g>
${this.path ? svg`<path d=${this.path}></path>` : ""} ${
this.path
? svg`<path class="primary-path" d=${this.path}></path>`
: nothing
}
${
this.secondaryPath
? svg`<path class="secondary-path" d=${this.secondaryPath}></path>`
: nothing
}
</g> </g>
</svg>`; </svg>`;
} }
@ -30,7 +48,7 @@ export class HaSvgIcon extends LitElement {
justify-content: center; justify-content: center;
position: relative; position: relative;
vertical-align: middle; vertical-align: middle;
fill: currentcolor; fill: var(--icon-primary-color, currentcolor);
width: var(--mdc-icon-size, 24px); width: var(--mdc-icon-size, 24px);
height: var(--mdc-icon-size, 24px); height: var(--mdc-icon-size, 24px);
} }
@ -40,6 +58,13 @@ export class HaSvgIcon extends LitElement {
pointer-events: none; pointer-events: none;
display: block; display: block;
} }
path.primary-path {
opacity: var(--icon-primary-opactity, 1);
}
path.secondary-path {
fill: var(--icon-secondary-color, currentcolor);
opacity: var(--icon-secondary-opactity, 0.5);
}
`; `;
} }
} }

View File

@ -2,6 +2,7 @@ import { customIconsets } from "./custom_iconsets";
export interface CustomIcon { export interface CustomIcon {
path: string; path: string;
secondaryPath?: string;
viewBox?: string; viewBox?: string;
} }