import React , { forwardRef , useCallback } from "react" ; import type { ComponentPropsWithoutRef , ElementRef } from "react" ; import hoistNonReactStatics from "hoist-non-react-statics" ; import { withThemeFactory } from "./factory" ; import type { Composable , Styles , ThemeReducer } from "./factory" ; import { ThemeContext } from "../ThemeProvider/context" ; type WithTheme < C > = C & { theme : string ; } ; param theme param styles export function withTheme ( themeReducer , styles ) { return < C extends Composable > ( Composed ) : WithTheme < C > => { type Ref = ElementRef < C > ; type Props = ComponentPropsWithoutRef < C > ; const WithTheme = withThemeFactory < Ref , Props , typeof Composed > ( themeReducer , styles , Composed ) ; return hoistNonReactStatics ( Object . assign ( forwardRef < Ref , Props > ( ( props , ref ) => { const themeConsumer = useCallback ( ( theme ) => < WithTheme { ... props } theme = { theme } composedRef = { ref } / > , [ props , ref ] ) ; return < ThemeContext . Consumer children = { themeConsumer } />; } ) , { theme : styles . __digest } ) , Composed ) as unknown as WithTheme < C > ; } ; }