svg-dynamic-icon 1.0.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/README.md +59 -0
- package/assets/icons/Fallback.svg +3 -0
- package/build.js +18 -0
- package/dist/assets/Fallback-R62GNTAJ.svg +3 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +101 -0
- package/dist/index.js.map +7 -0
- package/dist/src/DynamicIcon.d.ts +8 -0
- package/dist/src/DynamicIcon.js +36 -0
- package/index.ts +1 -0
- package/package.json +23 -0
- package/src/DynamicIcon.tsx +46 -0
- package/src/types/svg.d.ts +4 -0
- package/tsconfig.json +15 -0
package/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# svg-dynamic-icon
|
2
|
+
|
3
|
+
A lightweight React component for rendering **custom-made SVG icons** with **TailwindCSS** support.
|
4
|
+
Perfect for projects with a custom icon set, and you want dynamic usage without hardcoding imports.
|
5
|
+
|
6
|
+
## 📦 Installation
|
7
|
+
|
8
|
+
```bash
|
9
|
+
npm install svg-dynamic-icon
|
10
|
+
```
|
11
|
+
|
12
|
+
## 🚀 Usage
|
13
|
+
|
14
|
+
```bash
|
15
|
+
import { DynamicIcon } from "svg-dynamic-icon";
|
16
|
+
|
17
|
+
function Example() {
|
18
|
+
return (
|
19
|
+
<DynamicIcon
|
20
|
+
iconName="Checkmark" // refers to: assets/icons/Checkmark.svg
|
21
|
+
size=32 // icon will be 32px by 32px
|
22
|
+
color="text-blue-500" // tailwind class for icon color
|
23
|
+
/>
|
24
|
+
);
|
25
|
+
}
|
26
|
+
```
|
27
|
+
|
28
|
+
## 📁 Icon Setup
|
29
|
+
|
30
|
+
- Place your SVG icons in the /assets/icons/ folder.
|
31
|
+
- File names must match the iconName prop exactly (MyIcon.svg → iconName="MyIcon").
|
32
|
+
- Upload square-shaped icons for best rendering (e.g., 24x24 or 64x64).
|
33
|
+
|
34
|
+
## ⚙️ Props
|
35
|
+
|
36
|
+
**Prop** | **Type** | **Required** | **Description**
|
37
|
+
iconName | string | ✅ | Name of the SVG icon file (without .svg) located in /assets/icons/
|
38
|
+
size | number | ❌ | Icon size in pixels (width & height). Default: 25
|
39
|
+
color | string | ❌ | TailwindCSS text color class (e.g., text-white, text-gray-500)
|
40
|
+
|
41
|
+
## 💡 Example Folder Structure
|
42
|
+
|
43
|
+
project-root/
|
44
|
+
├── assets/
|
45
|
+
│....└── icons/
|
46
|
+
│.........├── Checkmark.svg
|
47
|
+
│.........└── Close.svg
|
48
|
+
├── App.tsx
|
49
|
+
└── ...
|
50
|
+
|
51
|
+
## 🧩 Built With
|
52
|
+
|
53
|
+
- React
|
54
|
+
- react-svg
|
55
|
+
- tailwind-merge
|
56
|
+
|
57
|
+
📝 License
|
58
|
+
|
59
|
+
MIT
|
package/build.js
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
// build.js
|
2
|
+
const esbuild = require("esbuild");
|
3
|
+
|
4
|
+
esbuild
|
5
|
+
.build({
|
6
|
+
entryPoints: ["index.ts"],
|
7
|
+
bundle: true,
|
8
|
+
minify: false,
|
9
|
+
sourcemap: true,
|
10
|
+
outfile: "dist/index.js",
|
11
|
+
platform: "node",
|
12
|
+
external: ["react", "react-dom", "react-svg", "tailwind-merge"],
|
13
|
+
loader: {
|
14
|
+
".svg": "file",
|
15
|
+
},
|
16
|
+
assetNames: "assets/[name]-[hash]",
|
17
|
+
})
|
18
|
+
.catch(() => process.exit(1));
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export { default as DynamicIcon } from "./src/DynamicIcon";
|
package/dist/index.js
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __create = Object.create;
|
3
|
+
var __defProp = Object.defineProperty;
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
8
|
+
var __glob = (map) => (path) => {
|
9
|
+
var fn = map[path];
|
10
|
+
if (fn) return fn();
|
11
|
+
throw new Error("Module not found in bundle: " + path);
|
12
|
+
};
|
13
|
+
var __commonJS = (cb, mod) => function __require() {
|
14
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
15
|
+
};
|
16
|
+
var __export = (target, all) => {
|
17
|
+
for (var name in all)
|
18
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
19
|
+
};
|
20
|
+
var __copyProps = (to, from, except, desc) => {
|
21
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
22
|
+
for (let key of __getOwnPropNames(from))
|
23
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
24
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
25
|
+
}
|
26
|
+
return to;
|
27
|
+
};
|
28
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
29
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
30
|
+
// file that has been converted to a CommonJS file using a Babel-
|
31
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
32
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
33
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
34
|
+
mod
|
35
|
+
));
|
36
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
37
|
+
|
38
|
+
// assets/icons/Fallback.svg
|
39
|
+
var require_Fallback = __commonJS({
|
40
|
+
"assets/icons/Fallback.svg"(exports2, module2) {
|
41
|
+
module2.exports = "./assets/Fallback-R62GNTAJ.svg";
|
42
|
+
}
|
43
|
+
});
|
44
|
+
|
45
|
+
// index.ts
|
46
|
+
var index_exports = {};
|
47
|
+
__export(index_exports, {
|
48
|
+
DynamicIcon: () => DynamicIcon_default
|
49
|
+
});
|
50
|
+
module.exports = __toCommonJS(index_exports);
|
51
|
+
|
52
|
+
// src/DynamicIcon.tsx
|
53
|
+
var import_react = require("react");
|
54
|
+
var import_react_svg = require("react-svg");
|
55
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
56
|
+
|
57
|
+
// import("../assets/icons/**/*.svg") in src/DynamicIcon.tsx
|
58
|
+
var globImport_assets_icons_svg = __glob({
|
59
|
+
"../assets/icons/Fallback.svg": () => Promise.resolve().then(() => __toESM(require_Fallback()))
|
60
|
+
});
|
61
|
+
|
62
|
+
// src/DynamicIcon.tsx
|
63
|
+
var DynamicIcon = ({
|
64
|
+
iconName = "Fallback",
|
65
|
+
size = "25",
|
66
|
+
color = "black"
|
67
|
+
}) => {
|
68
|
+
const [iconUrl, setIconUrl] = (0, import_react.useState)(null);
|
69
|
+
(0, import_react.useEffect)(() => {
|
70
|
+
const importIcon = async () => {
|
71
|
+
try {
|
72
|
+
const icon = await globImport_assets_icons_svg(`../assets/icons/${iconName}.svg`);
|
73
|
+
setIconUrl(icon.default);
|
74
|
+
} catch (error) {
|
75
|
+
console.error(`Icon ${iconName} not found`);
|
76
|
+
setIconUrl(null);
|
77
|
+
}
|
78
|
+
};
|
79
|
+
importIcon();
|
80
|
+
}, [iconName]);
|
81
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
82
|
+
import_react_svg.ReactSVG,
|
83
|
+
{
|
84
|
+
src: iconUrl || "",
|
85
|
+
className: color,
|
86
|
+
beforeInjection: (svg) => {
|
87
|
+
svg.setAttribute("style", `width: ${size}px; height: ${size}px;`);
|
88
|
+
svg.setAttribute("fill", "currentColor");
|
89
|
+
svg.querySelectorAll("path").forEach((path) => {
|
90
|
+
path.setAttribute("fill", "currentColor");
|
91
|
+
});
|
92
|
+
}
|
93
|
+
}
|
94
|
+
);
|
95
|
+
};
|
96
|
+
var DynamicIcon_default = DynamicIcon;
|
97
|
+
// Annotate the CommonJS export names for ESM import in node:
|
98
|
+
0 && (module.exports = {
|
99
|
+
DynamicIcon
|
100
|
+
});
|
101
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1,7 @@
|
|
1
|
+
{
|
2
|
+
"version": 3,
|
3
|
+
"sources": ["../index.ts", "../src/DynamicIcon.tsx"],
|
4
|
+
"sourcesContent": ["export { default as DynamicIcon } from \"./src/DynamicIcon\";\r\n", "import React, { useEffect, useState } from \"react\";\r\nimport { ReactSVG } from \"react-svg\";\r\n\r\ninterface DynamicIconProps {\r\n iconName: string;\r\n size?: number;\r\n color?: string;\r\n}\r\n\r\nconst DynamicIcon: React.FC<DynamicIconProps> = ({\r\n iconName = \"Fallback\",\r\n size = \"25\",\r\n color = \"black\",\r\n}) => {\r\n const [iconUrl, setIconUrl] = useState<string | null>(null);\r\n\r\n useEffect(() => {\r\n const importIcon = async () => {\r\n try {\r\n const icon = await import(`../assets/icons/${iconName}.svg`);\r\n setIconUrl(icon.default);\r\n } catch (error) {\r\n console.error(`Icon ${iconName} not found`);\r\n setIconUrl(null);\r\n }\r\n };\r\n\r\n importIcon();\r\n }, [iconName]);\r\n\r\n return (\r\n <ReactSVG\r\n src={iconUrl || \"\"}\r\n className={color}\r\n beforeInjection={(svg) => {\r\n svg.setAttribute(\"style\", `width: ${size}px; height: ${size}px;`);\r\n svg.setAttribute(\"fill\", \"currentColor\");\r\n svg.querySelectorAll(\"path\").forEach((path) => {\r\n path.setAttribute(\"fill\", \"currentColor\");\r\n });\r\n }}\r\n />\r\n );\r\n};\r\n\r\nexport default DynamicIcon;\r\n"],
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,mBAA2C;AAC3C,uBAAyB;AA8BrB;;;;;;;;AAtBJ,IAAM,cAA0C,CAAC;AAAA,EAC/C,WAAW;AAAA,EACX,OAAO;AAAA,EACP,QAAQ;AACV,MAAM;AACJ,QAAM,CAAC,SAAS,UAAU,QAAI,uBAAwB,IAAI;AAE1D,8BAAU,MAAM;AACd,UAAM,aAAa,YAAY;AAC7B,UAAI;AACF,cAAM,OAAO,MAAa,+CAAmB,QAAQ;AACrD,mBAAW,KAAK,OAAO;AAAA,MACzB,SAAS,OAAO;AACd,gBAAQ,MAAM,QAAQ,QAAQ,YAAY;AAC1C,mBAAW,IAAI;AAAA,MACjB;AAAA,IACF;AAEA,eAAW;AAAA,EACb,GAAG,CAAC,QAAQ,CAAC;AAEb,SACE;AAAA,IAAC;AAAA;AAAA,MACC,KAAK,WAAW;AAAA,MAChB,WAAW;AAAA,MACX,iBAAiB,CAAC,QAAQ;AACxB,YAAI,aAAa,SAAS,UAAU,IAAI,eAAe,IAAI,KAAK;AAChE,YAAI,aAAa,QAAQ,cAAc;AACvC,YAAI,iBAAiB,MAAM,EAAE,QAAQ,CAAC,SAAS;AAC7C,eAAK,aAAa,QAAQ,cAAc;AAAA,QAC1C,CAAC;AAAA,MACH;AAAA;AAAA,EACF;AAEJ;AAEA,IAAO,sBAAQ;",
|
6
|
+
"names": []
|
7
|
+
}
|
@@ -0,0 +1,36 @@
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8
|
+
});
|
9
|
+
};
|
10
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
11
|
+
import { useEffect, useState } from "react";
|
12
|
+
import { ReactSVG } from "react-svg";
|
13
|
+
const DynamicIcon = ({ iconName = "Fallback", size = "25", color = "black", }) => {
|
14
|
+
const [iconUrl, setIconUrl] = useState(null);
|
15
|
+
useEffect(() => {
|
16
|
+
const importIcon = () => __awaiter(void 0, void 0, void 0, function* () {
|
17
|
+
try {
|
18
|
+
const icon = yield import(`../assets/icons/${iconName}.svg`);
|
19
|
+
setIconUrl(icon.default);
|
20
|
+
}
|
21
|
+
catch (error) {
|
22
|
+
console.error(`Icon ${iconName} not found`);
|
23
|
+
setIconUrl(null);
|
24
|
+
}
|
25
|
+
});
|
26
|
+
importIcon();
|
27
|
+
}, [iconName]);
|
28
|
+
return (_jsx(ReactSVG, { src: iconUrl || "", className: color, beforeInjection: (svg) => {
|
29
|
+
svg.setAttribute("style", `width: ${size}px; height: ${size}px;`);
|
30
|
+
svg.setAttribute("fill", "currentColor");
|
31
|
+
svg.querySelectorAll("path").forEach((path) => {
|
32
|
+
path.setAttribute("fill", "currentColor");
|
33
|
+
});
|
34
|
+
} }));
|
35
|
+
};
|
36
|
+
export default DynamicIcon;
|
package/index.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
export { default as DynamicIcon } from "./src/DynamicIcon";
|
package/package.json
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
{
|
2
|
+
"name": "svg-dynamic-icon",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"main": "index.js",
|
5
|
+
"scripts": {
|
6
|
+
"build": "node build.js"
|
7
|
+
},
|
8
|
+
"keywords": [],
|
9
|
+
"author": "",
|
10
|
+
"license": "ISC",
|
11
|
+
"description": "",
|
12
|
+
"devDependencies": {
|
13
|
+
"@types/node": "^22.14.1",
|
14
|
+
"@types/react": "^19.1.2",
|
15
|
+
"esbuild": "^0.25.2",
|
16
|
+
"typescript": "^5.8.3"
|
17
|
+
},
|
18
|
+
"peerDependencies": {
|
19
|
+
"react": "^17.0.0 || ^18.0.0",
|
20
|
+
"react-dom": "^17.0.0 || ^18.0.0",
|
21
|
+
"react-svg": "^15.1.21"
|
22
|
+
}
|
23
|
+
}
|
@@ -0,0 +1,46 @@
|
|
1
|
+
import React, { useEffect, useState } from "react";
|
2
|
+
import { ReactSVG } from "react-svg";
|
3
|
+
|
4
|
+
interface DynamicIconProps {
|
5
|
+
iconName: string;
|
6
|
+
size?: number;
|
7
|
+
color?: string;
|
8
|
+
}
|
9
|
+
|
10
|
+
const DynamicIcon: React.FC<DynamicIconProps> = ({
|
11
|
+
iconName = "Fallback",
|
12
|
+
size = "25",
|
13
|
+
color = "black",
|
14
|
+
}) => {
|
15
|
+
const [iconUrl, setIconUrl] = useState<string | null>(null);
|
16
|
+
|
17
|
+
useEffect(() => {
|
18
|
+
const importIcon = async () => {
|
19
|
+
try {
|
20
|
+
const icon = await import(`../assets/icons/${iconName}.svg`);
|
21
|
+
setIconUrl(icon.default);
|
22
|
+
} catch (error) {
|
23
|
+
console.error(`Icon ${iconName} not found`);
|
24
|
+
setIconUrl(null);
|
25
|
+
}
|
26
|
+
};
|
27
|
+
|
28
|
+
importIcon();
|
29
|
+
}, [iconName]);
|
30
|
+
|
31
|
+
return (
|
32
|
+
<ReactSVG
|
33
|
+
src={iconUrl || ""}
|
34
|
+
className={color}
|
35
|
+
beforeInjection={(svg) => {
|
36
|
+
svg.setAttribute("style", `width: ${size}px; height: ${size}px;`);
|
37
|
+
svg.setAttribute("fill", "currentColor");
|
38
|
+
svg.querySelectorAll("path").forEach((path) => {
|
39
|
+
path.setAttribute("fill", "currentColor");
|
40
|
+
});
|
41
|
+
}}
|
42
|
+
/>
|
43
|
+
);
|
44
|
+
};
|
45
|
+
|
46
|
+
export default DynamicIcon;
|
package/tsconfig.json
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
"target": "ES6",
|
4
|
+
"module": "ESNext",
|
5
|
+
"jsx": "react-jsx",
|
6
|
+
"declaration": true,
|
7
|
+
"outDir": "dist",
|
8
|
+
"moduleResolution": "node",
|
9
|
+
"esModuleInterop": true,
|
10
|
+
"skipLibCheck": true,
|
11
|
+
"strict": true,
|
12
|
+
"resolveJsonModule": true
|
13
|
+
},
|
14
|
+
"include": ["src", "index.ts", "src/types"]
|
15
|
+
}
|