@ncds/ui-admin-icon 0.0.11 → 0.0.12

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/cjs/index.js CHANGED
@@ -40,20 +40,50 @@ const react_1 = __importStar(require("react"));
40
40
  const covertToPascalCase = (str) => {
41
41
  return str.replace(/-([a-z0-9])/g, (_, char) => char.toUpperCase()).replace(/^[a-z]/, (char) => char.toUpperCase());
42
42
  };
43
+ const viteLoader = (name) => {
44
+ const iconName = covertToPascalCase(name);
45
+ try {
46
+ return Promise.resolve(`${`./components/${iconName}`}`).then(s => __importStar(require(s))).then((module) => module.default);
47
+ }
48
+ catch {
49
+ return Promise.reject(new Error(`Icon ${name} not found`));
50
+ }
51
+ };
52
+ const webpackLoader = (name) => {
53
+ // @ts-ignore
54
+ const requireContext = require.context('./components', true, /\.(js|tsx)$/);
55
+ const iconName = covertToPascalCase(name);
56
+ try {
57
+ return Promise.resolve(requireContext(`./${iconName}`).default);
58
+ }
59
+ catch {
60
+ return Promise.reject(new Error(`Icon ${name} not found`));
61
+ }
62
+ };
63
+ const getIconLoader = () => {
64
+ // @ts-ignore
65
+ if (typeof require !== 'undefined' && typeof require.context === 'function') {
66
+ return webpackLoader;
67
+ }
68
+ return viteLoader;
69
+ };
70
+ const loadIcon = getIconLoader();
43
71
  const Icon = ({ name, ...props }) => {
44
72
  const iconRef = (0, react_1.useRef)(null);
45
- const [isLoaded, setIsLoaded] = (0, react_1.useState)(false);
73
+ const [isLoading, setIsLoading] = (0, react_1.useState)(true);
46
74
  (0, react_1.useEffect)(() => {
47
- const importIcon = async () => {
48
- setIsLoaded(false);
49
- const { default: IconComponent } = await Promise.resolve(`${`./components/${covertToPascalCase(name)}`}`).then(s => __importStar(require(s)));
50
- iconRef.current = IconComponent;
51
- setIsLoaded(true);
52
- };
53
- importIcon();
75
+ setIsLoading(true);
76
+ loadIcon(name)
77
+ .then((component) => {
78
+ iconRef.current = component;
79
+ setIsLoading(false);
80
+ })
81
+ .catch(() => {
82
+ setIsLoading(false);
83
+ });
54
84
  }, [name]);
55
85
  const IconComponent = iconRef.current;
56
- if (!isLoaded || !IconComponent)
86
+ if (isLoading || !IconComponent)
57
87
  return null;
58
88
  return react_1.default.createElement(IconComponent, { ...props });
59
89
  };
package/dist/esm/index.js CHANGED
@@ -1,21 +1,51 @@
1
- import React, { useEffect, useRef, useState } from 'react';
1
+ import React, { useEffect, useState, useRef } from 'react';
2
2
  const covertToPascalCase = (str) => {
3
3
  return str.replace(/-([a-z0-9])/g, (_, char) => char.toUpperCase()).replace(/^[a-z]/, (char) => char.toUpperCase());
4
4
  };
5
+ const viteLoader = (name) => {
6
+ const iconName = covertToPascalCase(name);
7
+ try {
8
+ return import(/* @vite-ignore */ `./components/${iconName}`).then((module) => module.default);
9
+ }
10
+ catch {
11
+ return Promise.reject(new Error(`Icon ${name} not found`));
12
+ }
13
+ };
14
+ const webpackLoader = (name) => {
15
+ // @ts-ignore
16
+ const requireContext = require.context('./components', true, /\.(js|tsx)$/);
17
+ const iconName = covertToPascalCase(name);
18
+ try {
19
+ return Promise.resolve(requireContext(`./${iconName}`).default);
20
+ }
21
+ catch {
22
+ return Promise.reject(new Error(`Icon ${name} not found`));
23
+ }
24
+ };
25
+ const getIconLoader = () => {
26
+ // @ts-ignore
27
+ if (typeof require !== 'undefined' && typeof require.context === 'function') {
28
+ return webpackLoader;
29
+ }
30
+ return viteLoader;
31
+ };
32
+ const loadIcon = getIconLoader();
5
33
  const Icon = ({ name, ...props }) => {
6
34
  const iconRef = useRef(null);
7
- const [isLoaded, setIsLoaded] = useState(false);
35
+ const [isLoading, setIsLoading] = useState(true);
8
36
  useEffect(() => {
9
- const importIcon = async () => {
10
- setIsLoaded(false);
11
- const { default: IconComponent } = await import(`./components/${covertToPascalCase(name)}`);
12
- iconRef.current = IconComponent;
13
- setIsLoaded(true);
14
- };
15
- importIcon();
37
+ setIsLoading(true);
38
+ loadIcon(name)
39
+ .then((component) => {
40
+ iconRef.current = component;
41
+ setIsLoading(false);
42
+ })
43
+ .catch(() => {
44
+ setIsLoading(false);
45
+ });
16
46
  }, [name]);
17
47
  const IconComponent = iconRef.current;
18
- if (!isLoaded || !IconComponent)
48
+ if (isLoading || !IconComponent)
19
49
  return null;
20
50
  return React.createElement(IconComponent, { ...props });
21
51
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ncds/ui-admin-icon",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "keywords": [
5
5
  "icons",
6
6
  "react",