Skip to content

Commit

Permalink
feat(components): ✨ DynamicIcon for relative tabler imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Nudelsuppe42 committed Jul 31, 2024
1 parent 3f0094e commit 9572e13
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/components/core/DynamicIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Icon, IconProps, icons } from "@tabler/icons-react";
import { ForwardRefExoticComponent, RefAttributes } from "react";

export default function DynamicIcon({
icon,
fallback: Fallback,
}: {
icon: string;
fallback?: ForwardRefExoticComponent<IconProps & RefAttributes<Icon>>;
}) {
const Icon = icons[icon as keyof typeof icons];
if (isValidIconName(icon)) {
return <Icon />;
}

return Fallback ? <Fallback /> : <></>;
}

function isValidIconName(icon: string): icon is keyof typeof icons {
return icon in icons;
}

0 comments on commit 9572e13

Please sign in to comment.