@@ -86,21 +86,8 @@ const alias = (n: ThemedComponentName): keyof JSX.IntrinsicElements =>
8686 isAlias ( n ) ? aliases [ n ] : n
8787
8888export const themed =
89- ( key : ThemedComponentName | ( string & { } ) ) =>
90- ( { theme, ...rest } : ThemedProps ) => {
91- const extraStyles : CSSObject = { }
92-
93- if ( key === 'th' || key === 'td' ) {
94- const { align } = rest as DetailedHTMLProps <
95- React . ThHTMLAttributes < HTMLTableHeaderCellElement > ,
96- HTMLTableHeaderCellElement
97- >
98-
99- if ( align !== 'char' ) extraStyles . textAlign = align
100- }
101-
102- return css ( { ...get ( theme , `styles.${ key } ` ) , ...extraStyles } ) ( theme )
103- }
89+ ( key : ThemedComponentName | ( string & { } ) ) => ( theme : Theme ) =>
90+ css ( get ( theme , `styles.${ key } ` ) ) ( theme )
10491
10592// opt out of typechecking whenever `as` prop is used
10693interface AnyComponentProps extends JSX . IntrinsicAttributes {
@@ -109,8 +96,11 @@ interface AnyComponentProps extends JSX.IntrinsicAttributes {
10996
11097export interface ThemedComponent < Name extends string > {
11198 (
112- props : Name extends keyof JSX . IntrinsicElements ? ComponentProps < Name > : { }
99+ props : ( Name extends keyof JSX . IntrinsicElements
100+ ? ComponentProps < Name >
101+ : { } ) & { css ?: CSSObject }
113102 ) : JSX . Element
103+ displayName : string
114104}
115105
116106export type ThemedComponentsDict = {
@@ -127,14 +117,30 @@ const createThemedComponent = <Name extends string>(
127117) : ThemedComponent < Name > => {
128118 const variantStyles = themed ( variant )
129119
130- return ( props ) => {
120+ const component : ThemedComponent < Name > = ( props ) => {
121+ const extraStyles : { textAlign ?: 'left' | 'right' | 'center' | 'justify' } =
122+ { }
123+
124+ if ( name === 'th' || name === 'td' ) {
125+ const { align } = props as DetailedHTMLProps <
126+ React . ThHTMLAttributes < HTMLTableHeaderCellElement > ,
127+ HTMLTableHeaderCellElement
128+ >
129+
130+ if ( align !== 'char' ) extraStyles . textAlign = align
131+ }
132+
131133 const css = ( props as any ) [ 'css' ]
132134
133135 return jsx ( name , {
134136 ...props ,
135- css : css ? [ css , variantStyles ] : variantStyles ,
137+ css : [ props . css , variantStyles , extraStyles ] . filter ( Boolean ) ,
136138 } )
137139 }
140+
141+ component . displayName = `Themed(${ name } )`
142+
143+ return component
138144}
139145
140146interface ThemedDivProps
@@ -167,10 +173,21 @@ const createComponents = (comps: MDXProviderComponents) => {
167173 // todo: test this behaviour
168174 componentKeys . forEach ( ( key ) => {
169175 const componentAtKey = comps [ key ]
176+
177+ console . log ( 'createComponents' , key , componentAtKey )
178+
170179 if ( componentAtKey ) {
171- next [ key ] = ( props ) => jsx ( componentAtKey , { ...props , css : themed ( key ) } )
180+ const component : ThemedComponent < string > = ( props ) => {
181+ console . log ( 'component render props=' , props )
182+ return jsx ( componentAtKey , { ...props , css : themed ( key ) } )
183+ }
184+
185+ component . displayName = "MdxComponents('" + key + "')"
186+
187+ next [ key ] = component
172188 }
173189 } )
190+
174191 return next
175192}
176193
0 commit comments