Skip to content

Commit 8a7e411

Browse files
atanassterlachlanjchasparus
authored
chore: deprecate mdx/Styled (#1461)
* chore: deprecate mdx/Styled * Add CHANGELOG for making Styled backwards-compatible * Update CHANGELOG.md Co-authored-by: Piotr Monwid-Olechnowicz <[email protected]> * Update packages/mdx/src/index.ts Co-authored-by: Piotr Monwid-Olechnowicz <[email protected]> * Stop exporting warnStyled (this is not public API) * fix: add alias and tests for Styled * Remove extra CHANGELOG entry * Prefix warning with package name * Improve deprecation warning Co-authored-by: Lachlan Campbell <[email protected]> Co-authored-by: Piotr Monwid-Olechnowicz <[email protected]>
1 parent 5296afc commit 8a7e411

4 files changed

Lines changed: 77 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## v0.6.0 UNRELEASED
44

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

78
## v0.6.0-alpha.7 2021-02-15

packages/mdx/src/index.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
HTMLAttributes,
99
ElementType,
1010
ComponentProps,
11+
createElement,
12+
useEffect,
1113
} from 'react'
1214
import styled, { StyledComponent } from '@emotion/styled'
1315
import { MDXProvider as _MDXProvider, useMDXComponents } from '@mdx-js/react'
@@ -156,12 +158,34 @@ export const Themed: ThemedDiv & ThemedComponentsDict = styled('div')(
156158
themed('div')
157159
) as ThemedDiv & ThemedComponentsDict
158160

161+
/**
162+
* @deprecated since 0.6.0.
163+
*
164+
* `Styled` was renamed to `Themed` to avoid confusion with styled components.
165+
*/
166+
export const Styled: ThemedDiv & ThemedComponentsDict = styled('div')(
167+
themed('div')
168+
) as ThemedDiv & ThemedComponentsDict
169+
170+
const warnStyled = (tag: keyof IntrinsicSxElements): FC => (props) => {
171+
useEffect(() => {
172+
if (process.env.NODE_ENV !== 'production') {
173+
console.warn(
174+
'[theme-ui] The Styled component from "@theme-ui/mdx" is deprecated and will be removed in a future version. It has been renamed to Themed with the same API.'
175+
)
176+
}
177+
}, [])
178+
return createElement(alias(tag), props)
179+
}
180+
159181
export const components = {} as ThemedComponentsDict
160182

161183
tags.forEach((tag) => {
162184
// fixme?
163185
components[tag] = styled(alias(tag))(themed(tag)) as any
164186
Themed[tag] = components[tag] as any
187+
188+
Styled[tag] = styled(warnStyled(tag))(themed(tag)) as any
165189
})
166190

167191
const createComponents = (comps: MDXProviderComponents) => {

packages/mdx/test/index.tsx

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import React from 'react'
33
import { mdx } from '@mdx-js/react'
44
import { render } from '@testing-library/react'
55
import { matchers } from '@emotion/jest'
6+
import mockConsole from 'jest-mock-console'
67
import { ThemeProvider } from '@theme-ui/core'
78
import { renderJSON } from '@theme-ui/test-utils'
89

9-
import { themed, Themed, components, MDXProvider } from '../src'
10+
import { themed, Themed, Styled, components, MDXProvider } from '../src'
1011

1112
expect.extend(matchers)
1213

@@ -191,3 +192,52 @@ test('table columns align', () => {
191192
expect(tree.getByText('TextCenter')).toHaveStyleRule('text-align', 'center')
192193
expect(tree.getByText('TextRight')).toHaveStyleRule('text-align', 'right')
193194
})
195+
196+
test('Warn deprecated Styled', () => {
197+
const restore = mockConsole()
198+
const tree = render(
199+
<ThemeProvider
200+
theme={{
201+
styles: {
202+
h1: {
203+
color: 'tomato',
204+
},
205+
},
206+
}}>
207+
<MDXProvider>
208+
<Styled.inlineCode>styled</Styled.inlineCode>
209+
</MDXProvider>
210+
</ThemeProvider>
211+
)!
212+
const code = tree.getByText('styled')
213+
expect(code).toMatchInlineSnapshot(`
214+
<code
215+
class="emotion-0"
216+
>
217+
styled
218+
</code>
219+
`)
220+
expect(console.warn).toHaveBeenCalled()
221+
restore()
222+
})
223+
224+
test('Deprecated Styled test', () => {
225+
const json = renderJSON(
226+
<ThemeProvider
227+
theme={{
228+
styles: {
229+
h1: {
230+
color: 'tomato',
231+
},
232+
},
233+
}}>
234+
<MDXProvider>
235+
<Styled.h1>
236+
H1
237+
</Styled.h1>
238+
</MDXProvider>
239+
</ThemeProvider>
240+
)!
241+
expect(json.type).toBe('h1')
242+
expect(json).toHaveStyleRule('color', 'tomato')
243+
})

packages/theme-ui/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type {
1919
StylePropertyValue,
2020
} from '@theme-ui/core'
2121
export { useColorMode, InitializeColorMode } from '@theme-ui/color-modes'
22-
export { Themed, components } from '@theme-ui/mdx'
22+
export { Themed, Styled, components } from '@theme-ui/mdx'
2323
export { ThemeProvider } from '@theme-ui/theme-provider'
2424
export * from '@theme-ui/components'
2525
export { css, get } from '@theme-ui/css'

0 commit comments

Comments
 (0)