Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mdx): add .sx props to Themed.X styles #2250

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions packages/mdx/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @jsx jsx */
import { jsx, IntrinsicSxElements } from '@theme-ui/core'
import { css, CSSObject, get, Theme } from '@theme-ui/css'
import { jsx, IntrinsicSxElements, SxProp } from '@theme-ui/core'
import { css, get, Theme } from '@theme-ui/css'
import {
ComponentType,
FC,
Expand Down Expand Up @@ -88,16 +88,10 @@ export const themed =
(key: ThemedComponentName | (string & {})) => (theme: Theme) =>
css(get(theme, `styles.${key}`))(theme)

// opt out of typechecking whenever `as` prop is used
interface AnyComponentProps extends JSX.IntrinsicAttributes {
[key: string]: unknown
}

export interface ThemedComponent<Name extends string> {
(
props: (Name extends keyof JSX.IntrinsicElements
? ComponentProps<Name>
: {}) & { css?: CSSObject }
props: SxProp &
(Name extends keyof JSX.IntrinsicElements ? ComponentProps<Name> : {})
): JSX.Element
displayName: string
}
Expand Down Expand Up @@ -129,8 +123,6 @@ const createThemedComponent = <Name extends string>(
if (align !== 'char') extraStyles.textAlign = align
}

const css = (props as any)['css']

return jsx(name, {
...props,
css: [props.css, variantStyles, extraStyles].filter(Boolean),
Expand Down
14 changes: 13 additions & 1 deletion packages/mdx/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import React from 'react'
import { mdx } from '@mdx-js/react'
import { render } from '@testing-library/react'
import { matchers } from '@emotion/jest'
import mockConsole from 'jest-mock-console'
import { ThemeProvider } from '@theme-ui/core'
import { renderJSON } from '@theme-ui/test-utils'

Expand Down Expand Up @@ -47,6 +46,19 @@ test('styles React components', () => {
expect(json).toHaveStyleRule('color', 'tomato')
})

test('Themed.div accepts .sx prop', async () => {
const tree = render(
<ThemeProvider theme={{ colors: { primary: 'blue' } }}>
<Themed.div sx={{ color: 'primary' }}>blue text</Themed.div>
</ThemeProvider>
)!

const div = await tree.findByText('blue text')
const style = global.getComputedStyle(div)

expect(style.color).toBe('blue')
})

test('themed extracts styles from the theme', () => {
expect(
themed('footer')({ styles: { footer: { background: 'skyblue' } } })
Expand Down