@wentools/iconic 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Wentools
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 ADDED
@@ -0,0 +1,118 @@
1
+ # @wentools/iconic
2
+
3
+ Svelte 5 icon component with animations and a resolver factory for Lucide icons.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @wentools/iconic
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Direct component usage
14
+
15
+ ```svelte
16
+ <script>
17
+ import { Icon } from '@wentools/iconic'
18
+ import { Search } from '@lucide/svelte'
19
+ </script>
20
+
21
+ <Icon icon={Search} />
22
+ <Icon icon={Search} animation="spin" />
23
+ <Icon icon={Search} size={24} color="red" />
24
+ ```
25
+
26
+ ### String-based resolution
27
+
28
+ Set up a project-specific icon registry:
29
+
30
+ ```typescript
31
+ // src/lib/icons/index.ts
32
+ import { createIconResolver } from '@wentools/iconic'
33
+ import { Search, Home, Plus } from '@lucide/svelte'
34
+
35
+ export const iconResolver = createIconResolver({
36
+ search: Search,
37
+ home: Home,
38
+ plus: Plus,
39
+ })
40
+
41
+ export type IconVariant = keyof typeof iconResolver.map
42
+ ```
43
+
44
+ Wire the resolver in your root layout:
45
+
46
+ ```svelte
47
+ <!-- src/routes/+layout.svelte -->
48
+ <script>
49
+ import { setIconResolver } from '@wentools/iconic'
50
+ import { iconResolver } from '$lib/icons'
51
+
52
+ setIconResolver(iconResolver)
53
+ </script>
54
+ ```
55
+
56
+ Then use string variants anywhere:
57
+
58
+ ```svelte
59
+ <Icon icon="search" animation="pulse" />
60
+ ```
61
+
62
+ ### Animations
63
+
64
+ Five built-in animations with `prefers-reduced-motion` support:
65
+
66
+ - `spin` — continuous rotation
67
+ - `pulse` — opacity fade
68
+ - `bounce` — vertical bounce
69
+ - `shake` — horizontal shake
70
+ - `ping` — scale + fade out
71
+
72
+ ```svelte
73
+ <Icon icon={Search} animation="spin" />
74
+ <Icon icon={Search} animation="bounce" animationDuration={2} />
75
+ <Icon icon={Search} animation="pulse" animationIterations={3} />
76
+ ```
77
+
78
+ ### IconAction type
79
+
80
+ For components with interactive icons:
81
+
82
+ ```typescript
83
+ import type { IconAction } from '@wentools/iconic'
84
+
85
+ interface Props {
86
+ actions: IconAction<MyIconVariant>[]
87
+ }
88
+ ```
89
+
90
+ ## API
91
+
92
+ ### `Icon` component
93
+
94
+ | Prop | Type | Default | Description |
95
+ |------|------|---------|-------------|
96
+ | `icon` | `Component \| string` | required | Lucide component or string variant |
97
+ | `size` | `string \| number` | `'var(--icon-size, 1rem)'` | Icon size |
98
+ | `color` | `string` | `'currentColor'` | Icon color |
99
+ | `strokeWidth` | `number` | `2` | Stroke width |
100
+ | `absoluteStrokeWidth` | `boolean` | `false` | Use absolute stroke width |
101
+ | `animation` | `IconAnimation` | — | Animation type |
102
+ | `animationDuration` | `number` | varies | Duration in seconds |
103
+ | `animationEasing` | `string` | varies | CSS easing function |
104
+ | `animationIterations` | `number \| 'infinite'` | `'infinite'` | Iteration count |
105
+
106
+ ### `createIconResolver(map)`
107
+
108
+ Creates a typed resolver from a `{ variant: Component }` map.
109
+
110
+ Returns `{ resolveToComponent, isIconVariant, iconVariants, map }`.
111
+
112
+ ### `setIconResolver(resolver)` / `getIconResolver()`
113
+
114
+ Svelte context helpers for app-wide string resolution.
115
+
116
+ ## License
117
+
118
+ MIT
@@ -0,0 +1,212 @@
1
+ <script lang="ts">
2
+ import type { Component } from 'svelte'
3
+ import type { IconAnimation } from '../types'
4
+ import type { IconResolver } from '../resolver/create_icon_resolver'
5
+ import { ICON_RESOLVER_KEY } from '../resolver/context'
6
+ import { getContext } from 'svelte'
7
+
8
+ interface Props {
9
+ icon: Component | string
10
+ size?: string | number
11
+ color?: string
12
+ strokeWidth?: number
13
+ absoluteStrokeWidth?: boolean
14
+ animation?: IconAnimation
15
+ animationDuration?: number
16
+ animationEasing?: string
17
+ animationIterations?: number | 'infinite'
18
+ }
19
+
20
+ let {
21
+ icon,
22
+ size = 'var(--icon-size, 1rem)',
23
+ color = 'currentColor',
24
+ strokeWidth = 2,
25
+ absoluteStrokeWidth = false,
26
+ animation,
27
+ animationDuration,
28
+ animationEasing,
29
+ animationIterations,
30
+ }: Props = $props()
31
+
32
+ // Get resolver at component init time (top-level), not inside $derived
33
+ const resolver = getContext<IconResolver | undefined>(ICON_RESOLVER_KEY)
34
+
35
+ // Resolve icon variant to component if needed
36
+ const Icon = $derived.by(() => {
37
+ if (typeof icon !== 'string') return icon
38
+ if (!resolver) {
39
+ throw new Error(
40
+ 'Icon received a string variant but no icon resolver is set. ' +
41
+ 'Call setIconResolver() in a parent component (e.g. root layout).',
42
+ )
43
+ }
44
+ return resolver.resolveToComponent(icon)
45
+ })
46
+
47
+ // Convert size to appropriate format for Lucide icons
48
+ const lucideSize = $derived(typeof size === 'number' ? size : '100%')
49
+ const wrapperSize = $derived(typeof size === 'number' ? `${size}px` : size)
50
+
51
+ // Use the animation prop directly
52
+ const activeAnimation = $derived(animation ?? null)
53
+
54
+ // Build animation CSS custom properties
55
+ const animationCustomProps = $derived.by(() => {
56
+ if (!activeAnimation) return {}
57
+
58
+ const defaultDurations: Record<IconAnimation, number> = {
59
+ spin: 1,
60
+ pulse: 2,
61
+ bounce: 1.2,
62
+ shake: 0.5,
63
+ ping: 1,
64
+ }
65
+
66
+ const defaultEasings: Record<IconAnimation, string> = {
67
+ spin: 'linear',
68
+ pulse: 'ease-in-out',
69
+ bounce: 'ease-in-out',
70
+ shake: 'ease-in-out',
71
+ ping: 'cubic-bezier(0, 0, 0.2, 1)',
72
+ }
73
+
74
+ return {
75
+ '--animation-duration': `${animationDuration ?? defaultDurations[activeAnimation]}s`,
76
+ '--animation-easing': animationEasing ?? defaultEasings[activeAnimation],
77
+ '--animation-iterations': animationIterations ?? 'infinite',
78
+ }
79
+ })
80
+
81
+ // Build style string from custom properties
82
+ const animationStyle = $derived.by(() => {
83
+ const props = animationCustomProps
84
+ return Object.entries(props)
85
+ .map(([key, value]) => `${key}: ${value}`)
86
+ .join('; ')
87
+ })
88
+
89
+ // Build class string
90
+ const animationClass = $derived(activeAnimation ? `animate-${activeAnimation}` : '')
91
+ </script>
92
+
93
+ <div class={animationClass} style:--size={wrapperSize} style:--color={color} style={animationStyle}>
94
+ <Icon {color} size={lucideSize} {strokeWidth} {absoluteStrokeWidth} />
95
+ </div>
96
+
97
+ <style>
98
+ div {
99
+ --shake-distance: calc(var(--icon-size, 1rem) * 0.1);
100
+
101
+ height: var(--size);
102
+ width: var(--size);
103
+ color: var(--color);
104
+ display: inline-flex;
105
+ align-items: center;
106
+ justify-content: center;
107
+ transition: color 200ms ease;
108
+ }
109
+
110
+ /* Animation keyframes */
111
+ @keyframes spin {
112
+ from {
113
+ transform: rotate(0deg);
114
+ }
115
+ to {
116
+ transform: rotate(360deg);
117
+ }
118
+ }
119
+
120
+ @keyframes pulse {
121
+ 0%,
122
+ 100% {
123
+ opacity: 1;
124
+ }
125
+ 50% {
126
+ opacity: 0.5;
127
+ }
128
+ }
129
+
130
+ @keyframes bounce {
131
+ 0%,
132
+ 20%,
133
+ 50%,
134
+ 80%,
135
+ 100% {
136
+ transform: translateY(0);
137
+ animation-timing-function: cubic-bezier(0.8, 0, 1, 1);
138
+ }
139
+ 40% {
140
+ transform: translateY(-30%);
141
+ animation-timing-function: cubic-bezier(0, 0, 0.2, 1);
142
+ }
143
+ 60% {
144
+ transform: translateY(-15%);
145
+ animation-timing-function: cubic-bezier(0, 0, 0.2, 1);
146
+ }
147
+ }
148
+
149
+ @keyframes shake {
150
+ 0%,
151
+ 100% {
152
+ transform: translateX(0);
153
+ }
154
+ 10%,
155
+ 30%,
156
+ 50%,
157
+ 70%,
158
+ 90% {
159
+ transform: translateX(calc(var(--shake-distance) * -1));
160
+ }
161
+ 20%,
162
+ 40%,
163
+ 60%,
164
+ 80% {
165
+ transform: translateX(var(--shake-distance));
166
+ }
167
+ }
168
+
169
+ @keyframes ping {
170
+ 0% {
171
+ transform: scale(1);
172
+ opacity: 1;
173
+ }
174
+ 75%,
175
+ 100% {
176
+ transform: scale(1.2);
177
+ opacity: 0;
178
+ }
179
+ }
180
+
181
+ /* Animation classes */
182
+ .animate-spin {
183
+ animation: spin var(--animation-duration) var(--animation-easing) var(--animation-iterations);
184
+ }
185
+
186
+ .animate-pulse {
187
+ animation: pulse var(--animation-duration) var(--animation-easing) var(--animation-iterations);
188
+ }
189
+
190
+ .animate-bounce {
191
+ animation: bounce var(--animation-duration) var(--animation-easing) var(--animation-iterations);
192
+ }
193
+
194
+ .animate-shake {
195
+ animation: shake var(--animation-duration) var(--animation-easing) var(--animation-iterations);
196
+ }
197
+
198
+ .animate-ping {
199
+ animation: ping var(--animation-duration) var(--animation-easing) var(--animation-iterations);
200
+ }
201
+
202
+ /* Respect user's motion preferences */
203
+ @media (prefers-reduced-motion: reduce) {
204
+ .animate-spin,
205
+ .animate-pulse,
206
+ .animate-bounce,
207
+ .animate-shake,
208
+ .animate-ping {
209
+ animation: none;
210
+ }
211
+ }
212
+ </style>
@@ -0,0 +1,16 @@
1
+ import type { Component } from 'svelte';
2
+ import type { IconAnimation } from '../types';
3
+ interface Props {
4
+ icon: Component | string;
5
+ size?: string | number;
6
+ color?: string;
7
+ strokeWidth?: number;
8
+ absoluteStrokeWidth?: boolean;
9
+ animation?: IconAnimation;
10
+ animationDuration?: number;
11
+ animationEasing?: string;
12
+ animationIterations?: number | 'infinite';
13
+ }
14
+ declare const Icon: Component<Props, {}, "">;
15
+ type Icon = ReturnType<typeof Icon>;
16
+ export default Icon;
@@ -0,0 +1 @@
1
+ export { default as Icon } from './Icon.svelte';
@@ -0,0 +1 @@
1
+ export { default as Icon } from './Icon.svelte';
@@ -0,0 +1,4 @@
1
+ export { Icon } from './icon/index';
2
+ export type { IconAnimation, IconAction } from './types';
3
+ export { createIconResolver, setIconResolver, getIconResolver } from './resolver/index';
4
+ export type { IconResolver } from './resolver/index';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { Icon } from './icon/index';
2
+ export { createIconResolver, setIconResolver, getIconResolver } from './resolver/index';
@@ -0,0 +1,5 @@
1
+ import type { IconResolver } from './create_icon_resolver';
2
+ declare const ICON_RESOLVER_KEY: unique symbol;
3
+ declare function setIconResolver(resolver: IconResolver): void;
4
+ declare function getIconResolver(): IconResolver | undefined;
5
+ export { ICON_RESOLVER_KEY, setIconResolver, getIconResolver };
@@ -0,0 +1,9 @@
1
+ import { getContext, setContext } from 'svelte';
2
+ const ICON_RESOLVER_KEY = Symbol('icon-resolver');
3
+ function setIconResolver(resolver) {
4
+ setContext(ICON_RESOLVER_KEY, resolver);
5
+ }
6
+ function getIconResolver() {
7
+ return getContext(ICON_RESOLVER_KEY);
8
+ }
9
+ export { ICON_RESOLVER_KEY, setIconResolver, getIconResolver };
@@ -0,0 +1,14 @@
1
+ import type { Component } from 'svelte';
2
+ interface IconResolver {
3
+ resolveToComponent: (variant: string) => Component;
4
+ isIconVariant: (value: string) => boolean;
5
+ iconVariants: readonly string[];
6
+ }
7
+ declare function createIconResolver<const T extends Record<string, Component>>(map: T): {
8
+ resolveToComponent: (variant: keyof T & string) => Component;
9
+ isIconVariant: (value: string) => value is keyof T & string;
10
+ iconVariants: readonly (keyof T & string)[];
11
+ map: T;
12
+ };
13
+ export { createIconResolver };
14
+ export type { IconResolver };
@@ -0,0 +1,20 @@
1
+ function createIconResolver(map) {
2
+ const variants = Object.keys(map);
3
+ const resolveToComponent = (variant) => {
4
+ const component = map[variant];
5
+ if (!component) {
6
+ throw new Error(`Unknown icon variant: "${variant}"`);
7
+ }
8
+ return component;
9
+ };
10
+ const isIconVariant = (value) => {
11
+ return value in map;
12
+ };
13
+ return {
14
+ resolveToComponent,
15
+ isIconVariant,
16
+ iconVariants: variants,
17
+ map,
18
+ };
19
+ }
20
+ export { createIconResolver };
@@ -0,0 +1,3 @@
1
+ export { createIconResolver } from './create_icon_resolver';
2
+ export type { IconResolver } from './create_icon_resolver';
3
+ export { setIconResolver, getIconResolver } from './context';
@@ -0,0 +1,2 @@
1
+ export { createIconResolver } from './create_icon_resolver';
2
+ export { setIconResolver, getIconResolver } from './context';
@@ -0,0 +1,17 @@
1
+ import type { Component } from 'svelte';
2
+ type IconAnimation = 'spin' | 'pulse' | 'bounce' | 'shake' | 'ping';
3
+ /**
4
+ * Shared type for icon actions used in form inputs, tags, badges, etc.
5
+ * Generic over the icon variant string type so projects can narrow it.
6
+ */
7
+ interface IconAction<V extends string = string> {
8
+ icon: Component | V;
9
+ action?: () => void;
10
+ label?: string;
11
+ title?: string;
12
+ animation?: IconAnimation;
13
+ animationDuration?: number;
14
+ animationEasing?: string;
15
+ animationIterations?: number | 'infinite';
16
+ }
17
+ export type { IconAnimation, IconAction };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@wentools/iconic",
3
+ "version": "0.1.0",
4
+ "description": "Svelte 5 icon component with animations and resolver factory for Lucide icons",
5
+ "type": "module",
6
+ "svelte": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "svelte": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "svelte-kit sync && svelte-package",
20
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json"
21
+ },
22
+ "peerDependencies": {
23
+ "svelte": "^5.0.0"
24
+ },
25
+ "devDependencies": {
26
+ "@sveltejs/package": "^2.0.0",
27
+ "@sveltejs/kit": "^2.0.0",
28
+ "svelte": "^5.0.0",
29
+ "svelte-check": "^4.0.0",
30
+ "typescript": "^5.0.0"
31
+ },
32
+ "license": "MIT",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://gitlab.com/wentools/iconic"
36
+ },
37
+ "keywords": [
38
+ "svelte",
39
+ "icons",
40
+ "lucide",
41
+ "animation",
42
+ "component"
43
+ ]
44
+ }