"use client" import { forwardRef, useMemo } from "react" import { createContext } from "../create-context" import { mergeProps } from "../merge-props" import { cx } from "../utils" import type { SystemStyleObject } from "./css.types" import { EMPTY_SLOT_STYLES } from "./empty" import { chakra } from "./factory" import type { JsxFactoryOptions } from "./factory.types" import type { ConfigRecipeSlots } from "./generated/recipes.gen" import type { SystemSlotRecipeFn } from "./recipe.types" import { type SlotRecipeKey, type UseSlotRecipeOptions, useSlotRecipe, } from "./use-slot-recipe" interface WrapElementProps

{ wrapElement?(element: React.ReactElement, props: P): React.ReactElement } export interface WithRootProviderOptions

extends WrapElementProps

{ defaultProps?: Partial

| undefined } export interface WithProviderOptions

extends JsxFactoryOptions

, WrapElementProps

{} export interface WithContextOptions

extends JsxFactoryOptions

{} const upperFirst = (str: string) => str.charAt(0).toUpperCase() + str.slice(1) export const createSlotRecipeContext = ( options: UseSlotRecipeOptions, ) => { const { key: recipeKey, recipe: recipeConfig } = options const contextName = upperFirst( recipeKey || (recipeConfig as any).className || "Component", ) const [StylesProvider, useStyles] = createContext< Record >({ name: `${contextName}StylesContext`, errorMessage: `use${contextName}Styles returned is 'undefined'. Seems you forgot to wrap the components in "<${contextName}.Root />" `, }) const [ClassNamesProvider, useClassNames] = createContext< Record >({ name: `${contextName}ClassNameContext`, errorMessage: `use${contextName}ClassNames returned is 'undefined'. Seems you forgot to wrap the components in "<${contextName}.Root />" `, strict: false, }) const [PropsProvider, usePropsContext] = createContext>({ strict: false, name: `${contextName}PropsContext`, providerName: `${contextName}PropsContext`, defaultValue: {}, }) function useRecipeResult(props: any) { const { unstyled, ...restProps } = props const slotRecipe = useSlotRecipe({ key: recipeKey, recipe: restProps.recipe || recipeConfig, }) as SystemSlotRecipeFn // @ts-ignore const [variantProps, otherProps] = useMemo( () => slotRecipe.splitVariantProps(restProps), [restProps, slotRecipe], ) const styles = useMemo( () => (unstyled ? EMPTY_SLOT_STYLES : slotRecipe(variantProps)), [unstyled, variantProps, slotRecipe], ) return { styles: styles as Record, classNames: slotRecipe.classNameMap as Record, props: otherProps, } } function withRootProvider

( Component: React.ElementType, options: WithRootProviderOptions

= {}, ): React.FC> { const { defaultProps } = options const StyledComponent = (inProps: any) => { const propsContext = usePropsContext() const props = useMemo( () => mergeProps(defaultProps, propsContext, inProps), [propsContext, inProps], ) const { styles, classNames, props: rootProps } = useRecipeResult(props) return ( ) } // @ts-expect-error StyledComponent.displayName = Component.displayName || Component.name return StyledComponent as any } const withProvider = ( Component: React.ElementType, slot: R extends keyof ConfigRecipeSlots ? ConfigRecipeSlots[R] : string, options?: WithProviderOptions

, ): React.ForwardRefExoticComponent< React.PropsWithoutRef

& React.RefAttributes > => { const { defaultProps, ...restOptions } = options ?? {} const SuperComponent = chakra(Component, {}, restOptions as any) const StyledComponent = forwardRef((inProps, ref) => { const propsContext = usePropsContext() const props = useMemo( () => mergeProps(defaultProps ?? {}, propsContext, inProps), [propsContext, inProps], ) const { styles, props: rootProps, classNames } = useRecipeResult(props) const className = classNames[slot as keyof typeof classNames] const element = ( ) return options?.wrapElement?.(element, props) ?? element }) // @ts-expect-error StyledComponent.displayName = Component.displayName || Component.name return StyledComponent as any } const withContext = ( Component: React.ElementType, slot?: R extends keyof ConfigRecipeSlots ? ConfigRecipeSlots[R] : string, options?: WithContextOptions

, ): React.ForwardRefExoticComponent< React.PropsWithoutRef

& React.RefAttributes > => { const SuperComponent = chakra(Component, {}, options as any) const StyledComponent = forwardRef((props, ref) => { const { unstyled, ...restProps } = props const styles = useStyles() const classNames = useClassNames() const className = classNames?.[slot as keyof typeof classNames] return ( ) }) // @ts-expect-error StyledComponent.displayName = Component.displayName || Component.name return StyledComponent as any } return { StylesProvider, ClassNamesProvider, PropsProvider, usePropsContext, useRecipeResult, withProvider, withContext, withRootProvider, useStyles, useClassNames, } }