forked from maximilian-lindsey/showcar-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icons.js
41 lines (33 loc) · 997 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
40
41
var icons = {};
var regex = /^\.\/([^\.]+)\.svg$/gm;
function importAll(r) {
r.keys().forEach(key => (icons[key.toLowerCase().replace(regex, "$1")] = r(key)));
}
importAll(require.context("../icons", true, /\.svg$/));
var proto = Object.create(HTMLElement.prototype);
function inlineIconIntoElement(el) {
var type = el.getAttribute("type");
try {
if (type) {
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);