-
Notifications
You must be signed in to change notification settings - Fork 25
/
icon.js
72 lines (59 loc) · 1.61 KB
/
icon.js
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
import '../colors/colors.js';
import { css, html, LitElement, noChange } from 'lit';
import { fixSvg } from './fix-svg.js';
import { guard } from 'lit/directives/guard.js';
import { iconStyles } from './icon-styles.js';
import { loadSvg } from '../../generated/icons/presetIconLoader.js';
import { RtlMixin } from '../../mixins/rtl/rtl-mixin.js';
import { unsafeSVG } from 'lit/directives/unsafe-svg.js';
import { until } from 'lit/directives/until.js';
class Icon extends RtlMixin(LitElement) {
static get properties() {
return {
icon: {
type: String,
reflect: true
}
};
}
static get styles() {
return [ iconStyles, css`
:host([icon*="tier1:"]) {
height: var(--d2l-icon-height, 18px);
width: var(--d2l-icon-width, 18px);
}
:host([icon*="tier2:"]) {
height: var(--d2l-icon-height, 24px);
width: var(--d2l-icon-width, 24px);
}
:host([icon*="tier3:"]) {
height: var(--d2l-icon-height, 30px);
width: var(--d2l-icon-width, 30px);
}
`];
}
render() {
return guard([this.icon], () => until(this._getIcon(), noChange));
}
_fixSvg(svgStr) {
if (svgStr === undefined) {
return undefined;
}
const template = document.createElement('template');
template.innerHTML = svgStr;
const svg = template.content.firstChild;
fixSvg(svg);
return html`${unsafeSVG(template.innerHTML)}`;
}
async _getIcon() {
if (this.icon) {
let icon = this.icon;
if (icon.substring(0, 4) === 'd2l-') {
icon = icon.substring(4);
}
const svg = await loadSvg(icon);
return this._fixSvg(svg ? svg.val : undefined);
}
}
}
customElements.define('d2l-icon', Icon);