|
1 | 1 | import { PFSize } from "#common/enums"; |
2 | 2 |
|
| 3 | +import Styles from "#elements/AppIcon.css"; |
3 | 4 | import { AKElement } from "#elements/Base"; |
4 | 5 |
|
5 | | -import { match, P } from "ts-pattern"; |
6 | | - |
7 | | -import { msg } from "@lit/localize"; |
8 | | -import { css, CSSResult, html, TemplateResult } from "lit"; |
| 6 | +import { msg, str } from "@lit/localize"; |
| 7 | +import { CSSResult, html, TemplateResult } from "lit"; |
9 | 8 | import { customElement, property } from "lit/decorators.js"; |
10 | 9 |
|
11 | 10 | import PFFAIcons from "@patternfly/patternfly/base/patternfly-fa-icons.css"; |
12 | | -import PFAvatar from "@patternfly/patternfly/components/Avatar/avatar.css"; |
13 | 11 |
|
14 | 12 | export interface IAppIcon { |
15 | | - name?: string; |
16 | | - icon?: string; |
17 | | - size?: PFSize; |
| 13 | + name?: string | null; |
| 14 | + icon?: string | null; |
| 15 | + size?: PFSize | null; |
18 | 16 | } |
19 | 17 |
|
20 | 18 | @customElement("ak-app-icon") |
21 | 19 | export class AppIcon extends AKElement implements IAppIcon { |
| 20 | + public static readonly FontAwesomeProtocol = "fa://"; |
| 21 | + |
| 22 | + static styles: CSSResult[] = [PFFAIcons, Styles]; |
| 23 | + |
22 | 24 | @property({ type: String }) |
23 | | - name?: string; |
| 25 | + public name: string | null = null; |
24 | 26 |
|
25 | 27 | @property({ type: String }) |
26 | | - icon?: string; |
| 28 | + public icon: string | null = null; |
27 | 29 |
|
28 | 30 | @property({ reflect: true }) |
29 | | - size: PFSize = PFSize.Medium; |
30 | | - |
31 | | - static styles: CSSResult[] = [ |
32 | | - PFFAIcons, |
33 | | - PFAvatar, |
34 | | - css` |
35 | | - :host { |
36 | | - max-height: calc(var(--icon-height) + var(--icon-border) + var(--icon-border)); |
37 | | -
|
38 | | - display: flex; |
39 | | - place-content: center; |
40 | | - } |
41 | | - :host([size="pf-m-lg"]) { |
42 | | - --icon-height: 4rem; |
43 | | - --icon-border: 0.25rem; |
44 | | - } |
45 | | - :host([size="pf-m-md"]) { |
46 | | - --icon-height: 2rem; |
47 | | - --icon-border: 0.125rem; |
48 | | - } |
49 | | - :host([size="pf-m-sm"]) { |
50 | | - --icon-height: 1rem; |
51 | | - --icon-border: 0.125rem; |
52 | | - } |
53 | | - :host([size="pf-m-xl"]) { |
54 | | - --icon-height: 6rem; |
55 | | - --icon-border: 0.25rem; |
56 | | - } |
57 | | - .pf-c-avatar { |
58 | | - --pf-c-avatar--BorderRadius: 0; |
59 | | - --pf-c-avatar--Height: calc( |
60 | | - var(--icon-height) + var(--icon-border) + var(--icon-border) |
61 | | - ); |
62 | | - --pf-c-avatar--Width: calc( |
63 | | - var(--icon-height) + var(--icon-border) + var(--icon-border) |
64 | | - ); |
65 | | - } |
66 | | - .icon { |
67 | | - --app-icon-shadow-blend-color: color-mix( |
68 | | - in srgb, |
69 | | - var(--app-icon--shadow-background-color, var(--pf-global--BackgroundColor--150)) |
70 | | - 100%, |
71 | | - black 100% |
72 | | - ); |
73 | | -
|
74 | | - font-size: var(--icon-font-size, var(--icon-height)); |
75 | | - color: var(--ak-global--Color--100); |
76 | | - padding: var(--icon-border); |
77 | | - max-height: calc(var(--icon-height) + var(--icon-border) + var(--icon-border)); |
78 | | - line-height: calc(var(--icon-height) + var(--icon-border) + var(--icon-border)); |
79 | | - filter: drop-shadow(-0.5px 0px 0px var(--app-icon-shadow-blend-color)); |
80 | | - } |
81 | | -
|
82 | | - div { |
83 | | - height: calc(var(--icon-height) + var(--icon-border) + var(--icon-border)); |
84 | | - } |
85 | | - `, |
86 | | - ]; |
| 31 | + public size: PFSize = PFSize.Medium; |
87 | 32 |
|
88 | 33 | render(): TemplateResult { |
89 | | - // prettier-ignore |
90 | | - return match([this.name, this.icon]) |
91 | | - .with([P.nullish, P.nullish], |
92 | | - () => html`<div><i part="icon" aria-hidden="true" class="icon fas fa-question-circle"></i></div>`) |
93 | | - .with([P._, P.string.startsWith("fa://")], |
94 | | - ([_name, icon]) => html`<div><i part="icon" aria-hidden="true" class="icon fas ${icon.replaceAll("fa://", "")}"></i></div>`) |
95 | | - .with([P._, P.string], |
96 | | - ([_name, icon]) => html`<img part="icon" aria-hidden="true" class="icon pf-c-avatar" src="${icon}" alt="${msg("Application Icon")}" />`) |
97 | | - .with([P.string, P.nullish], |
98 | | - ([name]) => html`<span part="icon" aria-hidden="true" class="icon">${name.charAt(0).toUpperCase()}</span>`) |
99 | | - .exhaustive(); |
| 34 | + const applicationName = this.name ?? msg("Application"); |
| 35 | + const label = msg(str`${applicationName} Icon`); |
| 36 | + |
| 37 | + if (this.icon?.startsWith(AppIcon.FontAwesomeProtocol)) { |
| 38 | + return html`<i |
| 39 | + part="icon font-awesome" |
| 40 | + role="img" |
| 41 | + aria-label=${label} |
| 42 | + class="icon fas ${this.icon.slice(AppIcon.FontAwesomeProtocol.length)}" |
| 43 | + ></i>`; |
| 44 | + } |
| 45 | + |
| 46 | + const insignia = this.name?.charAt(0).toUpperCase() ?? "�"; |
| 47 | + |
| 48 | + if (this.icon) { |
| 49 | + return html`<img |
| 50 | + part="icon image" |
| 51 | + role="img" |
| 52 | + aria-label=${label} |
| 53 | + class="icon" |
| 54 | + src=${this.icon} |
| 55 | + alt=${insignia} |
| 56 | + />`; |
| 57 | + } |
| 58 | + |
| 59 | + return html`<span part="icon insignia" role="img" aria-label=${label} class="icon" |
| 60 | + >${insignia}</span |
| 61 | + >`; |
100 | 62 | } |
101 | 63 | } |
102 | 64 |
|
|
0 commit comments