From 9572e1381db73b7094a30aa6d43b0622b27d4298 Mon Sep 17 00:00:00 2001 From: Nudesuppe42 Date: Wed, 31 Jul 2024 12:43:41 +0200 Subject: [PATCH] feat(components): :sparkles: DynamicIcon for relative tabler imports --- src/components/core/DynamicIcon.tsx | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/components/core/DynamicIcon.tsx diff --git a/src/components/core/DynamicIcon.tsx b/src/components/core/DynamicIcon.tsx new file mode 100644 index 0000000..4fdb97f --- /dev/null +++ b/src/components/core/DynamicIcon.tsx @@ -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>; +}) { + const Icon = icons[icon as keyof typeof icons]; + if (isValidIconName(icon)) { + return ; + } + + return Fallback ? : <>; +} + +function isValidIconName(icon: string): icon is keyof typeof icons { + return icon in icons; +}