1+ ---
2+ import * as LucideIcons from ' lucide-astro' ;
3+ import * as SimpleIcons from ' simple-icons-astro' ;
4+
5+ // Props passed to the Icon component
6+ const { icon, iconType = ' lucide' , size = 24 , color = ' white' } = Astro .props ;
7+
8+ // Helper types to restrict icon name to valid keys
9+ type LucideIconKeys = keyof typeof LucideIcons ;
10+ type SimpleIconKeys = keyof typeof SimpleIcons ;
11+
12+ const toPascalCase = (str : string ) =>
13+ str .replace (/ (^ \w | -\w )/ g , (match ) => match .replace (' -' , ' ' ).toUpperCase ());
14+
15+ let IconComponent: any = null ;
16+
17+ if (iconType .toLowerCase () === ' lucide' ) {
18+ // Ensure icon is a valid key for LucideIcons and cast
19+ const lucideIconName = toPascalCase (icon ); // Convert lucideIconNames to PascalCase
20+ IconComponent = LucideIcons [lucideIconName as LucideIconKeys ];
21+ } else if (iconType .toLowerCase () === ' simple' ) {
22+ // Ensure icon is a valid key for SimpleIcons and cast
23+ const simpleIconName = toPascalCase (icon ); // Convert simpleIconNames to PascalCase
24+ IconComponent = SimpleIcons [simpleIconName as SimpleIconKeys ];
25+ }
26+
27+ // Handle the case when the icon is not found
28+ if (! IconComponent ) {
29+ console .error (` Icon ${icon } not found for ${iconType } ` );
30+ }
31+ ---
32+
33+ { iconType .toLowerCase () === ' lucide' && IconComponent && (
34+ <IconComponent color = { color } size = { size } />
35+ )}
36+
37+ { iconType .toLowerCase () === ' simple' && IconComponent && (
38+ <IconComponent color = { color } size = { size } />
39+ )}
0 commit comments