@splendidlabz/svelte 4.12.1 → 4.13.0
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/components/SVG/SVG.svelte +18 -11
- package/components/SVG/parser.js +4 -4
- package/package.json +1 -1
|
@@ -8,17 +8,18 @@
|
|
|
8
8
|
* @component
|
|
9
9
|
* @example
|
|
10
10
|
* <!-- Renders /src/svg/logo.svg -->
|
|
11
|
-
* <SVG
|
|
11
|
+
* <SVG name="logo" class="w-6 h-6" />
|
|
12
12
|
* @example
|
|
13
13
|
* <!-- Use DOM parser when the SVG needs runtime DOM APIs -->
|
|
14
|
-
* <SVG
|
|
15
|
-
* @throws {Error} icon
|
|
14
|
+
* <SVG name="spinner" parser="dom" style={{ color: 'rebeccapurple' }} />
|
|
15
|
+
* @throws {Error} When both name and icon are missing
|
|
16
16
|
* @throws {Error} When the requested SVG file cannot be found
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* @typedef {object} SVGProps
|
|
21
|
-
* @property {string}
|
|
21
|
+
* @property {string} [name] - Basename of the SVG file (without .svg)
|
|
22
|
+
* @property {string} [icon] - Alias of name. Use whichever reads better
|
|
22
23
|
* @property {object|string} [style] - Inline styles. Object is converted with `toStyleString`
|
|
23
24
|
* @property {"naive"|"dom"} [parser="naive"] - Parsing strategy
|
|
24
25
|
* @property {*} [children] - Ignored; component does not support slots
|
|
@@ -30,12 +31,15 @@
|
|
|
30
31
|
/** @type {SVGProps} */
|
|
31
32
|
const {
|
|
32
33
|
icon,
|
|
34
|
+
name,
|
|
33
35
|
style,
|
|
34
36
|
parser = 'naive', // or DOM
|
|
35
37
|
children, // Just to remove children slots
|
|
36
38
|
...rest
|
|
37
39
|
} = $props()
|
|
38
40
|
|
|
41
|
+
const basename = name || icon
|
|
42
|
+
|
|
39
43
|
const files = {
|
|
40
44
|
...import.meta.glob(`/src/svg/**/*.svg`, { eager: true, query: 'raw' }),
|
|
41
45
|
...import.meta.glob(`/src/svgs/**/*.svg`, { eager: true, query: 'raw' }),
|
|
@@ -43,9 +47,11 @@
|
|
|
43
47
|
|
|
44
48
|
// Find the SVG
|
|
45
49
|
const file = init(() => {
|
|
46
|
-
if (!
|
|
47
|
-
const found = Object.keys(files).find(
|
|
48
|
-
|
|
50
|
+
if (!basename) throw new Error('name (or icon) property is required')
|
|
51
|
+
const found = Object.keys(files).find(
|
|
52
|
+
f => f.split('/').pop() === `${basename}.svg`,
|
|
53
|
+
)
|
|
54
|
+
if (!found) throw new Error(`${basename}.svg not found`)
|
|
49
55
|
return found
|
|
50
56
|
})
|
|
51
57
|
|
|
@@ -77,19 +83,20 @@
|
|
|
77
83
|
Inline SVG renderer for files under `/src/svg` or `/src/svgs`.
|
|
78
84
|
|
|
79
85
|
- Props:
|
|
80
|
-
-
|
|
86
|
+
- name (string): Basename of the SVG file (without .svg)
|
|
87
|
+
- icon (string): Alias of name. Use whichever reads better
|
|
81
88
|
- style (object|string, optional): Inline styles; objects are converted with `toStyleString`
|
|
82
89
|
- parser ("naive"|"dom", optional, default "naive"): Parsing strategy
|
|
83
90
|
- Slots: none
|
|
84
91
|
- Events: none
|
|
85
92
|
- Errors:
|
|
86
|
-
- Throws if `icon`
|
|
93
|
+
- Throws if both `name` and `icon` are missing
|
|
87
94
|
- Throws if the SVG file cannot be found
|
|
88
95
|
|
|
89
96
|
Usage:
|
|
90
97
|
```svelte
|
|
91
|
-
<SVG
|
|
92
|
-
<SVG
|
|
98
|
+
<SVG name="logo" class="w-6 h-6" />
|
|
99
|
+
<SVG name="spinner" parser="dom" style={{ color: 'rebeccapurple' }} />
|
|
93
100
|
```
|
|
94
101
|
-->
|
|
95
102
|
|
package/components/SVG/parser.js
CHANGED
|
@@ -46,10 +46,10 @@ export function naiveParser(content, options) {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
// If width/height exist but viewBox doesn't, create viewBox
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
finalViewBox = `0 0 ${
|
|
49
|
+
// from the file's own dimensions — a width/height option only
|
|
50
|
+
// changes the display size, not the coordinate system.
|
|
51
|
+
if (!currentViewBox && currentWidth && currentHeight) {
|
|
52
|
+
finalViewBox = `0 0 ${currentWidth} ${currentHeight}`
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
// Apply width if we have a final value
|