@telperion/elements 1.1.2 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.build-manifest.json +1 -1
- package/README.md +2 -0
- package/icon/icon-element.cjs +78 -7
- package/icon/icon-element.cjs.map +1 -1
- package/icon/icon-element.js +78 -7
- package/icon/icon-element.js.map +1 -1
- package/icon/index.cjs +6 -0
- package/icon/index.cjs.map +1 -1
- package/icon/index.d.cts +57 -8
- package/icon/index.d.ts +57 -8
- package/icon/index.js +1 -0
- package/icon/index.js.map +1 -1
- package/icon/thesvg-sprite.cjs +298 -0
- package/icon/thesvg-sprite.cjs.map +1 -0
- package/icon/thesvg-sprite.js +292 -0
- package/icon/thesvg-sprite.js.map +1 -0
- package/iife/icon/index.js +7 -7
- package/iife/icon/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Global sprite + loader for the theSVG family of `<tp-icon>`.
|
|
5
|
+
*
|
|
6
|
+
* On first sight of a `(slug, variant)` pair we fetch
|
|
7
|
+
* `${baseUrl}/${slug}/${variant}.svg` (defaults to the GLINCKER theSVG
|
|
8
|
+
* jsDelivr mirror), clone the entire SVG body into a fresh
|
|
9
|
+
* `<symbol id="tp-thesvg-${slug}-${variant}">`, namespace any internal `id`
|
|
10
|
+
* attributes (and the `url(#…)` / `href="#…"` references that point at them)
|
|
11
|
+
* so multiple icons can coexist in the shared sprite without colliding, and
|
|
12
|
+
* append the symbol to a single `<svg data-tp-thesvg>` at the bottom of
|
|
13
|
+
* `document.body`.
|
|
14
|
+
*
|
|
15
|
+
* Subsequent `<tp-icon family="thesvg" slug="…" variant="…">` instances
|
|
16
|
+
* reference the same symbol via `<use href="#tp-thesvg-${slug}-${variant}"/>`.
|
|
17
|
+
*/
|
|
18
|
+
const SLUG_PATTERN = /^[a-z0-9.-]+$/;
|
|
19
|
+
const VARIANT_PATTERN = /^[a-z0-9-]+$/;
|
|
20
|
+
const SPRITE_MARKER = "data-tp-thesvg";
|
|
21
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
22
|
+
const XLINK_NS = "http://www.w3.org/1999/xlink";
|
|
23
|
+
let baseUrl = "https://cdn.jsdelivr.net/gh/glincker/thesvg@main/public/icons";
|
|
24
|
+
let sprite = null;
|
|
25
|
+
const inFlight = new Map();
|
|
26
|
+
/**
|
|
27
|
+
* The DOM id used for the `<symbol>` corresponding to a given theSVG
|
|
28
|
+
* `(slug, variant)` pair. Exposed so `<tp-icon>` and tests can build matching
|
|
29
|
+
* `<use href>` references.
|
|
30
|
+
*/
|
|
31
|
+
function getTheSvgSymbolId(slug, variant = "default") {
|
|
32
|
+
return `tp-thesvg-${slug}-${variant}`;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Override the CDN base URL used by the loader. Useful for self-hosting,
|
|
36
|
+
* offline development, or pointing at the canonical site
|
|
37
|
+
* (`https://thesvg.org/icons`). Trailing slashes are stripped.
|
|
38
|
+
*/
|
|
39
|
+
function setTheSvgBaseUrl(url) {
|
|
40
|
+
baseUrl = url.replace(/\/+$/, "");
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Drop every cached symbol and remove the sprite from the DOM. Intended for
|
|
44
|
+
* test isolation; production code rarely needs to call this.
|
|
45
|
+
*/
|
|
46
|
+
function clearTheSvgCache() {
|
|
47
|
+
inFlight.clear();
|
|
48
|
+
if (sprite && sprite.parentNode) {
|
|
49
|
+
sprite.parentNode.removeChild(sprite);
|
|
50
|
+
}
|
|
51
|
+
sprite = null;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Inspect which variants of `slug` are currently materialised in the sprite.
|
|
55
|
+
* Reads from the DOM only — does not hit the network and does not list
|
|
56
|
+
* variants that exist on the CDN but haven't been requested yet.
|
|
57
|
+
*/
|
|
58
|
+
function listLoadedTheSvgVariants(slug) {
|
|
59
|
+
if (!SLUG_PATTERN.test(slug))
|
|
60
|
+
return [];
|
|
61
|
+
if (!sprite)
|
|
62
|
+
return [];
|
|
63
|
+
const prefix = `tp-thesvg-${slug}-`;
|
|
64
|
+
const out = [];
|
|
65
|
+
for (const symbol of Array.from(sprite.querySelectorAll("symbol"))) {
|
|
66
|
+
const id = symbol.getAttribute("id");
|
|
67
|
+
if (id && id.startsWith(prefix))
|
|
68
|
+
out.push(id.slice(prefix.length));
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Ensure a `<symbol id="tp-thesvg-${slug}-${variant}">` exists in the global
|
|
74
|
+
* sprite. Returns a promise that resolves once the symbol is in the DOM (or
|
|
75
|
+
* immediately if it was already loaded). Safe to call repeatedly with the
|
|
76
|
+
* same `(slug, variant)` pair.
|
|
77
|
+
*
|
|
78
|
+
* Throws synchronously when either argument fails its whitelist
|
|
79
|
+
* (`slug`: `/^[a-z0-9.-]+$/`, `variant`: `/^[a-z0-9-]+$/`).
|
|
80
|
+
*/
|
|
81
|
+
function loadTheSvgIcon(slug, variant = "default") {
|
|
82
|
+
if (!SLUG_PATTERN.test(slug)) {
|
|
83
|
+
throw new Error(`[tp-icon] Invalid theSVG slug: ${JSON.stringify(slug)}`);
|
|
84
|
+
}
|
|
85
|
+
if (!VARIANT_PATTERN.test(variant)) {
|
|
86
|
+
throw new Error(`[tp-icon] Invalid theSVG variant: ${JSON.stringify(variant)}`);
|
|
87
|
+
}
|
|
88
|
+
const key = `${slug}/${variant}`;
|
|
89
|
+
const cached = inFlight.get(key);
|
|
90
|
+
if (cached)
|
|
91
|
+
return cached;
|
|
92
|
+
const promise = (async () => {
|
|
93
|
+
const res = await fetch(`${baseUrl}/${slug}/${variant}.svg`);
|
|
94
|
+
if (!res.ok) {
|
|
95
|
+
throw new Error(`[tp-icon] Failed to load theSVG "${slug}/${variant}": ${res.status} ${res.statusText}`);
|
|
96
|
+
}
|
|
97
|
+
const text = await res.text();
|
|
98
|
+
const doc = new DOMParser().parseFromString(text, "image/svg+xml");
|
|
99
|
+
if (doc.querySelector("parsererror")) {
|
|
100
|
+
throw new Error(`[tp-icon] Failed to parse SVG for "${slug}/${variant}"`);
|
|
101
|
+
}
|
|
102
|
+
const root = doc.documentElement;
|
|
103
|
+
if (!root || root.nodeName.toLowerCase() !== "svg") {
|
|
104
|
+
throw new Error(`[tp-icon] No <svg> root in response for "${slug}/${variant}"`);
|
|
105
|
+
}
|
|
106
|
+
const viewBox = resolveViewBox(root);
|
|
107
|
+
const symbolId = getTheSvgSymbolId(slug, variant);
|
|
108
|
+
const target = ensureSprite();
|
|
109
|
+
const symbol = document.createElementNS(SVG_NS, "symbol");
|
|
110
|
+
symbol.setAttribute("id", symbolId);
|
|
111
|
+
symbol.setAttribute("viewBox", viewBox);
|
|
112
|
+
// Forward presentation attributes from the source <svg> root onto the
|
|
113
|
+
// <symbol> so children that rely on inheritance (e.g. theSVG icons that
|
|
114
|
+
// declare `fill="#4285F4"` on the root and leave `<path>` elements
|
|
115
|
+
// unpainted) keep their colors when rendered via <use>.
|
|
116
|
+
forwardPresentationAttrs(root, symbol);
|
|
117
|
+
// Import children first so we own the cloned nodes, then namespace ids
|
|
118
|
+
// and rewrite intra-document references to those ids.
|
|
119
|
+
for (const child of Array.from(root.childNodes)) {
|
|
120
|
+
symbol.appendChild(document.importNode(child, true));
|
|
121
|
+
}
|
|
122
|
+
namespaceIds(symbol, symbolId);
|
|
123
|
+
target.appendChild(symbol);
|
|
124
|
+
return { slug, variant, viewBox };
|
|
125
|
+
})();
|
|
126
|
+
// On failure, evict so a later retry can succeed.
|
|
127
|
+
promise.catch(() => {
|
|
128
|
+
if (inFlight.get(key) === promise)
|
|
129
|
+
inFlight.delete(key);
|
|
130
|
+
});
|
|
131
|
+
inFlight.set(key, promise);
|
|
132
|
+
return promise;
|
|
133
|
+
}
|
|
134
|
+
function ensureSprite() {
|
|
135
|
+
if (sprite && sprite.isConnected)
|
|
136
|
+
return sprite;
|
|
137
|
+
// Survive HMR / multiple module instances: reuse an existing tagged sprite.
|
|
138
|
+
const existing = document.querySelector(`svg[${SPRITE_MARKER}]`);
|
|
139
|
+
if (existing) {
|
|
140
|
+
sprite = existing;
|
|
141
|
+
return sprite;
|
|
142
|
+
}
|
|
143
|
+
const svg = document.createElementNS(SVG_NS, "svg");
|
|
144
|
+
svg.setAttribute(SPRITE_MARKER, "");
|
|
145
|
+
svg.setAttribute("aria-hidden", "true");
|
|
146
|
+
svg.setAttribute("xmlns", SVG_NS);
|
|
147
|
+
svg.style.position = "absolute";
|
|
148
|
+
svg.style.width = "0";
|
|
149
|
+
svg.style.height = "0";
|
|
150
|
+
svg.style.overflow = "hidden";
|
|
151
|
+
document.body.appendChild(svg);
|
|
152
|
+
sprite = svg;
|
|
153
|
+
return svg;
|
|
154
|
+
}
|
|
155
|
+
function escapeRegExp(value) {
|
|
156
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Pick the most accurate `viewBox` for the source `<svg>` root.
|
|
160
|
+
*
|
|
161
|
+
* 1. Use the explicit `viewBox` attribute if present.
|
|
162
|
+
* 2. Otherwise, synthesise one from the root's `width` / `height` (some
|
|
163
|
+
* theSVG icons — e.g. `amazon` — ship `width="603" height="182"`
|
|
164
|
+
* with no `viewBox`).
|
|
165
|
+
* 3. Fall back to `0 0 24 24` only as a last resort.
|
|
166
|
+
*
|
|
167
|
+
* Width/height values may carry units (`px`, `pt`, etc.); we strip those
|
|
168
|
+
* since `viewBox` is unitless.
|
|
169
|
+
*/
|
|
170
|
+
function resolveViewBox(root) {
|
|
171
|
+
const explicit = root.getAttribute("viewBox");
|
|
172
|
+
if (explicit)
|
|
173
|
+
return explicit;
|
|
174
|
+
const width = parseDimension(root.getAttribute("width"));
|
|
175
|
+
const height = parseDimension(root.getAttribute("height"));
|
|
176
|
+
if (width !== null && height !== null)
|
|
177
|
+
return `0 0 ${width} ${height}`;
|
|
178
|
+
return "0 0 24 24";
|
|
179
|
+
}
|
|
180
|
+
function parseDimension(value) {
|
|
181
|
+
if (value === null)
|
|
182
|
+
return null;
|
|
183
|
+
const match = /^([\d.]+)/.exec(value.trim());
|
|
184
|
+
if (!match)
|
|
185
|
+
return null;
|
|
186
|
+
const num = Number(match[1]);
|
|
187
|
+
return Number.isFinite(num) && num > 0 ? num : null;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* SVG presentation attributes that cascade to children. We forward whatever
|
|
191
|
+
* subset is set on the source `<svg>` root onto the `<symbol>` so any
|
|
192
|
+
* unstyled descendant element keeps its inherited paint / stroke / opacity
|
|
193
|
+
* when the icon is rendered via `<use>`.
|
|
194
|
+
*/
|
|
195
|
+
const PRESENTATION_ATTRS = [
|
|
196
|
+
"fill",
|
|
197
|
+
"fill-opacity",
|
|
198
|
+
"fill-rule",
|
|
199
|
+
"stroke",
|
|
200
|
+
"stroke-opacity",
|
|
201
|
+
"stroke-width",
|
|
202
|
+
"stroke-linecap",
|
|
203
|
+
"stroke-linejoin",
|
|
204
|
+
"stroke-miterlimit",
|
|
205
|
+
"stroke-dasharray",
|
|
206
|
+
"stroke-dashoffset",
|
|
207
|
+
"color",
|
|
208
|
+
"opacity",
|
|
209
|
+
];
|
|
210
|
+
function forwardPresentationAttrs(source, target) {
|
|
211
|
+
for (const name of PRESENTATION_ATTRS) {
|
|
212
|
+
const value = source.getAttribute(name);
|
|
213
|
+
if (value !== null)
|
|
214
|
+
target.setAttribute(name, value);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Walk `root` twice: first collect every `id` attribute and rename it to
|
|
219
|
+
* `${prefix}-${id}`, then rewrite every reference to those ids. References
|
|
220
|
+
* appear in two shapes:
|
|
221
|
+
* - URL functional notation: `url(#a)` / `url("#a")` / `url('#a')` inside
|
|
222
|
+
* any attribute value (commonly `fill`, `stroke`, `filter`, `mask`,
|
|
223
|
+
* `clip-path`).
|
|
224
|
+
* - Hash references: `href="#a"` and the legacy `xlink:href="#a"`.
|
|
225
|
+
*/
|
|
226
|
+
function namespaceIds(root, prefix) {
|
|
227
|
+
const renames = new Map();
|
|
228
|
+
// Skip `root` itself; only namespace ids on its descendants. The symbol
|
|
229
|
+
// element keeps its outer id (`prefix`).
|
|
230
|
+
for (const child of Array.from(root.children)) {
|
|
231
|
+
walk(child, (el) => {
|
|
232
|
+
const id = el.getAttribute("id");
|
|
233
|
+
if (id) {
|
|
234
|
+
const next = `${prefix}-${id}`;
|
|
235
|
+
el.setAttribute("id", next);
|
|
236
|
+
renames.set(id, next);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
if (renames.size === 0)
|
|
241
|
+
return;
|
|
242
|
+
// Build one regex per old id to rewrite `url(#oldId)` references inside any
|
|
243
|
+
// attribute value (handles bare/quoted forms).
|
|
244
|
+
const urlPatterns = [];
|
|
245
|
+
for (const [oldId, newId] of renames) {
|
|
246
|
+
urlPatterns.push({
|
|
247
|
+
regex: new RegExp(`url\\((["']?)#${escapeRegExp(oldId)}\\1\\)`, "g"),
|
|
248
|
+
replacement: `url($1#${newId}$1)`,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
for (const child of Array.from(root.children)) {
|
|
252
|
+
walk(child, (el) => {
|
|
253
|
+
for (const attr of Array.from(el.attributes)) {
|
|
254
|
+
// Skip the `id` attribute itself; we already renamed it above.
|
|
255
|
+
if (attr.name === "id")
|
|
256
|
+
continue;
|
|
257
|
+
let value = attr.value;
|
|
258
|
+
let changed = false;
|
|
259
|
+
// url(#…) references in any attribute (fill, stroke, filter, …).
|
|
260
|
+
if (value.includes("url(")) {
|
|
261
|
+
for (const { regex, replacement } of urlPatterns) {
|
|
262
|
+
const next = value.replace(regex, replacement);
|
|
263
|
+
if (next !== value) {
|
|
264
|
+
value = next;
|
|
265
|
+
changed = true;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
// Hash references: href / xlink:href.
|
|
270
|
+
const isHrefAttr = attr.name === "href" ||
|
|
271
|
+
attr.name === "xlink:href" ||
|
|
272
|
+
(attr.namespaceURI === XLINK_NS && attr.localName === "href");
|
|
273
|
+
if (isHrefAttr && value.startsWith("#")) {
|
|
274
|
+
const target = value.slice(1);
|
|
275
|
+
const renamed = renames.get(target);
|
|
276
|
+
if (renamed) {
|
|
277
|
+
value = `#${renamed}`;
|
|
278
|
+
changed = true;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (changed)
|
|
282
|
+
attr.value = value;
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function walk(root, visit) {
|
|
288
|
+
visit(root);
|
|
289
|
+
for (const child of Array.from(root.children))
|
|
290
|
+
walk(child, visit);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
exports.clearTheSvgCache = clearTheSvgCache;
|
|
294
|
+
exports.getTheSvgSymbolId = getTheSvgSymbolId;
|
|
295
|
+
exports.listLoadedTheSvgVariants = listLoadedTheSvgVariants;
|
|
296
|
+
exports.loadTheSvgIcon = loadTheSvgIcon;
|
|
297
|
+
exports.setTheSvgBaseUrl = setTheSvgBaseUrl;
|
|
298
|
+
//# sourceMappingURL=thesvg-sprite.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thesvg-sprite.cjs","sources":["../../../../libs/elements/src/icon/thesvg-sprite.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;AAcG;AAEH,MAAM,YAAY,GAAG,eAAe;AACpC,MAAM,eAAe,GAAG,cAAc;AACtC,MAAM,aAAa,GAAG,gBAAgB;AACtC,MAAM,MAAM,GAAG,4BAA4B;AAC3C,MAAM,QAAQ,GAAG,8BAA8B;AAE/C,IAAI,OAAO,GAAG,+DAA+D;AAC7E,IAAI,MAAM,GAAyB,IAAI;AACvC,MAAM,QAA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AI,CAAC,KAAK,EAAE,KAAK,CAAC;AACnE;;;;;;;;"}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Global sprite + loader for the theSVG family of `<tp-icon>`.
|
|
3
|
+
*
|
|
4
|
+
* On first sight of a `(slug, variant)` pair we fetch
|
|
5
|
+
* `${baseUrl}/${slug}/${variant}.svg` (defaults to the GLINCKER theSVG
|
|
6
|
+
* jsDelivr mirror), clone the entire SVG body into a fresh
|
|
7
|
+
* `<symbol id="tp-thesvg-${slug}-${variant}">`, namespace any internal `id`
|
|
8
|
+
* attributes (and the `url(#…)` / `href="#…"` references that point at them)
|
|
9
|
+
* so multiple icons can coexist in the shared sprite without colliding, and
|
|
10
|
+
* append the symbol to a single `<svg data-tp-thesvg>` at the bottom of
|
|
11
|
+
* `document.body`.
|
|
12
|
+
*
|
|
13
|
+
* Subsequent `<tp-icon family="thesvg" slug="…" variant="…">` instances
|
|
14
|
+
* reference the same symbol via `<use href="#tp-thesvg-${slug}-${variant}"/>`.
|
|
15
|
+
*/
|
|
16
|
+
const SLUG_PATTERN = /^[a-z0-9.-]+$/;
|
|
17
|
+
const VARIANT_PATTERN = /^[a-z0-9-]+$/;
|
|
18
|
+
const SPRITE_MARKER = "data-tp-thesvg";
|
|
19
|
+
const SVG_NS = "http://www.w3.org/2000/svg";
|
|
20
|
+
const XLINK_NS = "http://www.w3.org/1999/xlink";
|
|
21
|
+
let baseUrl = "https://cdn.jsdelivr.net/gh/glincker/thesvg@main/public/icons";
|
|
22
|
+
let sprite = null;
|
|
23
|
+
const inFlight = new Map();
|
|
24
|
+
/**
|
|
25
|
+
* The DOM id used for the `<symbol>` corresponding to a given theSVG
|
|
26
|
+
* `(slug, variant)` pair. Exposed so `<tp-icon>` and tests can build matching
|
|
27
|
+
* `<use href>` references.
|
|
28
|
+
*/
|
|
29
|
+
function getTheSvgSymbolId(slug, variant = "default") {
|
|
30
|
+
return `tp-thesvg-${slug}-${variant}`;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Override the CDN base URL used by the loader. Useful for self-hosting,
|
|
34
|
+
* offline development, or pointing at the canonical site
|
|
35
|
+
* (`https://thesvg.org/icons`). Trailing slashes are stripped.
|
|
36
|
+
*/
|
|
37
|
+
function setTheSvgBaseUrl(url) {
|
|
38
|
+
baseUrl = url.replace(/\/+$/, "");
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Drop every cached symbol and remove the sprite from the DOM. Intended for
|
|
42
|
+
* test isolation; production code rarely needs to call this.
|
|
43
|
+
*/
|
|
44
|
+
function clearTheSvgCache() {
|
|
45
|
+
inFlight.clear();
|
|
46
|
+
if (sprite && sprite.parentNode) {
|
|
47
|
+
sprite.parentNode.removeChild(sprite);
|
|
48
|
+
}
|
|
49
|
+
sprite = null;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Inspect which variants of `slug` are currently materialised in the sprite.
|
|
53
|
+
* Reads from the DOM only — does not hit the network and does not list
|
|
54
|
+
* variants that exist on the CDN but haven't been requested yet.
|
|
55
|
+
*/
|
|
56
|
+
function listLoadedTheSvgVariants(slug) {
|
|
57
|
+
if (!SLUG_PATTERN.test(slug))
|
|
58
|
+
return [];
|
|
59
|
+
if (!sprite)
|
|
60
|
+
return [];
|
|
61
|
+
const prefix = `tp-thesvg-${slug}-`;
|
|
62
|
+
const out = [];
|
|
63
|
+
for (const symbol of Array.from(sprite.querySelectorAll("symbol"))) {
|
|
64
|
+
const id = symbol.getAttribute("id");
|
|
65
|
+
if (id && id.startsWith(prefix))
|
|
66
|
+
out.push(id.slice(prefix.length));
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Ensure a `<symbol id="tp-thesvg-${slug}-${variant}">` exists in the global
|
|
72
|
+
* sprite. Returns a promise that resolves once the symbol is in the DOM (or
|
|
73
|
+
* immediately if it was already loaded). Safe to call repeatedly with the
|
|
74
|
+
* same `(slug, variant)` pair.
|
|
75
|
+
*
|
|
76
|
+
* Throws synchronously when either argument fails its whitelist
|
|
77
|
+
* (`slug`: `/^[a-z0-9.-]+$/`, `variant`: `/^[a-z0-9-]+$/`).
|
|
78
|
+
*/
|
|
79
|
+
function loadTheSvgIcon(slug, variant = "default") {
|
|
80
|
+
if (!SLUG_PATTERN.test(slug)) {
|
|
81
|
+
throw new Error(`[tp-icon] Invalid theSVG slug: ${JSON.stringify(slug)}`);
|
|
82
|
+
}
|
|
83
|
+
if (!VARIANT_PATTERN.test(variant)) {
|
|
84
|
+
throw new Error(`[tp-icon] Invalid theSVG variant: ${JSON.stringify(variant)}`);
|
|
85
|
+
}
|
|
86
|
+
const key = `${slug}/${variant}`;
|
|
87
|
+
const cached = inFlight.get(key);
|
|
88
|
+
if (cached)
|
|
89
|
+
return cached;
|
|
90
|
+
const promise = (async () => {
|
|
91
|
+
const res = await fetch(`${baseUrl}/${slug}/${variant}.svg`);
|
|
92
|
+
if (!res.ok) {
|
|
93
|
+
throw new Error(`[tp-icon] Failed to load theSVG "${slug}/${variant}": ${res.status} ${res.statusText}`);
|
|
94
|
+
}
|
|
95
|
+
const text = await res.text();
|
|
96
|
+
const doc = new DOMParser().parseFromString(text, "image/svg+xml");
|
|
97
|
+
if (doc.querySelector("parsererror")) {
|
|
98
|
+
throw new Error(`[tp-icon] Failed to parse SVG for "${slug}/${variant}"`);
|
|
99
|
+
}
|
|
100
|
+
const root = doc.documentElement;
|
|
101
|
+
if (!root || root.nodeName.toLowerCase() !== "svg") {
|
|
102
|
+
throw new Error(`[tp-icon] No <svg> root in response for "${slug}/${variant}"`);
|
|
103
|
+
}
|
|
104
|
+
const viewBox = resolveViewBox(root);
|
|
105
|
+
const symbolId = getTheSvgSymbolId(slug, variant);
|
|
106
|
+
const target = ensureSprite();
|
|
107
|
+
const symbol = document.createElementNS(SVG_NS, "symbol");
|
|
108
|
+
symbol.setAttribute("id", symbolId);
|
|
109
|
+
symbol.setAttribute("viewBox", viewBox);
|
|
110
|
+
// Forward presentation attributes from the source <svg> root onto the
|
|
111
|
+
// <symbol> so children that rely on inheritance (e.g. theSVG icons that
|
|
112
|
+
// declare `fill="#4285F4"` on the root and leave `<path>` elements
|
|
113
|
+
// unpainted) keep their colors when rendered via <use>.
|
|
114
|
+
forwardPresentationAttrs(root, symbol);
|
|
115
|
+
// Import children first so we own the cloned nodes, then namespace ids
|
|
116
|
+
// and rewrite intra-document references to those ids.
|
|
117
|
+
for (const child of Array.from(root.childNodes)) {
|
|
118
|
+
symbol.appendChild(document.importNode(child, true));
|
|
119
|
+
}
|
|
120
|
+
namespaceIds(symbol, symbolId);
|
|
121
|
+
target.appendChild(symbol);
|
|
122
|
+
return { slug, variant, viewBox };
|
|
123
|
+
})();
|
|
124
|
+
// On failure, evict so a later retry can succeed.
|
|
125
|
+
promise.catch(() => {
|
|
126
|
+
if (inFlight.get(key) === promise)
|
|
127
|
+
inFlight.delete(key);
|
|
128
|
+
});
|
|
129
|
+
inFlight.set(key, promise);
|
|
130
|
+
return promise;
|
|
131
|
+
}
|
|
132
|
+
function ensureSprite() {
|
|
133
|
+
if (sprite && sprite.isConnected)
|
|
134
|
+
return sprite;
|
|
135
|
+
// Survive HMR / multiple module instances: reuse an existing tagged sprite.
|
|
136
|
+
const existing = document.querySelector(`svg[${SPRITE_MARKER}]`);
|
|
137
|
+
if (existing) {
|
|
138
|
+
sprite = existing;
|
|
139
|
+
return sprite;
|
|
140
|
+
}
|
|
141
|
+
const svg = document.createElementNS(SVG_NS, "svg");
|
|
142
|
+
svg.setAttribute(SPRITE_MARKER, "");
|
|
143
|
+
svg.setAttribute("aria-hidden", "true");
|
|
144
|
+
svg.setAttribute("xmlns", SVG_NS);
|
|
145
|
+
svg.style.position = "absolute";
|
|
146
|
+
svg.style.width = "0";
|
|
147
|
+
svg.style.height = "0";
|
|
148
|
+
svg.style.overflow = "hidden";
|
|
149
|
+
document.body.appendChild(svg);
|
|
150
|
+
sprite = svg;
|
|
151
|
+
return svg;
|
|
152
|
+
}
|
|
153
|
+
function escapeRegExp(value) {
|
|
154
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Pick the most accurate `viewBox` for the source `<svg>` root.
|
|
158
|
+
*
|
|
159
|
+
* 1. Use the explicit `viewBox` attribute if present.
|
|
160
|
+
* 2. Otherwise, synthesise one from the root's `width` / `height` (some
|
|
161
|
+
* theSVG icons — e.g. `amazon` — ship `width="603" height="182"`
|
|
162
|
+
* with no `viewBox`).
|
|
163
|
+
* 3. Fall back to `0 0 24 24` only as a last resort.
|
|
164
|
+
*
|
|
165
|
+
* Width/height values may carry units (`px`, `pt`, etc.); we strip those
|
|
166
|
+
* since `viewBox` is unitless.
|
|
167
|
+
*/
|
|
168
|
+
function resolveViewBox(root) {
|
|
169
|
+
const explicit = root.getAttribute("viewBox");
|
|
170
|
+
if (explicit)
|
|
171
|
+
return explicit;
|
|
172
|
+
const width = parseDimension(root.getAttribute("width"));
|
|
173
|
+
const height = parseDimension(root.getAttribute("height"));
|
|
174
|
+
if (width !== null && height !== null)
|
|
175
|
+
return `0 0 ${width} ${height}`;
|
|
176
|
+
return "0 0 24 24";
|
|
177
|
+
}
|
|
178
|
+
function parseDimension(value) {
|
|
179
|
+
if (value === null)
|
|
180
|
+
return null;
|
|
181
|
+
const match = /^([\d.]+)/.exec(value.trim());
|
|
182
|
+
if (!match)
|
|
183
|
+
return null;
|
|
184
|
+
const num = Number(match[1]);
|
|
185
|
+
return Number.isFinite(num) && num > 0 ? num : null;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* SVG presentation attributes that cascade to children. We forward whatever
|
|
189
|
+
* subset is set on the source `<svg>` root onto the `<symbol>` so any
|
|
190
|
+
* unstyled descendant element keeps its inherited paint / stroke / opacity
|
|
191
|
+
* when the icon is rendered via `<use>`.
|
|
192
|
+
*/
|
|
193
|
+
const PRESENTATION_ATTRS = [
|
|
194
|
+
"fill",
|
|
195
|
+
"fill-opacity",
|
|
196
|
+
"fill-rule",
|
|
197
|
+
"stroke",
|
|
198
|
+
"stroke-opacity",
|
|
199
|
+
"stroke-width",
|
|
200
|
+
"stroke-linecap",
|
|
201
|
+
"stroke-linejoin",
|
|
202
|
+
"stroke-miterlimit",
|
|
203
|
+
"stroke-dasharray",
|
|
204
|
+
"stroke-dashoffset",
|
|
205
|
+
"color",
|
|
206
|
+
"opacity",
|
|
207
|
+
];
|
|
208
|
+
function forwardPresentationAttrs(source, target) {
|
|
209
|
+
for (const name of PRESENTATION_ATTRS) {
|
|
210
|
+
const value = source.getAttribute(name);
|
|
211
|
+
if (value !== null)
|
|
212
|
+
target.setAttribute(name, value);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Walk `root` twice: first collect every `id` attribute and rename it to
|
|
217
|
+
* `${prefix}-${id}`, then rewrite every reference to those ids. References
|
|
218
|
+
* appear in two shapes:
|
|
219
|
+
* - URL functional notation: `url(#a)` / `url("#a")` / `url('#a')` inside
|
|
220
|
+
* any attribute value (commonly `fill`, `stroke`, `filter`, `mask`,
|
|
221
|
+
* `clip-path`).
|
|
222
|
+
* - Hash references: `href="#a"` and the legacy `xlink:href="#a"`.
|
|
223
|
+
*/
|
|
224
|
+
function namespaceIds(root, prefix) {
|
|
225
|
+
const renames = new Map();
|
|
226
|
+
// Skip `root` itself; only namespace ids on its descendants. The symbol
|
|
227
|
+
// element keeps its outer id (`prefix`).
|
|
228
|
+
for (const child of Array.from(root.children)) {
|
|
229
|
+
walk(child, (el) => {
|
|
230
|
+
const id = el.getAttribute("id");
|
|
231
|
+
if (id) {
|
|
232
|
+
const next = `${prefix}-${id}`;
|
|
233
|
+
el.setAttribute("id", next);
|
|
234
|
+
renames.set(id, next);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
if (renames.size === 0)
|
|
239
|
+
return;
|
|
240
|
+
// Build one regex per old id to rewrite `url(#oldId)` references inside any
|
|
241
|
+
// attribute value (handles bare/quoted forms).
|
|
242
|
+
const urlPatterns = [];
|
|
243
|
+
for (const [oldId, newId] of renames) {
|
|
244
|
+
urlPatterns.push({
|
|
245
|
+
regex: new RegExp(`url\\((["']?)#${escapeRegExp(oldId)}\\1\\)`, "g"),
|
|
246
|
+
replacement: `url($1#${newId}$1)`,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
for (const child of Array.from(root.children)) {
|
|
250
|
+
walk(child, (el) => {
|
|
251
|
+
for (const attr of Array.from(el.attributes)) {
|
|
252
|
+
// Skip the `id` attribute itself; we already renamed it above.
|
|
253
|
+
if (attr.name === "id")
|
|
254
|
+
continue;
|
|
255
|
+
let value = attr.value;
|
|
256
|
+
let changed = false;
|
|
257
|
+
// url(#…) references in any attribute (fill, stroke, filter, …).
|
|
258
|
+
if (value.includes("url(")) {
|
|
259
|
+
for (const { regex, replacement } of urlPatterns) {
|
|
260
|
+
const next = value.replace(regex, replacement);
|
|
261
|
+
if (next !== value) {
|
|
262
|
+
value = next;
|
|
263
|
+
changed = true;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
// Hash references: href / xlink:href.
|
|
268
|
+
const isHrefAttr = attr.name === "href" ||
|
|
269
|
+
attr.name === "xlink:href" ||
|
|
270
|
+
(attr.namespaceURI === XLINK_NS && attr.localName === "href");
|
|
271
|
+
if (isHrefAttr && value.startsWith("#")) {
|
|
272
|
+
const target = value.slice(1);
|
|
273
|
+
const renamed = renames.get(target);
|
|
274
|
+
if (renamed) {
|
|
275
|
+
value = `#${renamed}`;
|
|
276
|
+
changed = true;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (changed)
|
|
280
|
+
attr.value = value;
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
function walk(root, visit) {
|
|
286
|
+
visit(root);
|
|
287
|
+
for (const child of Array.from(root.children))
|
|
288
|
+
walk(child, visit);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export { clearTheSvgCache, getTheSvgSymbolId, listLoadedTheSvgVariants, loadTheSvgIcon, setTheSvgBaseUrl };
|
|
292
|
+
//# sourceMappingURL=thesvg-sprite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thesvg-sprite.js","sources":["../../../../libs/elements/src/icon/thesvg-sprite.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;AAcG;AAEH,MAAM,YAAY,GAAG,eAAe;AACpC,MAAM,eAAe,GAAG,cAAc;AACtC,MAAM,aAAa,GAAG,gBAAgB;AACtC,MAAM,MAAM,GAAG,4BAA4B;AAC3C,MAAM,QAAQ,GAAG,8BAA8B;AAE/C,IAAI,OAAO,GAAG,+DAA+D;AAC7E,IAAI,MAAM,GAAyB,IAAI;AACvC,MAAM,QAAQ,GAAG,IAAI,GAAG,EAA+B;AAWvD;;;;AAIG;SACa,iBAAiB,CAAC,IAAY,EAAE,UAAkB,SAAS,EAAA;AACzE,IAAA,OAAO,CAAA,UAAA,EAAa,IAAI,CAAA,CAAA,EAAI,OAAO,EAAE;AACvC;AAEA;;;;AAIG;AACG,SAAU,gBAAgB,CAAC,GAAW,EAAA;IAC1C,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;AACnC;AAEA;;;AAGG;SACa,gBAAgB,GAAA;IAC9B,QAAQ,CAAC,KAAK,EAAE;AAChB,IAAA,IAAI,MAAM,IAAI,MAAM,CAAC,UAAU,EAAE;AAC/B,QAAA,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,MAAM,CAAC;IACvC;IACA,MAAM,GAAG,IAAI;AACf;AAEA;;;;AAIG;AACG,SAAU,wBAAwB,CAAC,IAAY,EAAA;AACnD,IAAA,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC;AAAE,QAAA,OAAO,EAAE;AACvC,IAAA,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