import { type PolymorphicComponentProps } from '@kalink-ui/dibbly'; import { clsx } from 'clsx'; import { type ElementType } from 'react'; import { extractSprinklesProps, type GetSprinkles, type SprinklesFnBase, } from '../../styles'; /** * When you use `SeedProps` in other components and want to define a default * value for that prop in your component (e.g. `const { use = 'span' } = props;`) * you will need to cast the `use` prop as `YourComponentProps['use]` otherwise * it will be considered as `TUse | span` and you will get a type error. */ export type SeedProps< TUse extends ElementType, TSprinklesFn extends SprinklesFnBase, > = PolymorphicComponentProps & GetSprinkles; export interface CreateSeedParams { sprinkles: SprinklesFn; defaultClassName?: string; } export function plantSeed({ sprinkles, defaultClassName, }: CreateSeedParams) { const Seed = ( props: SeedProps, ) => { const { use: Comp = 'div', className, ...rest } = props; const [sprinklesProps, componentProps] = extractSprinklesProps( rest, sprinkles, ); return ( ); }; return Seed; } /** * A higher-order component that injects sprinkles and default className. */ export function withSeed({ sprinkles, defaultClassName, }: CreateSeedParams) { return function wrapWithSprinkles( WrappedComponent: ElementType & TProps>, ) { const ComponentWithSeed = ( props: SeedProps & TProps, ) => { const { className, ...rest } = props; const [sprinklesProps, componentProps] = extractSprinklesProps( rest, sprinkles, ); return ( // @ts-expect-error - TODO: fix this ); }; return ComponentWithSeed; }; }