@ncds/ui-admin-icon 0.0.15 → 0.0.16

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 CHANGED
@@ -28,18 +28,25 @@ The default export is a dynamic Icon component that loads icons on demand:
28
28
 
29
29
  ```jsx
30
30
  import Icon from '@ncds/ui-admin-icon';
31
- import type { IconName } '@ncds/ui-admin-icon';
32
31
 
33
32
  function MyComponent() {
34
33
  return (
35
34
  <div>
36
35
  <Icon name="plus" />
37
- <Icon name="heart" width={24} height={24} fill="red" />
36
+ <Icon name="heart" width={24} height={24} color="red" />
38
37
  </div>
39
38
  );
40
39
  }
41
40
  ```
42
41
 
42
+ The type of `name` is defined in the package, so you can use it for type checking:
43
+
44
+ ```ts
45
+ import type { IconName } from '@ncds/ui-admin-icon';
46
+
47
+ const iconName: IconName = 'plus';
48
+ ```
49
+
43
50
  ### Importing individual icons
44
51
 
45
52
  For better performance, you can import specific icons directly:
@@ -51,7 +58,7 @@ function MyComponent() {
51
58
  return (
52
59
  <div>
53
60
  <Plus />
54
- <Heart width={24} height={24} fill="red" />
61
+ <Heart width={24} height={24} color="red" />
55
62
  </div>
56
63
  );
57
64
  }
package/dist/cjs/index.js CHANGED
@@ -39,21 +39,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
39
39
  const react_1 = __importStar(require("react"));
40
40
  const map_1 = require("./map");
41
41
  const Icon = ({ name, ...props }) => {
42
- const iconRef = (0, react_1.useRef)(null);
43
- const [isLoading, setIsLoading] = (0, react_1.useState)(true);
42
+ const IconComponentRef = (0, react_1.useRef)(null);
43
+ const [, forceUpdate] = react_1.default.useState({});
44
44
  (0, react_1.useEffect)(() => {
45
- setIsLoading(true);
45
+ IconComponentRef.current = null;
46
+ let isMounted = true;
46
47
  (0, map_1.getIcon)(name)
47
- ?.then((module) => {
48
- iconRef.current = module.default;
49
- setIsLoading(false);
48
+ ?.then((iconModule) => {
49
+ if (isMounted && iconModule?.default) {
50
+ IconComponentRef.current = iconModule.default;
51
+ forceUpdate({});
52
+ }
50
53
  })
51
54
  .catch((error) => {
52
- setIsLoading(false);
55
+ console.error(`Error loading icon: ${name}`, error);
53
56
  });
57
+ return () => {
58
+ isMounted = false;
59
+ };
54
60
  }, [name]);
55
- const IconComponent = iconRef.current;
56
- if (isLoading || !IconComponent)
61
+ const IconComponent = IconComponentRef.current;
62
+ if (!IconComponent)
57
63
  return null;
58
64
  return react_1.default.createElement(IconComponent, { ...props });
59
65
  };
package/dist/esm/index.js CHANGED
@@ -1,21 +1,27 @@
1
- import React, { useEffect, useState, useRef } from 'react';
1
+ import React, { useEffect, useRef } from 'react';
2
2
  import { getIcon } from './map';
3
3
  const Icon = ({ name, ...props }) => {
4
- const iconRef = useRef(null);
5
- const [isLoading, setIsLoading] = useState(true);
4
+ const IconComponentRef = useRef(null);
5
+ const [, forceUpdate] = React.useState({});
6
6
  useEffect(() => {
7
- setIsLoading(true);
7
+ IconComponentRef.current = null;
8
+ let isMounted = true;
8
9
  getIcon(name)
9
- ?.then((module) => {
10
- iconRef.current = module.default;
11
- setIsLoading(false);
10
+ ?.then((iconModule) => {
11
+ if (isMounted && iconModule?.default) {
12
+ IconComponentRef.current = iconModule.default;
13
+ forceUpdate({});
14
+ }
12
15
  })
13
16
  .catch((error) => {
14
- setIsLoading(false);
17
+ console.error(`Error loading icon: ${name}`, error);
15
18
  });
19
+ return () => {
20
+ isMounted = false;
21
+ };
16
22
  }, [name]);
17
- const IconComponent = iconRef.current;
18
- if (isLoading || !IconComponent)
23
+ const IconComponent = IconComponentRef.current;
24
+ if (!IconComponent)
19
25
  return null;
20
26
  return React.createElement(IconComponent, { ...props });
21
27
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ncds/ui-admin-icon",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "keywords": [
5
5
  "icons",
6
6
  "react",