Skip to content

Commit 41ccf05

Browse files
committed
chore(components): fix themed()
1 parent b994c5e commit 41ccf05

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

packages/mdx/src/index.tsx

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,8 @@ const alias = (n: ThemedComponentName): keyof JSX.IntrinsicElements =>
8686
isAlias(n) ? aliases[n] : n
8787

8888
export 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
10693
interface AnyComponentProps extends JSX.IntrinsicAttributes {
@@ -109,8 +96,11 @@ interface AnyComponentProps extends JSX.IntrinsicAttributes {
10996

11097
export 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

116106
export 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

140146
interface 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

packages/mdx/test/index.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ expect.extend(matchers)
1717

1818
test('styles React components', () => {
1919
const Beep = (props: React.ComponentPropsWithoutRef<'h2'>) => (
20+
// eslint-disable-next-line jsx-a11y/heading-has-content
2021
<h2 {...props} />
2122
)
23+
2224
const Inner = (props: React.ComponentPropsWithoutRef<typeof Beep>) =>
2325
mdx('Beep', props)
2426

@@ -45,22 +47,9 @@ test('styles React components', () => {
4547
expect(json).toHaveStyleRule('color', 'tomato')
4648
})
4749

48-
test('cleans up style props', () => {
49-
const json = renderJSON(
50-
// @ts-expect-error
51-
<Themed.h1 mx={2} id="test">
52-
Hello
53-
</Themed.h1>
54-
)!
55-
expect(json.props.id).toBe('test')
56-
expect(json.props.mx).not.toBeDefined()
57-
})
58-
5950
test('themed extracts styles from the theme', () => {
6051
expect(
61-
themed('footer')({
62-
theme: { styles: { footer: { background: 'skyblue' } } },
63-
})
52+
themed('footer')({ styles: { footer: { background: 'skyblue' } } })
6453
).toStrictEqual({ background: 'skyblue' })
6554
})
6655

0 commit comments

Comments
 (0)