forked from maximilian-lindsey/showcar-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icons.js
39 lines (31 loc) · 984 Bytes
/
icons.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
const icons = {};
const regex = /^\.\/([^\.]+)\.svg$/gm;
function importAll(r) {
r.keys().forEach(key => (icons[key.toLowerCase().replace(regex, "$1")] = r(key)));
}
importAll(require.context("../icons", true, /\.svg$/));
const proto = Object.create(HTMLElement.prototype);
const inlineIconIntoElement = (el) => {
const type = el.getAttribute("type");
try {
el.innerHTML = icons[type.toLowerCase()].default;
} catch(e) {
console.error('Could not create icon with type', type, e);
}
};
proto.attachedCallback = function() {
inlineIconIntoElement(this);
};
proto.attributeChangedCallback = function(attributeName, previousValue, value) {
if (attributeName === "type") {
inlineIconIntoElement(this);
}
};
try {
document.registerElement("as24-icon", { prototype: proto });
} catch (e) {
if (window && window.console) {
window.console.warn('Failed to register CustomElement "as24-icon".', e);
}
}
window.showcarIconNames = Object.keys(icons);