viconic-react-icons 1.0.7 → 1.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/src/index.jsx CHANGED
@@ -1,13 +1,95 @@
1
1
  import React, { useEffect, useRef } from 'react';
2
2
  import './copyicons-smart-loader.js';
3
3
 
4
- export const ViconicIcon = ({ name, className, style, size, color, ...props }) => {
4
+ // ============================================
5
+ // Track initialized kits to avoid duplicate scripts
6
+ // ============================================
7
+ const _initializedKits = new Set();
8
+
9
+ /**
10
+ * Initialize a Viconic Kit in your React app.
11
+ * Injects the kit's loader.js script into <head>.
12
+ * Supports multiple kits — call once per kit.
13
+ *
14
+ * @param {Object} options
15
+ * @param {string} options.kitId - UUID of the Kit (required)
16
+ * @param {string} [options.cdnBase] - Custom CDN domain (default: cdn.viconic.dev)
17
+ *
18
+ * @example
19
+ * import { initViconic } from 'viconic-react-icons';
20
+ *
21
+ * initViconic({ kitId: '387a6161-cb39-411f-8f13-29a5813e4efd' });
22
+ * // Multiple kits:
23
+ * initViconic({ kitId: 'another-kit-uuid' });
24
+ */
25
+ export function initViconic({ kitId, cdnBase = 'cdn.viconic.dev' }) {
26
+ if (typeof window === 'undefined') return; // SSR guard
27
+ if (!kitId) {
28
+ console.warn('[Viconic] initViconic requires a kitId');
29
+ return;
30
+ }
31
+ if (_initializedKits.has(kitId)) return; // Already initialized
32
+ _initializedKits.add(kitId);
33
+
34
+ const scriptId = `viconic-kit-${kitId}`;
35
+ if (document.getElementById(scriptId)) return; // Script already in DOM
36
+
37
+ const script = document.createElement('script');
38
+ script.id = scriptId;
39
+ script.src = `https://${cdnBase}/kits/${kitId}/loader.js`;
40
+ script.async = true;
41
+ document.head.appendChild(script);
42
+ }
43
+
44
+ /**
45
+ * ViconicIcon — React component for rendering Viconic icons.
46
+ *
47
+ * Works with both:
48
+ * - System icons: <ViconicIcon name="lucide:home" />
49
+ * - Kit icons: <ViconicIcon name="@myprefix/home" /> (after initViconic)
50
+ *
51
+ * @param {string} name - Icon identifier (e.g., "lucide:home", "@prefix/name")
52
+ * @param {string} [className] - CSS class names
53
+ * @param {object} [style] - Inline styles
54
+ * @param {string|number} [size] - Icon size (CSS value)
55
+ * @param {string} [color] - Icon color (CSS value)
56
+ */
57
+ export const ViconicIcon = ({ name, className, style, size, color, ...props }) => {
5
58
  const iconRef = useRef(null);
6
59
 
7
60
  useEffect(() => {
8
- if (window.CopyIcons && iconRef.current) {
9
- window.CopyIcons.forceProcess(iconRef.current);
61
+ const el = iconRef.current;
62
+ if (!el) return;
63
+
64
+ // Path 1: Smart loader (system icons with prefix:name format)
65
+ if (window.CopyIcons && window.CopyIcons.forceProcess) {
66
+ window.CopyIcons.forceProcess(el);
10
67
  }
68
+
69
+ // Path 2: Kit icons — kit loader.js registers icons async into
70
+ // window.__viconic.icons and the custom element _u() method handles injection.
71
+ // If kit hasn't loaded yet, retry periodically until icon appears.
72
+ const isLoaded = el.classList.contains('vi-ok') || el.classList.contains('svg-loaded');
73
+ if (isLoaded) return;
74
+
75
+ let retries = 0;
76
+ const maxRetries = 50; // 50 × 100ms = 5 seconds max
77
+ const timer = setInterval(() => {
78
+ retries++;
79
+ if (el.classList.contains('vi-ok') || el.classList.contains('svg-loaded')) {
80
+ clearInterval(timer);
81
+ return;
82
+ }
83
+ // Trigger custom element update
84
+ if (el._u) el._u();
85
+ // Also try smart loader
86
+ if (window.CopyIcons && window.CopyIcons.forceProcess) {
87
+ window.CopyIcons.forceProcess(el);
88
+ }
89
+ if (retries >= maxRetries) clearInterval(timer);
90
+ }, 100);
91
+
92
+ return () => clearInterval(timer);
11
93
  }, [name, className, size, color, style]);
12
94
 
13
95
  const combinedStyle = {