/
svg-icon.js
154 lines (123 loc) · 4.35 KB
/
svg-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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
function LogError(message = '', stack = '') {
console.log(`%c Icon element: %c ${message}`, 'background-color: crimson; color: whitesmoke; font-weight: bold;', 'color: crimson;', '\n', stack);
}
function IconCore (Base) {
class Icon extends Base {
#_size = null;
#_src = null;
abortSignal = null;
set size (val = 20) {
if(['string', 'number'].includes(typeof val)) {
this.#_size = val;
}
}
get size () {
return this.#_size;
}
set src (val = '') {
this.#_src = `${val.replace(/(\.svg)/i, '')}.svg`;
}
get src () {
return this.#_src;
}
constructor(size, src) {
super();
this.#_size = size;
this.#_src = src;
}
async fetchIcon () {
const abortController = new AbortController();
this.abortSignal = abortController.signal;
return fetch(this.src, {
signal: this.abortSignal,
})
.then(res => {
if(res.status !== 200) {
LogError(`Server responded with code: ${res.status}.`, res.statusText);
}
return res;
})
.then(res => res.text())
.catch(err => LogError('Error while fetching an icon.', err));
}
}
return Icon;
}
const creatorClass = (classAcc, classItem) => classItem(classAcc);
const extenderClass = (...parts) => parts.reduce(creatorClass, HTMLElement);
class Icon extends extenderClass(IconCore) {
#_svg = null;
#_sealed = false;
#_root = null;
#_style = null;
// element created
constructor () {
super();
}
async #fetch () {
const data = await this.fetchIcon();
this.#replaceSVG(data);
}
#replaceSVG (data) {
const svg = this.#_root.querySelector('svg');
this.#_svg = document.createRange().createContextualFragment(data).firstElementChild;
this.#updateSVG();
svg !== null ? this.#_root.replaceChild(this.#_svg, svg) : this.#_root.appendChild(this.#_svg);
}
#updateSVG () {
if(this.#_svg) {
this.#_svg.setAttribute('width', parseInt(this.size));
this.#_svg.setAttribute('height', parseInt(this.size));
this.#_svg.setAttribute('fill', 'currentColor');
}
}
#createShadow () {
this.#_root = this;
if(this.#_sealed) {
this.#_root = this.attachShadow({ mode: 'closed' });
this.#_style = document.createElement('style');
this.#_style.setAttribute('type', 'text/css');
this.#_style.textContent = `:host{display:inline-flex;align-items:center;justify-content:center;}svg{fill:currentColor;}`;
this.#_root.appendChild(this.#_style);
}
}
async connectedCallback() {
this.src = this.getAttribute('src');
this.size = this.getAttribute('size');
this.#_sealed = this.hasAttribute('hollow') ? true : false;
this.#_sealed = this.hasAttribute('sealed') ? true : false;
this.#createShadow();
// browser calls this method when the element is added to the document
// (can be called many times if an element is repeatedly added/removed)
}
disconnectedCallback() {
this.abortSignal.abort();
// browser calls this method when the element is removed from the document
// (can be called many times if an element is repeatedly added/removed)
}
static get observedAttributes() {
return ['size', 'src'];
}
attributeChangedCallback(name, oldValue, newValue) {
switch(name) {
case 'size':
// Change the size
this.size = newValue;
this.#updateSVG();
break;
case 'src':
this.src = newValue;
this.#fetch();
break;
default:
break;
}
// called when one of attributes listed above is modified
}
adoptedCallback() {
LogError('Moving icon to new document is not implemented!');
// called when the element is moved to a new document
// (happens in document.adoptNode, very rarely used)
}
}
customElements.define('svg-icon', Icon);