-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIcon.ts
More file actions
142 lines (124 loc) · 3.55 KB
/
Copy pathIcon.ts
File metadata and controls
142 lines (124 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { LitElement, html, nothing, css } from 'lit';
import { unsafeSVG } from 'lit/directives/unsafe-svg.js';
import { JSXCustomElement } from '../../../types/jsx-custom-element.type';
import { safeCustomElement } from '@/lib/safe-custom-element';
import {
icons,
IconName,
FunctionalIconName,
DistinctiveIconName,
} from './icons';
export interface IconAttributes {
variant?: 'default' | 'circle';
label?: string;
icon?: IconName | FunctionalIconName | DistinctiveIconName;
set?: 'default' | 'functional' | 'distinctive';
}
@safeCustomElement('evg-icon')
export class Icon extends LitElement {
variant?: 'default' | 'circle';
label?: string;
icon?: IconName | FunctionalIconName | DistinctiveIconName;
set?: 'default' | 'functional' | 'distinctive';
static readonly properties = {
variant: { type: String },
label: { type: String },
icon: { type: String },
set: { type: String },
};
// Handles shadow dom svgs when using the icon prop.
// We also style the light dom SVG inside the
// SCSS. Make sure to update both.
static styles = css`
svg {
aspect-ratio: 1 / 1;
display: block;
fill: currentColor;
flex: 0 0 var(--evg-icon-size);
height: var(--evg-icon-size);
min-width: var(--evg-icon-size);
position: relative;
width: var(--evg-icon-size);
z-index: 1;
}
path {
fill: currentColor;
}
:host([variant='circle']) {
svg {
transform: scale(0.75);
}
}
`;
// Static cache shared across all Icon instances
private static _functionalIcons?: Record<string, string>;
private static _distinctiveIcons?: Record<string, string>;
private static _functionalPromise?: Promise<Record<string, string>>;
private static _distinctivePromise?: Promise<Record<string, string>>;
constructor() {
super();
}
private get iconSet(): Record<string, string> {
if (this.set === 'functional') {
return Icon._functionalIcons ?? {};
}
if (this.set === 'distinctive') {
return Icon._distinctiveIcons ?? {};
}
return icons;
}
private async loadIconSet() {
if (this.set === 'functional' && !Icon._functionalIcons) {
if (!Icon._functionalPromise) {
Icon._functionalPromise = import('./functional-icons').then(
(m) => m.functionalIcons,
);
}
Icon._functionalIcons = await Icon._functionalPromise;
this.requestUpdate();
} else if (this.set === 'distinctive' && !Icon._distinctiveIcons) {
if (!Icon._distinctivePromise) {
Icon._distinctivePromise = import('./distinctive-icons').then(
(m) => m.distinctiveIcons,
);
}
Icon._distinctiveIcons = await Icon._distinctivePromise;
this.requestUpdate();
}
}
updated(changedProperties: Map<string, unknown>) {
super.updated(changedProperties);
if (
(changedProperties.has('set') || changedProperties.has('icon')) &&
(this.set === 'functional' || this.set === 'distinctive')
) {
this.loadIconSet();
}
}
render() {
const { label, icon } = this;
const svg = icon ? this.iconSet[icon] : '';
return html`
<div
part="icon"
aria-hidden="${!label}"
aria-label="${label || nothing}"
role="img"
>
<slot>${unsafeSVG(svg)}</slot>
</div>
`;
}
}
declare global {
interface HTMLElementTagNameMap {
'evg-icon': IconAttributes;
}
}
declare module 'react' {
namespace JSX {
interface IntrinsicElements {
'evg-icon': JSXCustomElement<IconAttributes>;
}
}
}