viconic-react-icons 1.3.0 → 1.4.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/dist/index.js CHANGED
@@ -2061,11 +2061,7 @@ var ViconicIcon = ({ name, className, style, size, color, ...props }) => {
2061
2061
  el._u();
2062
2062
  }
2063
2063
  if (isInjected()) return;
2064
- const delays = [50, 100, 200, 400, 800, 500, 500, 500, 500, 500, 500, 500];
2065
- let attempt = 0;
2066
- let timerId = null;
2067
- let reloadTriggered = false;
2068
- function tryInject() {
2064
+ function tryInjectFromEvent() {
2069
2065
  if (isInjected()) return;
2070
2066
  const W = window.__viconic;
2071
2067
  if (W && W.icons) {
@@ -2081,8 +2077,12 @@ var ViconicIcon = ({ name, className, style, size, color, ...props }) => {
2081
2077
  if (!isKitIcon && window.CopyIcons && window.CopyIcons.forceProcess) {
2082
2078
  window.CopyIcons.forceProcess(el);
2083
2079
  }
2084
- if (isKitIcon && !reloadTriggered && attempt >= 4 && prefixMatch) {
2085
- reloadTriggered = true;
2080
+ }
2081
+ document.addEventListener("viconic:ready", tryInjectFromEvent);
2082
+ const fallbackTimer = setTimeout(() => {
2083
+ if (isInjected()) return;
2084
+ tryInjectFromEvent();
2085
+ if (!isInjected() && isKitIcon && prefixMatch) {
2086
2086
  const prefix = prefixMatch[1];
2087
2087
  if (window.CopyIconsKit) {
2088
2088
  for (const kId in window.CopyIconsKit) {
@@ -2094,15 +2094,10 @@ var ViconicIcon = ({ name, className, style, size, color, ...props }) => {
2094
2094
  }
2095
2095
  }
2096
2096
  }
2097
- if (isInjected()) return;
2098
- attempt++;
2099
- if (attempt < delays.length) {
2100
- timerId = setTimeout(tryInject, delays[attempt]);
2101
- }
2102
- }
2103
- timerId = setTimeout(tryInject, delays[0]);
2097
+ }, 500);
2104
2098
  return () => {
2105
- if (timerId) clearTimeout(timerId);
2099
+ document.removeEventListener("viconic:ready", tryInjectFromEvent);
2100
+ clearTimeout(fallbackTimer);
2106
2101
  };
2107
2102
  }, [name, className, size, color, style]);
2108
2103
  const combinedStyle = {
package/dist/index.mjs CHANGED
@@ -2026,11 +2026,7 @@ var ViconicIcon = ({ name, className, style, size, color, ...props }) => {
2026
2026
  el._u();
2027
2027
  }
2028
2028
  if (isInjected()) return;
2029
- const delays = [50, 100, 200, 400, 800, 500, 500, 500, 500, 500, 500, 500];
2030
- let attempt = 0;
2031
- let timerId = null;
2032
- let reloadTriggered = false;
2033
- function tryInject() {
2029
+ function tryInjectFromEvent() {
2034
2030
  if (isInjected()) return;
2035
2031
  const W = window.__viconic;
2036
2032
  if (W && W.icons) {
@@ -2046,8 +2042,12 @@ var ViconicIcon = ({ name, className, style, size, color, ...props }) => {
2046
2042
  if (!isKitIcon && window.CopyIcons && window.CopyIcons.forceProcess) {
2047
2043
  window.CopyIcons.forceProcess(el);
2048
2044
  }
2049
- if (isKitIcon && !reloadTriggered && attempt >= 4 && prefixMatch) {
2050
- reloadTriggered = true;
2045
+ }
2046
+ document.addEventListener("viconic:ready", tryInjectFromEvent);
2047
+ const fallbackTimer = setTimeout(() => {
2048
+ if (isInjected()) return;
2049
+ tryInjectFromEvent();
2050
+ if (!isInjected() && isKitIcon && prefixMatch) {
2051
2051
  const prefix = prefixMatch[1];
2052
2052
  if (window.CopyIconsKit) {
2053
2053
  for (const kId in window.CopyIconsKit) {
@@ -2059,15 +2059,10 @@ var ViconicIcon = ({ name, className, style, size, color, ...props }) => {
2059
2059
  }
2060
2060
  }
2061
2061
  }
2062
- if (isInjected()) return;
2063
- attempt++;
2064
- if (attempt < delays.length) {
2065
- timerId = setTimeout(tryInject, delays[attempt]);
2066
- }
2067
- }
2068
- timerId = setTimeout(tryInject, delays[0]);
2062
+ }, 500);
2069
2063
  return () => {
2070
- if (timerId) clearTimeout(timerId);
2064
+ document.removeEventListener("viconic:ready", tryInjectFromEvent);
2065
+ clearTimeout(fallbackTimer);
2071
2066
  };
2072
2067
  }, [name, className, size, color, style]);
2073
2068
  const combinedStyle = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "viconic-react-icons",
3
- "version": "1.3.0",
3
+ "version": "1.4.1",
4
4
  "description": "Viconic Smart Icons loader for React — supports Kit and 200k+ system icons",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
package/src/index.jsx CHANGED
@@ -1,223 +1,214 @@
1
- import React, { useEffect, useRef } from 'react';
2
- import './copyicons-smart-loader.js';
3
-
4
- // ============================================
5
- // PRE-LOAD KIT CACHE SYNCHRONOUSLY AT MODULE LEVEL
6
- // Same architecture as kit loader: read localStorage before any render
7
- // so icons are available instantly when components mount.
8
- // ============================================
9
- (function _preloadKitCache() {
10
- if (typeof window === 'undefined' || typeof localStorage === 'undefined') return;
11
- try {
12
- const W = (window.__viconic = window.__viconic || {});
13
- W.icons = W.icons || {};
14
- const TTL = 3 * 24 * 60 * 60 * 1000; // 3 days
15
- for (let i = 0; i < localStorage.length; i++) {
16
- const key = localStorage.key(i);
17
- if (!key || !key.startsWith('viconic:')) continue;
18
- try {
19
- const c = JSON.parse(localStorage.getItem(key));
20
- if (c && c.d && Date.now() - c.t < TTL) {
21
- // Register all icon name variants into W.icons
22
- for (const n in c.d) {
23
- if (!Object.prototype.hasOwnProperty.call(c.d, n)) continue;
24
- const svg = c.d[n];
25
- // Store by plain name so _u() can find it regardless of prefix
26
- if (!W.icons[n]) W.icons[n] = svg;
27
- }
28
- }
29
- } catch (e) { /* corrupted entry, skip */ }
30
- }
31
- } catch (e) { }
32
- })();
33
-
34
- // ============================================
35
- // Track initialized kits to avoid duplicate scripts
36
- // ============================================
37
- const _initializedKits = new Set();
38
-
39
- /**
40
- * Initialize a Viconic Kit in your React app.
41
- * Injects the kit's loader.js script into <head>.
42
- * Supports multiple kits — call once per kit.
43
- *
44
- * @param {Object} options
45
- * @param {string} options.kitId - UUID of the Kit (required)
46
- * @param {string} [options.cdnBase] - Custom CDN domain (default: cdn.viconic.dev)
47
- *
48
- * @example
49
- * import { initViconic } from 'viconic-react-icons';
50
- *
51
- * initViconic({ kitId: '387a6161-cb39-411f-8f13-29a5813e4efd' });
52
- * // Multiple kits:
53
- * initViconic({ kitId: 'another-kit-uuid', version: '1.0' });
54
- */
55
- export function initViconic(options = {}) {
56
- const { kitId, cdnBase = 'cdn.viconic.dev', version } = options;
57
- if (typeof window === 'undefined') return; // SSR guard
58
- if (!kitId) {
59
- console.warn('[Viconic] initViconic requires a kitId');
60
- return;
61
- }
62
- if (_initializedKits.has(kitId)) return; // Already initialized
63
- _initializedKits.add(kitId);
64
-
65
- const scriptId = `viconic-kit-${kitId}`;
66
- if (document.getElementById(scriptId)) return; // Script already in DOM
67
-
68
- const script = document.createElement('script');
69
- script.id = scriptId;
70
-
71
- // Add cache busting for development or specific version updates
72
- let url = `https://${cdnBase}/kits/${kitId}/loader.js`;
73
- if (version) {
74
- const vQuery = version === 'dev' ? Date.now() : version;
75
- url += `?v=${vQuery}`;
76
- }
77
-
78
- script.src = url;
79
- script.async = true;
80
- document.head.appendChild(script);
81
- }
82
-
83
- /**
84
- * ViconicIcon — React component for rendering Viconic icons.
85
- *
86
- * Works with both:
87
- * - System icons: <ViconicIcon name="lucide:home" />
88
- * - Kit icons: <ViconicIcon name="@myprefix/home" /> (after initViconic)
89
- *
90
- * @param {string} name - Icon identifier (e.g., "lucide:home", "@prefix/name")
91
- * @param {string} [className] - CSS class names
92
- * @param {object} [style] - Inline styles
93
- * @param {string|number} [size] - Icon size (CSS value)
94
- * @param {string} [color] - Icon color (CSS value)
95
- */
96
- export const ViconicIcon = ({ name, className, style, size, color, ...props }) => {
97
- const iconRef = useRef(null);
98
- const prevNameRef = useRef(name);
99
-
100
- useEffect(() => {
101
- const el = iconRef.current;
102
- if (!el) return;
103
-
104
- // --- FIX DOM REUSE (Reconciliation) ---
105
- if (prevNameRef.current !== name) {
106
- el.innerHTML = '';
107
- el.classList.remove('vi-ok', 'svg-loaded', 'vi-mono', 'ci-multicolor');
108
- prevNameRef.current = name;
109
- }
110
-
111
- const isKitIcon = name && name.startsWith('@');
112
- const prefixMatch = isKitIcon ? name.match(/^@([^/]+)\/(.+)$/) : null;
113
-
114
- function isInjected() {
115
- return el.classList.contains('vi-ok') || el.classList.contains('svg-loaded');
116
- }
117
-
118
- // --- PATH 1: Synchronous inject from W.icons (pre-loaded from localStorage at module init) ---
119
- if (!isInjected()) {
120
- const W = window.__viconic;
121
- if (W && W.icons) {
122
- // Try all key variants: iconBaseName, @prefix/name, prefix:name, prefix/name
123
- const iconBaseName = prefixMatch ? prefixMatch[2] : null;
124
- const svg = (iconBaseName && W.icons[iconBaseName])
125
- || (name && W.icons[name])
126
- || null;
127
- if (svg) {
128
- el.innerHTML = svg;
129
- el.classList.add('vi-ok', 'svg-loaded');
130
- }
131
- }
132
- }
133
-
134
- // --- PATH 2: Smart loader for system icons (prefix:name) ---
135
- if (!isInjected() && !isKitIcon) {
136
- if (window.CopyIcons && window.CopyIcons.forceProcess) {
137
- window.CopyIcons.forceProcess(el);
138
- }
139
- }
140
-
141
- // --- PATH 3: Custom element _u() call (kit loader registered element) ---
142
- if (!isInjected() && el._u) {
143
- el._u();
144
- }
145
-
146
- if (isInjected()) return;
147
-
148
- // --- PATH 4: Fallback retry kit loader script may still be loading async ---
149
- // Use short exponential backoff: 50ms, 100ms, 200ms, 400ms, 800ms, then every 500ms
150
- // Max 8 seconds total. Much cheaper than 100 × 100ms polling.
151
- const delays = [50, 100, 200, 400, 800, 500, 500, 500, 500, 500, 500, 500];
152
- let attempt = 0;
153
- let timerId = null;
154
- let reloadTriggered = false;
155
-
156
- function tryInject() {
157
- if (isInjected()) return;
158
-
159
- // Try W.icons again (kit may have loaded by now)
160
- const W = window.__viconic;
161
- if (W && W.icons) {
162
- const iconBaseName = prefixMatch ? prefixMatch[2] : null;
163
- const svg = (iconBaseName && W.icons[iconBaseName])
164
- || (name && W.icons[name])
165
- || null;
166
- if (svg) {
167
- el.innerHTML = svg;
168
- el.classList.add('vi-ok', 'svg-loaded');
169
- return;
170
- }
171
- }
172
-
173
- // Try custom element _u()
174
- if (el._u) el._u();
175
- if (!isKitIcon && window.CopyIcons && window.CopyIcons.forceProcess) {
176
- window.CopyIcons.forceProcess(el);
177
- }
178
-
179
- // Trigger kit reload once if still not loaded after ~1s
180
- if (isKitIcon && !reloadTriggered && attempt >= 4 && prefixMatch) {
181
- reloadTriggered = true;
182
- const prefix = prefixMatch[1];
183
- if (window.CopyIconsKit) {
184
- for (const kId in window.CopyIconsKit) {
185
- const kit = window.CopyIconsKit[kId];
186
- if (kit.config && kit.config.prefix === prefix && typeof kit.reload === 'function') {
187
- kit.reload();
188
- break;
189
- }
190
- }
191
- }
192
- }
193
-
194
- if (isInjected()) return;
195
-
196
- attempt++;
197
- if (attempt < delays.length) {
198
- timerId = setTimeout(tryInject, delays[attempt]);
199
- }
200
- }
201
-
202
- timerId = setTimeout(tryInject, delays[0]);
203
- return () => { if (timerId) clearTimeout(timerId); };
204
- }, [name, className, size, color, style]);
205
-
206
- const combinedStyle = {
207
- ...(size ? { fontSize: size } : {}),
208
- ...(color ? { color: color } : {}),
209
- ...style
210
- };
211
-
212
- return (
213
- <viconic-icon
214
- ref={iconRef}
215
- icon={name}
216
- class={className}
217
- style={combinedStyle}
218
- {...props}
219
- ></viconic-icon>
220
- );
221
- };
222
-
223
- export default ViconicIcon;
1
+ import React, { useEffect, useRef } from 'react';
2
+ import './copyicons-smart-loader.js';
3
+
4
+ // ============================================
5
+ // PRE-LOAD KIT CACHE SYNCHRONOUSLY AT MODULE LEVEL
6
+ // Same architecture as kit loader: read localStorage before any render
7
+ // so icons are available instantly when components mount.
8
+ // ============================================
9
+ (function _preloadKitCache() {
10
+ if (typeof window === 'undefined' || typeof localStorage === 'undefined') return;
11
+ try {
12
+ const W = (window.__viconic = window.__viconic || {});
13
+ W.icons = W.icons || {};
14
+ const TTL = 3 * 24 * 60 * 60 * 1000; // 3 days
15
+ for (let i = 0; i < localStorage.length; i++) {
16
+ const key = localStorage.key(i);
17
+ if (!key || !key.startsWith('viconic:')) continue;
18
+ try {
19
+ const c = JSON.parse(localStorage.getItem(key));
20
+ if (c && c.d && Date.now() - c.t < TTL) {
21
+ // Register all icon name variants into W.icons
22
+ for (const n in c.d) {
23
+ if (!Object.prototype.hasOwnProperty.call(c.d, n)) continue;
24
+ const svg = c.d[n];
25
+ // Store by plain name so _u() can find it regardless of prefix
26
+ if (!W.icons[n]) W.icons[n] = svg;
27
+ }
28
+ }
29
+ } catch (e) { /* corrupted entry, skip */ }
30
+ }
31
+ } catch (e) { }
32
+ })();
33
+
34
+ // ============================================
35
+ // Track initialized kits to avoid duplicate scripts
36
+ // ============================================
37
+ const _initializedKits = new Set();
38
+
39
+ /**
40
+ * Initialize a Viconic Kit in your React app.
41
+ * Injects the kit's loader.js script into <head>.
42
+ * Supports multiple kits — call once per kit.
43
+ *
44
+ * @param {Object} options
45
+ * @param {string} options.kitId - UUID of the Kit (required)
46
+ * @param {string} [options.cdnBase] - Custom CDN domain (default: cdn.viconic.dev)
47
+ *
48
+ * @example
49
+ * import { initViconic } from 'viconic-react-icons';
50
+ *
51
+ * initViconic({ kitId: '387a6161-cb39-411f-8f13-29a5813e4efd' });
52
+ * // Multiple kits:
53
+ * initViconic({ kitId: 'another-kit-uuid', version: '1.0' });
54
+ */
55
+ export function initViconic(options = {}) {
56
+ const { kitId, cdnBase = 'cdn.viconic.dev', version } = options;
57
+ if (typeof window === 'undefined') return; // SSR guard
58
+ if (!kitId) {
59
+ console.warn('[Viconic] initViconic requires a kitId');
60
+ return;
61
+ }
62
+ if (_initializedKits.has(kitId)) return; // Already initialized
63
+ _initializedKits.add(kitId);
64
+
65
+ const scriptId = `viconic-kit-${kitId}`;
66
+ if (document.getElementById(scriptId)) return; // Script already in DOM
67
+
68
+ const script = document.createElement('script');
69
+ script.id = scriptId;
70
+
71
+ // Add cache busting for development or specific version updates
72
+ let url = `https://${cdnBase}/kits/${kitId}/loader.js`;
73
+ if (version) {
74
+ const vQuery = version === 'dev' ? Date.now() : version;
75
+ url += `?v=${vQuery}`;
76
+ }
77
+
78
+ script.src = url;
79
+ script.async = true;
80
+ document.head.appendChild(script);
81
+ }
82
+
83
+ /**
84
+ * ViconicIcon — React component for rendering Viconic icons.
85
+ *
86
+ * Works with both:
87
+ * - System icons: <ViconicIcon name="lucide:home" />
88
+ * - Kit icons: <ViconicIcon name="@myprefix/home" /> (after initViconic)
89
+ *
90
+ * @param {string} name - Icon identifier (e.g., "lucide:home", "@prefix/name")
91
+ * @param {string} [className] - CSS class names
92
+ * @param {object} [style] - Inline styles
93
+ * @param {string|number} [size] - Icon size (CSS value)
94
+ * @param {string} [color] - Icon color (CSS value)
95
+ */
96
+ export const ViconicIcon = ({ name, className, style, size, color, ...props }) => {
97
+ const iconRef = useRef(null);
98
+ const prevNameRef = useRef(name);
99
+
100
+ useEffect(() => {
101
+ const el = iconRef.current;
102
+ if (!el) return;
103
+
104
+ // --- FIX DOM REUSE (Reconciliation) ---
105
+ if (prevNameRef.current !== name) {
106
+ el.innerHTML = '';
107
+ el.classList.remove('vi-ok', 'svg-loaded', 'vi-mono', 'ci-multicolor');
108
+ prevNameRef.current = name;
109
+ }
110
+
111
+ const isKitIcon = name && name.startsWith('@');
112
+ const prefixMatch = isKitIcon ? name.match(/^@([^/]+)\/(.+)$/) : null;
113
+
114
+ function isInjected() {
115
+ return el.classList.contains('vi-ok') || el.classList.contains('svg-loaded');
116
+ }
117
+
118
+ // --- PATH 1: Synchronous inject from W.icons (pre-loaded from localStorage at module init) ---
119
+ if (!isInjected()) {
120
+ const W = window.__viconic;
121
+ if (W && W.icons) {
122
+ // Try all key variants: iconBaseName, @prefix/name, prefix:name, prefix/name
123
+ const iconBaseName = prefixMatch ? prefixMatch[2] : null;
124
+ const svg = (iconBaseName && W.icons[iconBaseName])
125
+ || (name && W.icons[name])
126
+ || null;
127
+ if (svg) {
128
+ el.innerHTML = svg;
129
+ el.classList.add('vi-ok', 'svg-loaded');
130
+ }
131
+ }
132
+ }
133
+
134
+ // --- PATH 2: Smart loader for system icons (prefix:name) ---
135
+ if (!isInjected() && !isKitIcon) {
136
+ if (window.CopyIcons && window.CopyIcons.forceProcess) {
137
+ window.CopyIcons.forceProcess(el);
138
+ }
139
+ }
140
+
141
+ // --- PATH 3: Custom element _u() call (kit loader registered element) ---
142
+ if (!isInjected() && el._u) {
143
+ el._u();
144
+ }
145
+
146
+ if (isInjected()) return;
147
+
148
+ // --- PATH 4: Event-drivenlisten for viconic:ready fired by kit loader after register() ---
149
+ // Much faster than polling: icons appear the instant the loader finishes fetching them.
150
+ function tryInjectFromEvent() {
151
+ if (isInjected()) return;
152
+ const W = window.__viconic;
153
+ if (W && W.icons) {
154
+ const iconBaseName = prefixMatch ? prefixMatch[2] : null;
155
+ const svg = (iconBaseName && W.icons[iconBaseName])
156
+ || (name && W.icons[name])
157
+ || null;
158
+ if (svg) {
159
+ el.innerHTML = svg;
160
+ el.classList.add('vi-ok', 'svg-loaded');
161
+ return;
162
+ }
163
+ }
164
+ if (el._u) el._u();
165
+ if (!isKitIcon && window.CopyIcons && window.CopyIcons.forceProcess) {
166
+ window.CopyIcons.forceProcess(el);
167
+ }
168
+ }
169
+
170
+ document.addEventListener('viconic:ready', tryInjectFromEvent);
171
+
172
+ // --- PATH 5: Short-deadline fallback (500ms) in case script already fired before we listened ---
173
+ const fallbackTimer = setTimeout(() => {
174
+ if (isInjected()) return;
175
+ tryInjectFromEvent();
176
+ // If still not loaded after 500ms, trigger kit reload once
177
+ if (!isInjected() && isKitIcon && prefixMatch) {
178
+ const prefix = prefixMatch[1];
179
+ if (window.CopyIconsKit) {
180
+ for (const kId in window.CopyIconsKit) {
181
+ const kit = window.CopyIconsKit[kId];
182
+ if (kit.config && kit.config.prefix === prefix && typeof kit.reload === 'function') {
183
+ kit.reload();
184
+ break;
185
+ }
186
+ }
187
+ }
188
+ }
189
+ }, 500);
190
+
191
+ return () => {
192
+ document.removeEventListener('viconic:ready', tryInjectFromEvent);
193
+ clearTimeout(fallbackTimer);
194
+ };
195
+ }, [name, className, size, color, style]);
196
+
197
+ const combinedStyle = {
198
+ ...(size ? { fontSize: size } : {}),
199
+ ...(color ? { color: color } : {}),
200
+ ...style
201
+ };
202
+
203
+ return (
204
+ <viconic-icon
205
+ ref={iconRef}
206
+ icon={name}
207
+ class={className}
208
+ style={combinedStyle}
209
+ {...props}
210
+ ></viconic-icon>
211
+ );
212
+ };
213
+
214
+ export default ViconicIcon;