viconic-react-icons 1.4.0 â 1.4.2
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/README.md +53 -34
- package/package.json +1 -1
- package/src/index.jsx +214 -214
package/README.md
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
|
-
# Viconic React Icons
|
|
1
|
+
ïŧŋ# Viconic React Icons
|
|
2
2
|
|
|
3
|
-

|
|
4
|
-

|
|
3
|
+
[](https://www.npmjs.com/package/viconic-react-icons)
|
|
4
|
+
[](https://github.com/noname21024/copyicons-backend)
|
|
5
|
+
[](https://bundlephobia.com/package/viconic-react-icons)
|
|
5
6
|
|
|
6
7
|
The official React component wrapper for [Viconic](https://viconic.dev), a modern, hyper-fast, CDN-powered icon system.
|
|
7
8
|
|
|
8
|
-
`viconic-react-icons` gives you access to over 200,000+ open-source, pixel-perfect icons grouped in customizable collections. The icons are loaded dynamically from our Smart CDN at runtime, meaning your React bundle stays incredibly lightweight regardless of how many icons you use.
|
|
9
|
+
`viconic-react-icons` gives you access to over **200,000+ open-source, pixel-perfect icons** grouped in customizable collections. The icons are loaded dynamically from our Smart CDN at runtime, meaning your React bundle stays incredibly lightweight regardless of how many icons you use.
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
- ðŠķ **Zero-Bundle Bloat**: SVGs are fetched magically via our CDN and injected directly into the DOM.
|
|
12
|
-
- ðĻ **Fully Customizable**: Compatible with Tailwind CSS and standard inline styles. Inherits color natively via `currentColor`.
|
|
13
|
-
- ⥠**Smart Caching**: LocalStorage and Memory caching so icons appear instantly on repeated renders.
|
|
14
|
-
- ð§ **Kit Support**: Use your custom icon kit with `initViconic()` â supports multiple kits.
|
|
15
|
-
- ðĨïļ **SSR Ready**: Fully supports Next.js, Vite, and other Server-Side Rendering frameworks.
|
|
16
|
-
- ð **TypeScript Support**: Includes built-in types for easy autocompletion.
|
|
11
|
+
---
|
|
17
12
|
|
|
18
|
-
##
|
|
13
|
+
## ð Highlights
|
|
14
|
+
|
|
15
|
+
* ðŠķ **Zero-Bundle Bloat**: SVGs are fetched magically via our CDN and injected directly into the DOM.
|
|
16
|
+
* ⥠**Smart Caching**: LocalStorage and Memory caching so icons appear instantly on repeated renders. Event-driven icon injection for ultra-fast, progressive rendering.
|
|
17
|
+
* ïŋ―ïŋ― **Fully Customizable**: Compatible with Tailwind CSS and standard inline styles. Inherits color natively via `currentColor`.
|
|
18
|
+
* ð§ **Kit Support**: Use your custom icon kit with `initViconic()` â supports multiple kits and user-uploaded SVGs simultaneously.
|
|
19
|
+
* ðĨïļ **SSR Ready**: Fully supports Next.js, Vite, Remix, and other Server-Side Rendering frameworks.
|
|
20
|
+
* ð **TypeScript Support**: Includes built-in types for easy autocompletion.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## ðĶ Installation
|
|
19
25
|
|
|
20
26
|
```bash
|
|
21
27
|
npm install viconic-react-icons
|
|
@@ -25,7 +31,11 @@ yarn add viconic-react-icons
|
|
|
25
31
|
pnpm add viconic-react-icons
|
|
26
32
|
```
|
|
27
33
|
|
|
28
|
-
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## ð Quick Start
|
|
37
|
+
|
|
38
|
+
### 1. System Icons (200k+ Free Icons)
|
|
29
39
|
|
|
30
40
|
Import the `ViconicIcon` component and pass the unique `name` identifier corresponding to your icon of choice from [viconic.dev](https://viconic.dev).
|
|
31
41
|
|
|
@@ -37,7 +47,7 @@ function App() {
|
|
|
37
47
|
<div style={{ display: "flex", gap: "10px" }}>
|
|
38
48
|
{/* Basic Usage */}
|
|
39
49
|
<ViconicIcon name="h2:0" />
|
|
40
|
-
|
|
50
|
+
|
|
41
51
|
{/* With Tailwind CSS */}
|
|
42
52
|
<ViconicIcon name="lucide:home" className="w-8 h-8 text-blue-500" />
|
|
43
53
|
|
|
@@ -48,17 +58,17 @@ function App() {
|
|
|
48
58
|
}
|
|
49
59
|
```
|
|
50
60
|
|
|
51
|
-
|
|
61
|
+
### 2. Custom Kits & Uploaded Icons
|
|
52
62
|
|
|
53
|
-
Use icons from your own kit created at [viconic.dev](https://viconic.dev):
|
|
63
|
+
Use icons from your own customized kit (including your own SVG uploads) created at [viconic.dev](https://viconic.dev):
|
|
54
64
|
|
|
55
65
|
```jsx
|
|
56
66
|
import { initViconic, ViconicIcon } from "viconic-react-icons";
|
|
57
67
|
|
|
58
|
-
// Initialize your kit â call once at app startup (e.g., in main.jsx or
|
|
68
|
+
// Initialize your kit â call once at app startup (e.g., in main.jsx or _app.tsx)
|
|
59
69
|
initViconic({ kitId: "your-kit-uuid-here" });
|
|
60
70
|
|
|
61
|
-
// You can load multiple kits!
|
|
71
|
+
// You can load multiple kits at the same time!
|
|
62
72
|
initViconic({ kitId: "another-kit-uuid" });
|
|
63
73
|
|
|
64
74
|
function App() {
|
|
@@ -66,21 +76,23 @@ function App() {
|
|
|
66
76
|
<div style={{ display: "flex", gap: "10px" }}>
|
|
67
77
|
{/* Kit icon using @prefix/name format */}
|
|
68
78
|
<ViconicIcon name="@myprefix/home" />
|
|
69
|
-
|
|
70
|
-
{/*
|
|
71
|
-
<ViconicIcon name="myprefix
|
|
72
|
-
|
|
79
|
+
|
|
80
|
+
{/* User uploaded icon format */}
|
|
81
|
+
<ViconicIcon name="@myprefix/my-custom-logo" size="48px" />
|
|
82
|
+
|
|
73
83
|
{/* Mix kit icons with system icons */}
|
|
74
|
-
<ViconicIcon name="lucide:star" />
|
|
84
|
+
<ViconicIcon name="lucide:star" color="#FFD700" />
|
|
75
85
|
</div>
|
|
76
86
|
);
|
|
77
87
|
}
|
|
78
88
|
```
|
|
79
89
|
|
|
80
|
-
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## ðïļ Props
|
|
81
93
|
|
|
82
94
|
| Prop | Type | Default | Description |
|
|
83
|
-
|
|
|
95
|
+
| :--- | :--- | :--- | :--- |
|
|
84
96
|
| `name` | `string` | `undefined` | **Required.** The unique icon ID (e.g. `lucide:activity`, `@prefix/name`). |
|
|
85
97
|
| `size` | `string \| number` | `undefined` | Icon size as CSS value (e.g. `"24px"`, `"2rem"`). |
|
|
86
98
|
| `color` | `string` | `undefined` | Icon color as CSS value (e.g. `"red"`, `"#333"`). |
|
|
@@ -88,16 +100,19 @@ function App() {
|
|
|
88
100
|
| `style` | `React.CSSProperties` | `{}` | Inline CSS styling. |
|
|
89
101
|
| `...props` | `HTMLAttributes` | | Spread standard HTML attributes (e.g. `onClick`, `title`). |
|
|
90
102
|
|
|
91
|
-
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## ð ïļ API Reference
|
|
92
106
|
|
|
93
107
|
### `initViconic(options)`
|
|
94
108
|
|
|
95
|
-
Inject a kit's loader script into `<head>`. Call once per kit at app startup.
|
|
109
|
+
Inject a kit's smart loader script into `<head>`. Call once per kit at app startup.
|
|
96
110
|
|
|
97
111
|
| Option | Type | Default | Description |
|
|
98
|
-
|
|
|
112
|
+
| :--- | :--- | :--- | :--- |
|
|
99
113
|
| `kitId` | `string` | â | **Required.** UUID of your Viconic Kit. |
|
|
100
114
|
| `cdnBase` | `string` | `"cdn.viconic.dev"` | Custom CDN domain. |
|
|
115
|
+
| `version` | `string` | `undefined` | Bypass cache version (e.g. pass `"dev"` or a timestamp). |
|
|
101
116
|
|
|
102
117
|
```jsx
|
|
103
118
|
// In your main.jsx or App.jsx
|
|
@@ -106,14 +121,18 @@ import { initViconic } from "viconic-react-icons";
|
|
|
106
121
|
initViconic({ kitId: "387a6161-cb39-411f-8f13-29a5813e4efd" });
|
|
107
122
|
```
|
|
108
123
|
|
|
109
|
-
|
|
124
|
+
---
|
|
125
|
+
|
|
126
|
+
## ð§ Architecture
|
|
127
|
+
|
|
128
|
+
This package is a high-performance React wrapper around `<viconic-icon>` Web Components. The included smart loader script dynamically monitors the DOM using a lightweight `MutationObserver` and rapidly replaces `<viconic-icon>` elements with actual raw `<svg>` code.
|
|
110
129
|
|
|
111
|
-
|
|
130
|
+
When using kits, `initViconic()` injects the kit's loader from CDN, which leverages **chunked batch fetching**, **event-driven rendering**, and **parallel processing**.
|
|
112
131
|
|
|
113
|
-
|
|
132
|
+
Because we fetch SVGs in parallel and cache them heavily via LocalStorage across user sessions, your frontend performance score will skyrocket compared to packing large icon sets directly into your JS chunks.
|
|
114
133
|
|
|
115
|
-
|
|
134
|
+
---
|
|
116
135
|
|
|
117
|
-
## License
|
|
136
|
+
## ð License
|
|
118
137
|
|
|
119
|
-
This project is licensed under the MIT License. Icon licenses depend on the specific icon families you
|
|
138
|
+
This project is licensed under the MIT License. Icon licenses depend on the specific icon families you use.
|
package/package.json
CHANGED
package/src/index.jsx
CHANGED
|
@@ -1,214 +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: Event-driven â listen 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;
|
|
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-driven â listen 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;
|