-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdi-component.js
More file actions
140 lines (116 loc) · 3.94 KB
/
mdi-component.js
File metadata and controls
140 lines (116 loc) · 3.94 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
const baseURI = document.currentScript ? document.currentScript.baseURI : "";
class MDIComponent extends HTMLElement {
constructor(...args) {
super(...args);
this.shadow = this.attachShadow({ mode: 'open' });
}
get preTemplate() {
const name = this.getAttribute("name");
const filePath = `${baseURI}/node_modules/@mdi/svg/svg/${name}.svg`;
if (!name) throw new Error("'path' and 'name' attributes are both null");
return /*html*/`
<object type="image/svg+xml" data="${filePath}"></object>
`
}
template(path) {
const inheritedStyle = window.getComputedStyle(this);
const inheritedSize = inheritedStyle.getPropertyValue("font-size");
const inheritedColor = inheritedStyle.getPropertyValue("color");
const size = this.getAttribute("size") || inheritedSize;
const color = this.getAttribute("color") || inheritedColor;
const containerClasses = ["container"];
if (this.hasAttribute("horizontal")) containerClasses.push("flip-h");
if (this.hasAttribute("vertical")) containerClasses.push("flip-v");
return /*html*/`
<style>
.container {
display: inline-block;
}
.flip-h {
-webkit-transform: scaleX(-1);
transform: scaleX(-1);
filter: FlipH;
-ms-filter: "FlipH";
}
.flip-v {
-webkit-transform: scaleY(-1);
transform: scaleY(-1);
filter: FlipV;
-ms-filter: "FlipV";
}
.rotate {
-webkit-transform: rotate(${this.getAttribute("rotate")}deg);
-ms-transform: rotate(${this.getAttribute("rotate")}deg);
transform: rotate(${this.getAttribute("rotate")}deg);
}
@-webkit-keyframes mdi-spin {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(359deg);
transform: rotate(359deg);
}
}
@keyframes mdi-spin {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(359deg);
transform: rotate(359deg);
}
}
.spin {
-webkit-animation: mdi-spin 2s infinite linear;
animation: mdi-spin 2s infinite linear;
}
</style>
<span class="container ${this.hasAttribute("horizontal") ? "flip-h" : ""}">
<span class="container ${this.hasAttribute("vertical") ? "flip-v" : ""}">
<span class="container ${this.getAttribute("rotate") ? "rotate" : ""}">
<span class="container ${this.hasAttribute("spin") ? "spin" : ""}">
<svg height="${size}" width="${size}" viewBox="0 0 24 24">
<path fill="${color}" d="${path}"/>
</svg>
</span>
</span>
</span>
</span>
`;
}
static get observedAttributes() {
return ["path", "name", "size", "horizontal", "vertical", "spin", "rotate"];
}
attributeChangedCallback(name, oldValue, newValue) {
this.render();
}
connectedCallback() {
this.render();
}
render() {
const path = this.getAttribute("path");
if (path) return this.setView(this.template(path));
this.setPreTemplate();
}
setPreTemplate() {
this.setView(this.preTemplate);
const objectElement = this.shadow.querySelector("object");
objectElement.addEventListener("load", () => {
const svgDoc = objectElement.contentDocument;
const pathElement = svgDoc.querySelector("path");
const path = pathElement.getAttribute("d");
this.setAttribute("path", path);
this.render();
})
}
setView(template) {
const templateEl = document.createElement("template");
templateEl.innerHTML = template;
this.shadow.innerHTML = ``;
this.shadow.appendChild(templateEl.content.cloneNode(true));
}
}
customElements.define('mdi-component', MDIComponent);