@dynamic-icon/svelte 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +14 -2
- package/dist/DynamicIcon.svelte +100 -0
- package/dist/DynamicIcon.svelte.d.ts +13 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/types.d.ts +29 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +11 -0
- package/dist/utils.js +29 -0
- package/package.json +57 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 dynamic-icon contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dynamic-icon/svelte
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Renders icons from database records. Knows nothing about any icon library: sources are registered with
|
|
4
|
+
[`@dynamic-icon/svelte-config`](../svelte-config) (a peer dependency, so there is exactly one registry).
|
|
5
|
+
|
|
6
|
+
```svelte
|
|
7
|
+
<DynamicIcon {...record.icon} size={24} class="text-primary" />
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Props: `webType`, `webRef`, `color`, `media`, `scale`, `size` (px or CSS length), `strokeWidth`, `class`, `fallback` (snippet).
|
|
11
|
+
|
|
12
|
+
- `color` is a CSS colour (`#fff`, `rgb()`, `var()`, `red`) applied via `style`, or otherwise a utility class.
|
|
13
|
+
- `scale` multiplies a numeric `size` and rounds it; string sizes are used as-is.
|
|
14
|
+
- Only `image` and `markup` icons get a wrapper `<span>` (they need a sized box); everything else renders one element.
|
|
15
|
+
- Install both packages; the host app calls `configureIcons()` once. See the config package.
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* @component DynamicIcon
|
|
4
|
+
* Renders a database-driven icon record. Sources, fallback and defaults come from `configureIcons()`.
|
|
5
|
+
*
|
|
6
|
+
* <DynamicIcon {...record.icon} size={24} class="text-primary" />
|
|
7
|
+
*
|
|
8
|
+
* Resolution order by `webType`: `image` | `emoji` | `markup` | font type | configured source.
|
|
9
|
+
* `class` and `color` land on the rendered element; `image`/`markup` get a sized wrapper to carry them.
|
|
10
|
+
*/
|
|
11
|
+
import type { IconProps } from './types.js';
|
|
12
|
+
import { getIconConfig, resolveIcon } from '@dynamic-icon/svelte-config';
|
|
13
|
+
import { boxSize, boxStyle, classes, colorStyle, textStyle } from './utils.js';
|
|
14
|
+
|
|
15
|
+
const config = getIconConfig();
|
|
16
|
+
const Fallback = config.fallback;
|
|
17
|
+
|
|
18
|
+
let {
|
|
19
|
+
webType,
|
|
20
|
+
webRef,
|
|
21
|
+
color,
|
|
22
|
+
media,
|
|
23
|
+
scale,
|
|
24
|
+
size = config.size,
|
|
25
|
+
strokeWidth = config.strokeWidth,
|
|
26
|
+
fallback,
|
|
27
|
+
class: className
|
|
28
|
+
}: IconProps = $props();
|
|
29
|
+
|
|
30
|
+
// The only per-instance derived value: caching it keeps the component from re-mounting
|
|
31
|
+
// when unrelated props (size, color, class) change.
|
|
32
|
+
const Comp = $derived(resolveIcon(webType, webRef));
|
|
33
|
+
</script>
|
|
34
|
+
|
|
35
|
+
{#if Comp}
|
|
36
|
+
<Comp
|
|
37
|
+
size={boxSize(size, scale)}
|
|
38
|
+
{strokeWidth}
|
|
39
|
+
class={classes(color, className)}
|
|
40
|
+
style={colorStyle(color)}
|
|
41
|
+
/>
|
|
42
|
+
{:else if webType === 'image' && media?.link}
|
|
43
|
+
<span
|
|
44
|
+
class={classes(color, className, 'u-icon')}
|
|
45
|
+
style={boxStyle(boxSize(size, scale), color)}
|
|
46
|
+
role="presentation"
|
|
47
|
+
>
|
|
48
|
+
<img src={media.link} alt={media.caption ?? ''} loading="lazy" decoding="async" />
|
|
49
|
+
</span>
|
|
50
|
+
{:else if webType === 'markup' && webRef && config.sanitize}
|
|
51
|
+
<span
|
|
52
|
+
class={classes(color, className, 'u-icon u-markup')}
|
|
53
|
+
style={boxStyle(boxSize(size, scale), color)}
|
|
54
|
+
role="presentation"
|
|
55
|
+
>
|
|
56
|
+
{@html config.sanitize(webRef)}
|
|
57
|
+
</span>
|
|
58
|
+
{:else if webType === 'emoji' && webRef}
|
|
59
|
+
<span class={classes(color, className)} style={textStyle(boxSize(size, scale), color)}>{webRef}</span>
|
|
60
|
+
{:else if webType && webRef && config.fontTypes.has(webType)}
|
|
61
|
+
<i class={classes(color, className, webRef)} style={textStyle(boxSize(size, scale), color)}></i>
|
|
62
|
+
{:else if fallback}
|
|
63
|
+
{@render fallback()}
|
|
64
|
+
{:else if Fallback}
|
|
65
|
+
<Fallback
|
|
66
|
+
size={boxSize(size, scale)}
|
|
67
|
+
{strokeWidth}
|
|
68
|
+
class={classes(color, className)}
|
|
69
|
+
style={colorStyle(color)}
|
|
70
|
+
/>
|
|
71
|
+
{/if}
|
|
72
|
+
|
|
73
|
+
<style>
|
|
74
|
+
/* Scoped CSS instead of Tailwind utilities: Tailwind does not scan node_modules by default,
|
|
75
|
+
so utility classes used inside a package would silently not exist in the consumer's build. */
|
|
76
|
+
.u-icon {
|
|
77
|
+
display: inline-flex;
|
|
78
|
+
flex-shrink: 0;
|
|
79
|
+
align-items: center;
|
|
80
|
+
justify-content: center;
|
|
81
|
+
line-height: 1;
|
|
82
|
+
user-select: none;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
.u-icon > img {
|
|
86
|
+
width: 100%;
|
|
87
|
+
height: 100%;
|
|
88
|
+
object-fit: contain;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/* Injected SVG fills the box; root <svg> without an explicit fill inherits `color`. */
|
|
92
|
+
.u-markup :global(svg) {
|
|
93
|
+
width: 100%;
|
|
94
|
+
height: 100%;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
.u-markup :global(svg:not([fill])) {
|
|
98
|
+
fill: currentColor;
|
|
99
|
+
}
|
|
100
|
+
</style>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @component DynamicIcon
|
|
3
|
+
* Renders a database-driven icon record. Sources, fallback and defaults come from `configureIcons()`.
|
|
4
|
+
*
|
|
5
|
+
* <DynamicIcon {...record.icon} size={24} class="text-primary" />
|
|
6
|
+
*
|
|
7
|
+
* Resolution order by `webType`: `image` | `emoji` | `markup` | font type | configured source.
|
|
8
|
+
* `class` and `color` land on the rendered element; `image`/`markup` get a sized wrapper to carry them.
|
|
9
|
+
*/
|
|
10
|
+
import type { IconProps } from './types.js';
|
|
11
|
+
declare const DynamicIcon: import("svelte").Component<IconProps<string>, {}, "">;
|
|
12
|
+
type DynamicIcon = ReturnType<typeof DynamicIcon>;
|
|
13
|
+
export default DynamicIcon;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as DynamicIcon } from "./DynamicIcon.svelte";
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Snippet } from "svelte";
|
|
2
|
+
/** Media attached to an `image` icon. Structurally compatible with CMS file objects. */
|
|
3
|
+
export interface IconMedia {
|
|
4
|
+
link: string;
|
|
5
|
+
caption?: string;
|
|
6
|
+
}
|
|
7
|
+
/** Icon record as served by the CMS/DB. Narrow `T` to your own `webType`s if you like. */
|
|
8
|
+
export interface IconEntry<T extends string = string> {
|
|
9
|
+
/** Unique identifier key (e.g. 'nav_home'). Not used for rendering. */
|
|
10
|
+
name?: string;
|
|
11
|
+
/** Which source/renderer handles this icon. */
|
|
12
|
+
webType?: T;
|
|
13
|
+
/** Identifier within the source ('car-front'), icon-font classes, emoji, or raw markup. */
|
|
14
|
+
webRef?: string;
|
|
15
|
+
/** A Tailwind/utility class (`text-primary`) or a CSS colour (`#fff`, `rgb()`, `var()`, `red`). */
|
|
16
|
+
color?: string;
|
|
17
|
+
/** Image for the `image` type. */
|
|
18
|
+
media?: IconMedia;
|
|
19
|
+
/** Per-source size normalisation factor; the result is rounded to whole pixels. Numeric `size` only. */
|
|
20
|
+
scale?: number;
|
|
21
|
+
}
|
|
22
|
+
export interface IconProps<T extends string = string> extends IconEntry<T> {
|
|
23
|
+
/** Visual size: px when a number, any CSS length when a string. */
|
|
24
|
+
size?: number | string;
|
|
25
|
+
class?: string;
|
|
26
|
+
strokeWidth?: number;
|
|
27
|
+
/** Rendered when the icon cannot be resolved. Overrides `IconConfig.fallback`. */
|
|
28
|
+
fallback?: Snippet;
|
|
29
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const isCssColor: (color: string | undefined) => color is string;
|
|
2
|
+
/** Applies the per-source `scale` to numeric sizes, rounded to avoid blurry sub-pixel rendering. */
|
|
3
|
+
export declare const boxSize: (size: number | string, scale: number | undefined) => number | string;
|
|
4
|
+
/** `style` for icon components: only a CSS colour needs it. */
|
|
5
|
+
export declare const colorStyle: (color: string | undefined) => string | undefined;
|
|
6
|
+
/** `style` for the sized box around `image` / `markup` icons. */
|
|
7
|
+
export declare const boxStyle: (size: number | string, color: string | undefined) => string;
|
|
8
|
+
/** `style` for text-like icons (emoji, icon fonts). */
|
|
9
|
+
export declare const textStyle: (size: number | string, color: string | undefined) => string;
|
|
10
|
+
/** Utility-class colour first, then the consumer's class. Plain concatenation, no conflict resolution. */
|
|
11
|
+
export declare const classes: (color: string | undefined, extra: string | undefined, base?: string) => string | undefined;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A CSS colour value: hex, a keyword (`red`, `currentColor`) or a colour function (`rgb()`, `oklch()`, `var()`...).
|
|
3
|
+
* Anything else (`text-primary`, `text-red-500 dark:text-red-300`, `text-(--brand)`) is a utility class.
|
|
4
|
+
* Strict on purpose: the value ends up in a `style` attribute, so `;`, braces and quotes never qualify.
|
|
5
|
+
*/
|
|
6
|
+
const CSS_COLOR = /^(?:#[\da-f]{3,8}|[a-z]+|[a-z]+(?:-[a-z]+)*\([^;{}"'<>]*\))$/i;
|
|
7
|
+
export const isCssColor = (color) => color !== undefined && CSS_COLOR.test(color);
|
|
8
|
+
/** Applies the per-source `scale` to numeric sizes, rounded to avoid blurry sub-pixel rendering. */
|
|
9
|
+
export const boxSize = (size, scale) => typeof size === "number" && scale ? Math.round(size * scale) : size;
|
|
10
|
+
const cssLength = (size) => (typeof size === "number" ? `${size}px` : size);
|
|
11
|
+
const colorDecl = (color) => (isCssColor(color) ? `;color:${color}` : "");
|
|
12
|
+
/** `style` for icon components: only a CSS colour needs it. */
|
|
13
|
+
export const colorStyle = (color) => isCssColor(color) ? `color:${color}` : undefined;
|
|
14
|
+
/** `style` for the sized box around `image` / `markup` icons. */
|
|
15
|
+
export const boxStyle = (size, color) => {
|
|
16
|
+
const length = cssLength(size);
|
|
17
|
+
return `width:${length};height:${length}${colorDecl(color)}`;
|
|
18
|
+
};
|
|
19
|
+
/** `style` for text-like icons (emoji, icon fonts). */
|
|
20
|
+
export const textStyle = (size, color) => `font-size:${cssLength(size)}${colorDecl(color)}`;
|
|
21
|
+
/** Utility-class colour first, then the consumer's class. Plain concatenation, no conflict resolution. */
|
|
22
|
+
export const classes = (color, extra, base) => {
|
|
23
|
+
let out = base;
|
|
24
|
+
if (color && !isCssColor(color))
|
|
25
|
+
out = out ? `${out} ${color}` : color;
|
|
26
|
+
if (extra)
|
|
27
|
+
out = out ? `${out} ${extra}` : extra;
|
|
28
|
+
return out;
|
|
29
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamic-icon/svelte",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Tiny Svelte 5 component that renders database-driven icons. Sources are plugged in via @dynamic-icon/svelte-config.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"svelte",
|
|
7
|
+
"svelte5",
|
|
8
|
+
"icons",
|
|
9
|
+
"dynamic-icon"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"sideEffects": [
|
|
14
|
+
"**/*.css"
|
|
15
|
+
],
|
|
16
|
+
"svelte": "./dist/index.js",
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"svelte": "./dist/index.js",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE"
|
|
30
|
+
],
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"svelte": "^5.0.0",
|
|
33
|
+
"@dynamic-icon/svelte-config": "^0.1.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@sveltejs/package": "^2.3.0",
|
|
37
|
+
"@sveltejs/vite-plugin-svelte": "^4.0.0",
|
|
38
|
+
"@types/node": "^22.0.0",
|
|
39
|
+
"jsdom": "^25.0.0",
|
|
40
|
+
"svelte": "^5.0.0",
|
|
41
|
+
"svelte-check": "^4.0.0",
|
|
42
|
+
"typescript": "^5.6.0",
|
|
43
|
+
"vite": "^5.4.0",
|
|
44
|
+
"vitest": "^2.1.0",
|
|
45
|
+
"@dynamic-icon/svelte-config": "0.1.0"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=18"
|
|
49
|
+
},
|
|
50
|
+
"publishConfig": {
|
|
51
|
+
"access": "public"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "svelte-package -i src -o dist",
|
|
55
|
+
"typecheck": "svelte-check --tsconfig ./tsconfig.json",
|
|
56
|
+
"typecheck:test": "svelte-check --tsconfig ./tsconfig.test.json",
|
|
57
|
+
"test": "vitest run",
|
|
58
|
+
"test:watch": "vitest"
|
|
59
|
+
}
|
|
6
60
|
}
|