Skip to content

Commit

Permalink
feat(Icon): dynamically import icons
Browse files Browse the repository at this point in the history
  • Loading branch information
soujvnunes committed Mar 2, 2024
1 parent db8d75d commit 751acb6
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/components/Icon.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
import icons, { type Icons } from "consts/icons";
import { memo, useEffect, useRef, useState } from "react";
import type { IconsValue, Icons } from "consts/icons";
import cn from "helpers/cn";

interface IconProps
extends Pick<React.ComponentPropsWithoutRef<"svg">, "className" | "viewBox"> {
name: keyof Icons;
}

export default function Icon({ name, className, ...props }: IconProps) {
const paths = icons[name];
export default memo(function Icon({ name, className, ...props }: IconProps) {
const [path, setPath] = useState<IconsValue>([]);
const isMounted = useRef(true);

useEffect(() => {
if (isMounted.current) {
void (async function handlePath() {
setPath((await import("consts/icons")).default[name]);
})();
}

return () => {
isMounted.current = false;
};
}, [name]);

return (
<svg
Expand All @@ -18,9 +32,9 @@ export default function Icon({ name, className, ...props }: IconProps) {
className={cn("w-6 h-6 fill-current", className)}
{...props}
>
{paths.map((path) => (
{path.map((path) => (
<path key={path.d} {...path} />
))}
</svg>
);
}
});

0 comments on commit 751acb6

Please sign in to comment.