-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlast-icon.js
416 lines (388 loc) · 10.9 KB
/
last-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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
const JSDELIVR = "https://cdn.jsdelivr.net/";
const CACHE = {};
/**
* @typedef IconSet
* @property {String} alias Short two letters alias
* @property {Function} svgPath The svg path
* @property {Boolean} [fixFill] Does this set needs fixing fill:currentColor ?
* @property {String} [useStroke] Add stroke to svg
* @property {String} [defaultStroke] Default stroke to use (if supports stroke)
* @property {String} [defaultType] Default type to use (when there are multiple types)
* @property {Function} [fontClass] Font class
* @property {Boolean} [opticalFont] Is an optical font?
* @property {String} [name] Full name (injected automatically)
*/
/**
* @typedef Options
* @property {Boolean} debug Should we output messages to console
* @property {Boolean} lazy Load icons lazily
* @property {Object} replaceName Transparently replace icons with other values
* @property {Array} fonts Icon sets using font icons rather than svg
* @property {String} defaultSet Default icon set
* @property {Object.<string, IconSet>} sets Available iconsets
*/
const options = {
debug: false,
lazy: true,
replaceName: {},
fonts: [],
defaultSet: "tabler",
defaultStroke: 2,
sets: {
bootstrap: {
alias: "bs",
svgPath: () => `${JSDELIVR}npm/bootstrap-icons@1/icons/{icon}.svg`,
},
flags: {
alias: "fl",
// types: ["4x3", "1x1"],
defaultType: "4x3",
svgPath: () =>
`${JSDELIVR}npm/flag-svg-collection@1/flags/{type}/{icon}.svg`,
},
iconoir: {
alias: "in",
svgPath: () => `${JSDELIVR}gh/lucaburgio/iconoir/icons/{icon}.svg`,
fontClass: () => "iconoir-{icon}",
useStroke: true,
},
iconpark: {
alias: "ip",
types: [], // see full list here https://github.com/bytedance/IconPark/tree/master/source
svgPath: () =>
`${JSDELIVR}gh/bytedance/IconPark/source/{type}/{icon}.svg`,
useStroke: true,
},
lucide: {
alias: "lu",
svgPath: () => `${JSDELIVR}npm/lucide-static/icons/{icon}.svg`,
},
material: {
alias: "mi",
// types: ["filled", "outlined", "round", "sharp", "two-tone"],
defaultType: "filled",
svgPath: () =>
`${JSDELIVR}npm/@material-design-icons/svg/{type}/{icon}.svg`,
fontClass: (type) => {
if (type === "filled") {
return "material-icons";
}
return "material-icons-{type}";
},
},
phosphor: {
alias: "ph",
// types: ["regular", "bold", "duotone", "fill", "light", "thin"],
defaultType: "regular",
svgPath: (type) => {
if (type === "regular") {
return `${JSDELIVR}npm/@phosphor-icons/core@2/assets/{type}/{icon}.svg`;
}
return `${JSDELIVR}npm/@phosphor-icons/core@2/assets/{type}/{icon}-{type}.svg`;
},
fontClass: (type) => {
if (type === "regular") {
return "ph ph-{icon}";
}
return "ph-{type} ph-{icon}";
},
},
supertiny: {
alias: "st",
svgPath: () => `${JSDELIVR}npm/super-tiny-icons/images/svg/{icon}.svg`,
},
symbols: {
alias: "ms",
// types: ["outlined", "rounded", "sharp"],
defaultType: "outlined",
svgPath: () =>
`${JSDELIVR}npm/@material-symbols/[email protected]/{type}/{icon}.svg`,
fixFill: true,
fontClass: () => "material-symbols-{type}",
opticalFont: true,
},
tabler: {
alias: "ti",
svgPath: () => `${JSDELIVR}npm/@tabler/icons@2/icons/{icon}.svg`,
useStroke: true,
fontClass: () => "ti ti-{icon}",
},
},
};
/**
* @var {IntersectionObserver}
*/
const observer = new window.IntersectionObserver((entries, observerRef) => {
for (const entry of entries) {
if (entry.isIntersecting) {
observerRef.unobserve(entry.target);
entry.target.init();
}
}
});
/**
* @param {string} value
* @param {string} iconName
* @param {string} iconType
* @return {string}
*/
function replacePlaceholders(value, iconName, iconType) {
let v = value;
v = v.replace("{icon}", iconName);
if (iconType) {
v = v.replaceAll("{type}", iconType);
} else {
// Maybe we want to remove the type like in material icons
v = v.replace("-{type}", "");
}
return v;
}
function log(message) {
if (options.debug) {
console.log(`[l-i] ${message}`);
}
}
/**
* @param {string} iconName
* @param {IconSet} iconSet
* @param {string} iconType
* @return {Promise<String, Error>}
*/
function getIconSvg(iconName, iconSet, iconType) {
let iconUrl = iconSet.svgPath(iconType);
if (!iconUrl) {
throw Error(`Icon set ${iconSet} does not exists`);
}
const cacheKey = `${iconSet.name}-${iconName}-${iconType || "base"}`;
iconUrl = replacePlaceholders(iconUrl, iconName, iconType);
// If we have it in cache
if (iconUrl && CACHE[cacheKey]) {
log(`Fetching ${cacheKey} from cache`);
return CACHE[cacheKey];
}
// Or resolve
log(`Fetching ${cacheKey} from url ${iconUrl}`);
CACHE[cacheKey] = fetch(iconUrl).then((response) => {
if (response.ok) {
return response.text();
}
throw Error(response.status);
});
return CACHE[cacheKey];
}
/**
* @param {LastIcon} inst
* @param {string} name
* @param {IconSet} iconSet
* @param {string} type
*/
function refreshIcon(inst, name, iconSet, type) {
let iconName = name;
let iconType = type;
// Replace name
if (options.replaceName[iconName]) {
iconName = options.replaceName[iconName];
}
// Set default type if any
if (!iconType && iconSet.defaultType) {
iconType = iconSet.defaultType;
}
// Use font (if not using a specific stroke)
if (options.fonts.includes(iconSet.name) && !inst.hasAttribute("stroke")) {
log(`Using font for ${iconName}`);
let iconClass = iconSet.fontClass(iconType);
const nameAsClass = iconClass.includes("{icon}");
iconClass = replacePlaceholders(iconClass, iconName, iconType);
if (nameAsClass) {
inst.innerHTML = `<i class="${iconClass}"></i>`;
} else {
inst.innerHTML = `<i class="${iconClass}">${iconName}</i>`;
}
if (inst.stroke && iconSet.opticalFont) {
inst.style.setProperty("--weight", inst.stroke * 100);
}
return; // Return early
}
getIconSvg(iconName, iconSet, iconType)
.then((data) => {
let iconData = data;
// Strip class attribute as it may be affected by css
if (iconData.includes("class=")) {
iconData = iconData.replace(/ class="([a-z- ]*)"/g, "");
}
// Add and/or fix stroke
if (inst.stroke || iconSet.useStroke) {
iconData = iconData.replace(
/stroke-width="([0-9\.]*)"/g,
`stroke-width="${inst.stroke}"`,
);
}
// Fix fill to currentColor
if (iconSet.fixFill) {
iconData = iconData.replace(/(<svg.*?)>/, '$1 fill="currentColor">');
}
// If we have some html, pass it along (useful for svg anim)
if (inst.defaultHTML) {
iconData = iconData.replace("</svg>", `${inst.defaultHTML}</svg>`);
}
inst.innerHTML = iconData;
})
.catch((error) => {
inst.innerHTML = "<span>⚠️</span>";
console.error(`Failed to load icon ${iconName} (error ${error})`);
});
}
/**
* @param {HTMLElement} element
* @returns {Boolean}
*/
function isInViewport(element) {
const rect = element.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <=
(window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
/**
* Performs a deep merge of objects and returns new object. Does not modify
* objects (immutable) and merges arrays via concatenation.
*
* @param {...object} objects - Objects to merge
* @returns {object} New object with merged key/values
*/
function mergeDeep(...objects) {
const isObject = (obj) => obj && typeof obj === "object";
const isArray = Array.isArray;
return objects.reduce((prev, obj) => {
for (const key of Object.keys(obj)) {
const pVal = prev[key];
const oVal = obj[key];
if (isArray(pVal) && isArray(oVal)) {
prev[key] = pVal.concat(...oVal);
} else if (isObject(pVal) && isObject(oVal)) {
prev[key] = mergeDeep(pVal, oVal);
} else {
prev[key] = oVal;
}
}
return prev;
}, {});
}
const aliases = {};
function processIconSets() {
for (const [key, set] of Object.entries(options.sets)) {
// List aliases for easy retrieval
aliases[set.alias] = key;
// Include full name in iconset definition
set.name = key;
}
}
processIconSets();
class LastIcon extends HTMLElement {
/**
* @param {object} opts
* @returns {Options} The updated option object
*/
static configure(opts = {}) {
for (const k in opts) {
if (typeof options[k] === "undefined") {
console.error(`Invalid option key ${k}`);
return;
}
if (Array.isArray(opts[k])) {
options[k] = options[k].concat(opts[k]);
} else if (typeof opts[k] === "object") {
options[k] = mergeDeep(options[k], opts[k]);
} else {
options[k] = opts[k];
}
}
processIconSets();
// Log after we had the opportunity to change debug flag
log("configuring options");
return options;
}
/**
* @return {String|null}
*/
get type() {
return this.getAttribute("type") || null;
}
/**
* @return {String}
*/
get set() {
return aliases[this.getAttribute("set")] || options.defaultSet;
}
/**
* @return {IconSet}
*/
get iconSet() {
return options.sets[this.set];
}
/**
* @return {Number}
*/
get stroke() {
return this.getAttribute("stroke") || options.defaultStroke;
}
static get observedAttributes() {
return ["name", "stroke", "size", "set", "type"];
}
connectedCallback() {
// innerHTML is not available because not parsed yet
// setTimeout also allows whenDefined to kick in before init
setTimeout(() => {
if (options.lazy && !isInViewport(this)) {
// observer will call init when element is visible
observer.observe(this);
} else {
// init directly
this.init();
}
});
}
init() {
// Store default content as we will inject it back later
this.defaultHTML = this.innerHTML;
this.loadIcon();
}
loadIcon() {
const name = this.getAttribute("name");
if (!name) {
return;
}
const iconSet = this.iconSet;
// Clear icon
this.innerHTML = "";
// Useful for customizing size in css
const size = this.getAttribute("size");
if (size) {
this.setSize(size);
}
refreshIcon(this, name, iconSet, this.type);
}
setSize(size) {
this.style.setProperty("--size", `${size}px`);
if (this.iconSet.opticalFont) {
this.style.setProperty("--opsz", size);
}
}
attributeChangedCallback(attr, oldVal, newVal) {
// Wait until properly loaded for the first time
if (typeof this.defaultHTML !== "string") {
return;
}
log(`Attr ${attr} changed from ${oldVal} to ${newVal}`);
if (attr === "size") {
this.setSize(newVal);
} else if (newVal) {
log("attribute changed");
this.loadIcon();
}
}
}
customElements.define("l-i", LastIcon);