77 ReactNode ,
88 DetailedHTMLProps ,
99 HTMLAttributes ,
10- ElementType ,
1110 ComponentProps ,
1211 useMemo ,
1312} from 'react'
@@ -86,21 +85,8 @@ const alias = (n: ThemedComponentName): keyof JSX.IntrinsicElements =>
8685 isAlias ( n ) ? aliases [ n ] : n
8786
8887export 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- }
88+ ( key : ThemedComponentName | ( string & { } ) ) => ( theme : Theme ) =>
89+ css ( get ( theme , `styles.${ key } ` ) ) ( theme )
10490
10591// opt out of typechecking whenever `as` prop is used
10692interface AnyComponentProps extends JSX . IntrinsicAttributes {
@@ -109,8 +95,11 @@ interface AnyComponentProps extends JSX.IntrinsicAttributes {
10995
11096export interface ThemedComponent < Name extends string > {
11197 (
112- props : Name extends keyof JSX . IntrinsicElements ? ComponentProps < Name > : { }
98+ props : ( Name extends keyof JSX . IntrinsicElements
99+ ? ComponentProps < Name >
100+ : { } ) & { css ?: CSSObject }
113101 ) : JSX . Element
102+ displayName : string
114103}
115104
116105export type ThemedComponentsDict = {
@@ -127,14 +116,30 @@ const createThemedComponent = <Name extends string>(
127116) : ThemedComponent < Name > => {
128117 const variantStyles = themed ( variant )
129118
130- return ( props ) => {
119+ const component : ThemedComponent < Name > = ( props ) => {
120+ const extraStyles : { textAlign ?: 'left' | 'right' | 'center' | 'justify' } =
121+ { }
122+
123+ if ( name === 'th' || name === 'td' ) {
124+ const { align } = props as DetailedHTMLProps <
125+ React . ThHTMLAttributes < HTMLTableHeaderCellElement > ,
126+ HTMLTableHeaderCellElement
127+ >
128+
129+ if ( align !== 'char' ) extraStyles . textAlign = align
130+ }
131+
131132 const css = ( props as any ) [ 'css' ]
132133
133134 return jsx ( name , {
134135 ...props ,
135- css : css ? [ css , variantStyles ] : variantStyles ,
136+ css : [ props . css , variantStyles , extraStyles ] . filter ( Boolean ) ,
136137 } )
137138 }
139+
140+ component . displayName = `Themed(${ name } )`
141+
142+ return component
138143}
139144
140145interface ThemedDivProps
@@ -167,10 +172,18 @@ const createComponents = (comps: MDXProviderComponents) => {
167172 // todo: test this behaviour
168173 componentKeys . forEach ( ( key ) => {
169174 const componentAtKey = comps [ key ]
175+
170176 if ( componentAtKey ) {
171- next [ key ] = ( props ) => jsx ( componentAtKey , { ...props , css : themed ( key ) } )
177+ const component : ThemedComponent < string > = ( props ) => {
178+ return jsx ( componentAtKey , { ...props , css : themed ( key ) } )
179+ }
180+
181+ component . displayName = "MdxComponents('" + key + "')"
182+
183+ next [ key ] = component
172184 }
173185 } )
186+
174187 return next
175188}
176189
0 commit comments