-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg-icon.js
422 lines (375 loc) · 12 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
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
417
418
419
420
421
422
/*! Copyright (c) 2014 - 2023 Ayogo Health Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
/** The XML namespace needed for SVG elements. */
const svgns = "http://www.w3.org/2000/svg";
/** Styles to be applied within the Shadow DOM of the svg-icon element. */
const svgicon_styles = `
:host {
display: inline-block;
height: 1.5em;
width: 1.5em;
margin-bottom: -0.25em;
}
svg {
height: inherit;
width: inherit;
flex-shrink: inherit;
display: inline-block;
fill: currentColor;
vertical-align: inherit;
}
@supports (display: contents) {
:host {
display:contents;
}
svg {
margin: inherit;
}
}
`;
/**
* @type {CSSStyleSheet}
*/
let stylesheet = null;
try {
stylesheet = new CSSStyleSheet();
stylesheet.replaceSync(svgicon_styles);
} catch(e) { }
/**
* `svg-icon` Web Component
* ========================
*
* This is a web component for displaying icons from an SVG symbols atlas file.
*
* It's easy to use! Just provide the name of the icon and the SVG file
* containing it:
*
* ```
* <svg-icon name="arrow-left" src="assets/icons.svg"></svg-icon>
* ```
*
* Advanced Usage
* --------------
*
* ### Inlining Icons
*
* By default, icons will be referenced into the document using an SVG `use`
* tag with `xlink:href`. However, this isn't supported in some browsers and
* some runtime configurations. In those cases, the contents of the SVG icon
* will be inlined into the page.
*
* For testing and special cases, this behaviour can be forced by adding an
* `inline` attribute to the svg-icon:
*
* ```
* <svg-icon src="close-x" src="assets/icons.svg" inline></svg-icon>
* ```
*
* ### WKWebView Hacks
*
* When using SVG icons in a WKWebView from file:/// URLs, there are
* restrictions that prevent loading them using `xlink:href`. Unfortunately,
* there are also restrictions that prevent loading the SVG symbol file using
* fetch.
*
* As a workaround, on WKWebView in a file:/// origin, we attempt to load the
* SVG symbol file into an iframe and then wait for a postMessage call to
* serialize the XML and return it to the main page. This requires a special
* SVG to be included in the icon bundle that will generate the message:
*
* ```
* <svg viewBox="0 0 1 1">
* <script>//<![CDATA[
* if (window && window.parent && window.location.search.match(/postHack=/)) {
* setTimeout(function() {
* var filename = window.location.search.match(/postHack=([^&]+)/)[1];
* var message = 'SVGICON:' + filename + ':' + ':' + (new XMLSerializer()).serializeToString(document);
* window.parent.postMessage(message, '*');
* }, 0);
* }
* //]]></script>
* </svg>
* ```
*
* @element svg-icon
* @attr {string} src
* @attr {string} name
* @attr {boolean|"iframe"} [inline=false]
*/
export class SvgIconElement extends HTMLElement {
/**
* Global property to force all SVG icons to be inlined into the page rather
* than loading using `xlink:href`.
*
* This should be automatically set to true for browsers that are known to
* have incompatibilities with linked icons, but it is also available to be
* set globally for the application.
*
* Access this as `SvgIconElement.inlineAll`.
*/
static inlineAll = false;
/**
* Global property used as a prefix for all icon names.
*
* Typically when using a tool like [svgstore](https://npm.im/svgstore), a
* prefix is added to all the bundled icon symbols. This defaults to the
* string `icon-`, but can be overridden as needed. If no prefix is required,
* set this to an empty string.
*
* Access this as `SvgIconElement.prefix`.
*/
static prefix = "icon-";
/**
* Stores an internal map of SVG files that have been loaded for icon lookup.
*
* @type {Map<string,Promise<XMLDocument>>}
*/
static #fileMap = new Map();
/**
* The root SVG element for the component.
*
* @type {SVGSVGElement}
*/
#root = null;
/** @private */
static get observedAttributes() {
return ["name", "src", "inline"];
}
/**
* The name of the icon to display.
*/
get name() {
return this.getAttribute("name");
}
/**
* The source SVG atlas file that contains the icon to display.
*/
get src() {
return this.getAttribute("src");
}
/**
* Optional boolean attribute to force inlining of an icon instead of linking.
*/
get inline() {
return this.hasAttribute("inline");
}
/**
* The name of the icon to display.
*/
set name(value) {
if (value) {
this.setAttribute("name", value);
} else {
this.removeAttribute("name");
}
}
/**
* The source SVG atlas file that contains the icon to display.
*/
set src(value) {
if (value) {
this.setAttribute("src", value);
} else {
this.removeAttribute("src");
}
}
/**
* Callback that runs when the element is connected to the DOM.
*
* @private
*/
connectedCallback() {
/** @type {HTMLElement | ShadowRoot} */
let el = this;
// If there's no accessible label provided for the icon, let's assume it's
// purely decorative and hide it from AT
if (!el.hasAttribute("title") &&
!el.hasAttribute("role") &&
!el.hasAttribute("aria-label") &&
!el.hasAttribute("aria-labelledby")) {
el.setAttribute("aria-hidden", "true");
}
if (!this.#root) {
this.#root = document.createElementNS(svgns, "svg");
el = this.attachShadow({ mode: "open" });
if ("adoptedStyleSheets" in el && stylesheet !== null) {
el.adoptedStyleSheets = [stylesheet];
} else {
el.innerHTML = `<style>${svgicon_styles}</style>`;
}
el.appendChild(this.#root);
}
const iconname = this.getAttribute("name");
const iconsrc = this.getAttribute("src");
if (iconname && iconsrc && !iconname.match(/\{/)) {
this.#setIcon(iconname, iconsrc);
}
}
/**
* Callback that runs when the element's observed attributes change.
*
* @private
*/
attributeChangedCallback() {
const iconname = this.getAttribute("name");
const iconsrc = this.getAttribute("src");
if (this.#root && iconname && iconsrc) {
this.#setIcon(iconname, iconsrc);
}
}
/**
* Actually sets up the SVG icon to be displayed, when the component
* initializes or attributes are changed.
*
* @param {string} name The unprefixed name of the icon to reference from the
* atlas.
* @param {string} src The URL of the SVG atlas file.
*/
#setIcon(name, src) {
const range = document.createRange();
range.selectNodeContents(this.#root);
range.deleteContents();
if (src && (SvgIconElement.inlineAll || this.hasAttribute("inline"))) {
this.#setIconWithInlining(name, src);
} else {
this.#setIconWithReference(name, src);
}
}
/**
* Creates an SVG `use` element and adds it as a child.
*
* @param {string} name The unprefixed name of the icon to reference from the
* atlas.
* @param {string} src The URL of the SVG atlas file.
*/
#setIconWithReference(name, src) {
const icon = `${src || ""}#${SvgIconElement.prefix}${name}`;
const use = document.createElementNS(svgns, "use");
use.setAttributeNS("http://www.w3.org/1999/xlink", "href", icon);
this.#root.appendChild(use);
}
/**
* Retrieves the SVG atlas file, extracts the requested icon, and inlines it
* as a child.
*
* @param {string} name The unprefixed name of the icon to reference from the
* atlas.
* @param {string} src The URL of the SVG atlas file.
*/
#setIconWithInlining(name, src) {
if (!SvgIconElement.#fileMap.has(src)) {
// WKWebView can't deal with file URLs that use external files in
// xlink :(
// Also allow overriding with an attribute, for testing
if (
(document.location.protocol.match(/file:/) && "webkit" in window) ||
this.getAttribute("inline") === "iframe"
) {
this.#inlineWithIFrame(src);
} else {
this.#inlineWithXHR(src);
}
}
SvgIconElement.#fileMap.get(src).then((svgdoc) => {
const symbol = svgdoc.getElementById(`${SvgIconElement.prefix}${name}`);
if (symbol) {
if (symbol.hasAttribute("viewBox")) {
this.#root.setAttribute("viewBox", symbol.getAttribute("viewBox"));
}
const nodes = symbol.childNodes;
for (let i = 0; i < nodes.length; i++) {
this.#root.appendChild(nodes[i].cloneNode(true));
}
}
});
}
/**
* Creates a hidden iframe and loads the SVG atlas file inside. If the SVG
* atlas file uses postMessage to broadcast its contents, the parsed result
* is stored in the internal file map.
*
* @param {string} filename The URI of the SVG atlas file.
*/
#inlineWithIFrame(filename) {
const query = `postHack=${encodeURIComponent(filename)}`;
const ifr = document.createElement("iframe");
ifr.src = `${filename}${filename.indexOf("?") === -1 ? "?" : "&"}${query}`;
ifr.style.position = "absolute";
ifr.style.overflow = "hidden";
ifr.style.clip = "rect(1px, 1px, 1px, 1px)";
ifr.style.height = "0";
ifr.style.width = "0";
this.parentNode.insertBefore(ifr, this);
SvgIconElement.#fileMap.set(
filename,
new Promise((resolve) => {
const handler = (/** @type {MessageEvent} */ evt) => {
const matches = evt.data.match(/^SVGICON:([^:]+)::/);
if (matches) {
const name = decodeURIComponent(matches[1]);
if (name === filename) {
const response = evt.data.split("::")[1];
const parser = new DOMParser();
resolve(parser.parseFromString(response, "image/svg+xml"));
ifr.parentNode.removeChild(ifr);
window.removeEventListener("message", handler);
}
}
};
window.addEventListener("message", handler);
})
);
}
/**
* Makes an XHR request to the SVG atlas file, and stores the parsed result
* in the internal file map.
*
* @param {string} filename The URI of the SVG atlas file.
*/
#inlineWithXHR(filename) {
SvgIconElement.#fileMap.set(
filename,
new Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.open("GET", filename, true);
xhr.onload = () => {
const parser = new DOMParser();
resolve(parser.parseFromString(xhr.responseText, "image/svg+xml"));
};
xhr.send();
})
);
}
}
// inlineAll is true for all versions of IE (not including Edge)
if (!!navigator.userAgent.match(/Trident\//)) {
SvgIconElement.inlineAll = true;
}
// WKWebView can't deal with file URLs that use external files in xlink :(
if (document.location.protocol.match(/file:/) && ('webkit' in window)) {
SvgIconElement.inlineAll = true;
}
// Safari <= 12 seem to have issues with external SVG icon links too :(
if (navigator.userAgent.match(/Version\/[0-9\.]+( Mobile\/.+)? Safari\//) &&
parseInt(navigator.userAgent.match(/Version\/([0-9\.]+)/)[1], 10) < 13) {
SvgIconElement.inlineAll = true;
}
customElements.define("svg-icon", SvgIconElement);