import React from 'react' import type { StaticConfig, ThemeableProps, ThemeProps } from '../types' import { Theme } from '../views/Theme' import { getDefaultProps } from './getDefaultProps' export function themeable any>( Component: ComponentType, staticConfig: Partial, optimize = false ) { const withThemeComponent = React.forwardRef(function WithTheme( props: ThemeableProps, ref ) { 'use no memo' const userDefaults = getDefaultProps(staticConfig, props.componentName) const defaultTheme = userDefaults?.theme const defaultResetTheme = userDefaults?.themeReset const { theme, componentName, themeReset, ...rest } = props let overriddenContextProps: object | undefined const context = staticConfig?.context if (context) { for (const key in context.props) { const val = props[key] if (val !== undefined) { overriddenContextProps = overriddenContextProps || {} overriddenContextProps[key] = val } } } const element = ( // @ts-expect-error its ok ) // we filter out the props here, why? // Theme internally avoids wrapping unless 'theme' in props // reason for this is to avoid wrapping every single component with span // *if* ever it themes once, it leaves the span, to avoid reparenting // its expected if users want to avoid re-parenting, they keep the theme prop // and just set it to null. but we need to "respect" that here by filtering // one example of a bug caused by not doing this is in