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

chore: deprecate mdx/Styled #1461

Merged
merged 13 commits into from
Mar 19, 2021
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v0.6.0 UNRELEASED

- Make the rename of `Styled` to `Themed` non-breaking. Add a deprecation warning on `Styled` until a future release. PR #1461
- Paragraph component's hardcoded responsive style has been removed (issue #1476)

## v0.6.0-alpha.7 2021-02-15
Expand Down Expand Up @@ -36,6 +37,7 @@

## v0.6.0-alpha.6 2021-01-22

- Skip `false` values before passing style objects to Emotion. Allow `false` as style property value in TS types. Issue #1297, PR #1460.
- **BREAKING**: Default `useColorSchemeMediaQuery` to `true`. Issue #624, PR #1373

**How to migrate?** Add `useColorSchemeMediaQuery: false` to your theme if you don't have this property.
Expand Down
24 changes: 24 additions & 0 deletions packages/mdx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
HTMLAttributes,
ElementType,
ComponentProps,
createElement,
useEffect,
} from 'react'
import styled, { StyledComponent } from '@emotion/styled'
import { MDXProvider as _MDXProvider, useMDXComponents } from '@mdx-js/react'
Expand Down Expand Up @@ -156,12 +158,34 @@ export const Themed: ThemedDiv & ThemedComponentsDict = styled('div')(
themed('div')
) as ThemedDiv & ThemedComponentsDict

/**
* @deprecated since 0.6.0.
*
* `Styled` was renamed to `Themed` to avoid confusion with styled components.
*/
export const Styled: ThemedDiv & ThemedComponentsDict = styled('div')(
themed('div')
) as ThemedDiv & ThemedComponentsDict

const warnStyled = (tag: keyof IntrinsicSxElements): FC => (props) => {
useEffect(() => {
if (process.env.NODE_ENV !== 'production') {
console.warn(
'The Styled component from "@theme-ui/mdx" is deprecated and will be removed in a future version, please use Themed instead'
)
}
}, [])
return createElement(alias(tag), props)
}

export const components = {} as ThemedComponentsDict

tags.forEach((tag) => {
// fixme?
components[tag] = styled(alias(tag))(themed(tag)) as any
Themed[tag] = components[tag] as any

Styled[tag] = styled(warnStyled(tag))(themed(tag)) as any
})

const createComponents = (comps: MDXProviderComponents) => {
Expand Down
52 changes: 51 additions & 1 deletion packages/mdx/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ 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'

import { themed, Themed, components, MDXProvider } from '../src'
import { themed, Themed, Styled, components, MDXProvider } from '../src'

expect.extend(matchers)

Expand Down Expand Up @@ -191,3 +192,52 @@ test('table columns align', () => {
expect(tree.getByText('TextCenter')).toHaveStyleRule('text-align', 'center')
expect(tree.getByText('TextRight')).toHaveStyleRule('text-align', 'right')
})

test('Warn deprecated Styled', () => {
const restore = mockConsole()
const tree = render(
<ThemeProvider
theme={{
styles: {
h1: {
color: 'tomato',
},
},
}}>
<MDXProvider>
<Styled.inlineCode>styled</Styled.inlineCode>
</MDXProvider>
</ThemeProvider>
)!
const code = tree.getByText('styled')
expect(code).toMatchInlineSnapshot(`
<code
class="emotion-0"
>
styled
</code>
`)
expect(console.warn).toHaveBeenCalled()
restore()
})

test('Deprecated Styled test', () => {
const json = renderJSON(
<ThemeProvider
theme={{
styles: {
h1: {
color: 'tomato',
},
},
}}>
<MDXProvider>
<Styled.h1>
H1
</Styled.h1>
</MDXProvider>
</ThemeProvider>
)!
expect(json.type).toBe('h1')
expect(json).toHaveStyleRule('color', 'tomato')
})
2 changes: 1 addition & 1 deletion packages/theme-ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type {
StylePropertyValue,
} from '@theme-ui/core'
export { useColorMode, InitializeColorMode } from '@theme-ui/color-modes'
export { Themed, components } from '@theme-ui/mdx'
export { Themed, Styled, components } from '@theme-ui/mdx'
export { ThemeProvider } from '@theme-ui/theme-provider'
export * from '@theme-ui/components'
export { css, get } from '@theme-ui/css'
Expand Down