-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Checkbox, Color, Combobox, Number, Style } from '@makeswift/runtime/controls'; | ||
|
||
import { Icon, IconNames } from '@/vibes/soul/primitives/icon'; | ||
import { runtime } from '~/lib/makeswift/runtime'; | ||
|
||
// Helper function to convert kebab-case to Title Case | ||
function toTitleCase(str: string): string { | ||
return str | ||
.split('-') | ||
.map((word) => word.charAt(0).toUpperCase() + word.slice(1)) | ||
.join(' '); | ||
} | ||
|
||
runtime.registerComponent( | ||
function MSIcon({ name = 'star', ...rest }) { | ||
return <Icon {...rest} name={name} />; | ||
}, | ||
{ | ||
type: 'icon', | ||
label: 'Primitives / Icon', | ||
props: { | ||
className: Style({ properties: [Style.Margin] }), | ||
size: Number({ | ||
label: 'Size', | ||
defaultValue: 24, | ||
}), | ||
color: Color(), | ||
name: Combobox({ | ||
label: 'Icon name', | ||
getOptions(query) { | ||
return IconNames.map((name) => ({ | ||
id: name, | ||
value: name, | ||
label: toTitleCase(name), | ||
})).filter( | ||
(option) => !query || option.label.toLowerCase().includes(query.toLowerCase()), | ||
); | ||
}, | ||
}), | ||
strokeWidth: Number({ | ||
label: 'Stroke width', | ||
defaultValue: 2, | ||
step: 0.1, | ||
}), | ||
absoluteStrokeWidth: Checkbox({ | ||
label: 'Absolute stroke width', | ||
defaultValue: false, | ||
}), | ||
}, | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,32 @@ | ||
import { icons } from 'lucide-react'; | ||
import clsx from 'clsx'; | ||
import { LucideProps } from 'lucide-react'; | ||
import dynamicIconImports from 'lucide-react/dynamicIconImports'; | ||
import { ComponentRef, forwardRef, lazy, Suspense, useMemo } from 'react'; | ||
|
||
export type IconProps = { | ||
name: keyof typeof icons; | ||
color?: string; | ||
size?: number; | ||
className?: string; | ||
}; | ||
export type IconName = keyof typeof dynamicIconImports; | ||
|
||
export const Icon = function Icon({ name, color, size, className }: IconProps) { | ||
const LucideIcon = icons[name]; | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
export const IconNames = Object.keys(dynamicIconImports) as IconName[]; | ||
|
||
return <LucideIcon color={color} size={size} strokeWidth={1} className={className} />; | ||
}; | ||
type IconProps = Omit<LucideProps, 'ref'> & { name: IconName }; | ||
|
||
export const Icon = forwardRef<ComponentRef<'svg'>, IconProps>( | ||
({ className, name, size = 24, ...props }, ref) => { | ||
const LucideIcon = useMemo(() => lazy(dynamicIconImports[name]), [name]); | ||
|
||
return ( | ||
<Suspense | ||
fallback={ | ||
<div | ||
className={clsx('animate-pulse rounded-full bg-contrast-100', className)} | ||
style={{ width: size, height: size }} | ||
/> | ||
} | ||
> | ||
<LucideIcon ref={ref} {...props} className={className} size={size} /> | ||
</Suspense> | ||
); | ||
}, | ||
); | ||
|
||
export default Icon; |