-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicon.component.ts
73 lines (63 loc) · 1.54 KB
/
icon.component.ts
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
import {
Component,
ViewEncapsulation,
ChangeDetectionStrategy,
Input,
ElementRef,
isDevMode,
HostBinding,
} from "@angular/core";
import { a11y } from "@electric/style";
import { camelToKebabCase, match } from "@electric/utils";
import { IconRegistry } from "./icon.service";
import { IconName, IconSize } from "./icon.types";
@Component({
selector: "elx-icon",
template: "",
styleUrls: ["./icon.component.scss"],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class IconComponent {
@Input()
get icon() { return this._icon; }
set icon(value) {
this._icon = value;
if (value) this.render();
}
private _icon?: IconName;
@Input() size: IconSize = "md";
@HostBinding("class")
get hostClasses() {
let base = "elx-icon";
let iconClass = this.icon
? `${base}--${camelToKebabCase(this.icon)}`
: "";
return [base, iconClass];
}
@HostBinding("attr.aria-hidden")
readonly ariaHidden = "true";
@HostBinding("style.fontSize")
get fontSize() {
return match(this.size, {
xs: () => a11y.rem(16),
sm: () => a11y.rem(18),
md: () => a11y.rem(20),
lg: () => a11y.rem(24),
});
}
private get _element() {
return this._elementRef.nativeElement;
}
constructor (
private _elementRef: ElementRef<HTMLElement>,
private _registry: IconRegistry,
) {}
private render(): void {
if (this._registry.has(this.icon!)) {
this._element.innerHTML = this._registry.get(this.icon!)!;
} else if (isDevMode()) {
console.warn(`No definition found for icon '${this.icon}'!`);
}
}
}