Skip to content

Commit 076cd7b

Browse files
authored
Merge pull request #708 from epilande/ts-theme-provider
Convert @theme-ui/theme-provider to TypeScript
2 parents 5d5108d + b5a664d commit 076cd7b

File tree

16 files changed

+86
-62
lines changed

16 files changed

+86
-62
lines changed

CHANGELOG.md

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

33
## Unreleased
44

5+
- Make ThemeProvider `theme` prop required
56
- Removes overriding property on editor combobox
67
- Adjust media query sort logic #600
78
- Fixed link to Gatsby Plugin page in `getting-started` page. Issue #602

examples/codesandbox-starter/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { jsx, Layout, Styled } from 'theme-ui'
66

77
function App() {
88
return (
9-
<ThemeProvider>
9+
<ThemeProvider theme={{}}>
1010
<Layout sx={{ p: 3 }}>
1111
<Reset />
1212
<Styled.h1 sx={{ color: 'primary', mb: 3 }}>Hello Theme UI</Styled.h1>

packages/color-modes/test/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ test('useColorMode updates color mode state', () => {
9090
)
9191
}
9292
const tree = render(
93-
<ThemeProvider>
93+
<ThemeProvider theme={{}}>
9494
<ColorModeProvider>
9595
<Button />
9696
</ColorModeProvider>
@@ -182,7 +182,7 @@ test('uses default mode', () => {
182182
return <button children="test" />
183183
}
184184
const tree = render(
185-
<ThemeProvider>
185+
<ThemeProvider theme={{}}>
186186
<ColorModeProvider>
187187
<Button />
188188
</ColorModeProvider>
@@ -200,7 +200,7 @@ test('initializes mode based on localStorage', () => {
200200
return <button children="test" />
201201
}
202202
const tree = render(
203-
<ThemeProvider>
203+
<ThemeProvider theme={{}}>
204204
<ColorModeProvider>
205205
<Button />
206206
</ColorModeProvider>
@@ -237,7 +237,7 @@ test('retains initial context', () => {
237237
return false
238238
}
239239
render(
240-
<ThemeProvider>
240+
<ThemeProvider theme={{}}>
241241
<ColorModeProvider>
242242
<Consumer />
243243
</ColorModeProvider>
@@ -340,7 +340,7 @@ test('does not initialize mode from prefers-color-scheme media query when useCol
340340
return false
341341
}
342342
render(
343-
<ThemeProvider>
343+
<ThemeProvider theme={{}}>
344344
<ColorModeProvider>
345345
<Consumer />
346346
</ColorModeProvider>
@@ -606,7 +606,7 @@ test('warns when localStorage is disabled', () => {
606606
}
607607

608608
render(
609-
<ThemeProvider>
609+
<ThemeProvider theme={{}}>
610610
<ColorModeProvider>
611611
<Consumer />
612612
</ColorModeProvider>

packages/core/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ export const jsx: typeof React.createElement = (type, props, ...children) =>
3939

4040
export interface ContextValue {
4141
__EMOTION_VERSION__: string
42-
theme: Theme | null
42+
theme: Theme
43+
colorMode?: string
44+
setColorMode?: () => void
4345
}
4446
export const Context = React.createContext<ContextValue>({
4547
__EMOTION_VERSION__,
46-
theme: null,
48+
theme: {},
4749
})
4850

4951
export const useThemeUI = () => React.useContext(Context)
@@ -76,7 +78,7 @@ interface BaseProviderProps {
7678
const BaseProvider: React.FC<BaseProviderProps> = ({ context, children }) =>
7779
jsx(
7880
EmotionContext.Provider,
79-
{ value: context.theme! },
81+
{ value: context.theme },
8082
jsx(Context.Provider, {
8183
value: context,
8284
children,
@@ -103,7 +105,7 @@ export function ThemeProvider({ theme, children }: ThemeProviderProps) {
103105

104106
const context =
105107
typeof theme === 'function'
106-
? { ...outer, theme: theme(outer.theme!) }
108+
? { ...outer, theme: theme(outer.theme) }
107109
: merge.all<ContextValue>({}, outer, { theme })
108110

109111
return jsx(BaseProvider, { context }, children)

packages/core/test/index.js

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ import renderer from 'react-test-renderer'
44
import { render, fireEvent, cleanup, act } from '@testing-library/react'
55
import { matchers } from 'jest-emotion'
66
import mockConsole from 'jest-mock-console'
7-
import {
8-
jsx,
9-
Context,
10-
useThemeUI,
11-
merge,
12-
ThemeProvider,
13-
} from '../src'
7+
import { jsx, Context, useThemeUI, merge, ThemeProvider } from '../src'
148

159
afterEach(cleanup)
1610

@@ -21,7 +15,7 @@ const renderJSON = el => renderer.create(el).toJSON()
2115
describe('ThemeProvider', () => {
2216
test('renders', () => {
2317
const json = renderJSON(
24-
<ThemeProvider>
18+
<ThemeProvider theme={{}}>
2519
<h1>Hello</h1>
2620
</ThemeProvider>
2721
)
@@ -35,7 +29,7 @@ describe('ThemeProvider', () => {
3529
value={{
3630
emotionVersion: '9.0.0',
3731
}}>
38-
<ThemeProvider>Conflicting versions</ThemeProvider>
32+
<ThemeProvider theme={{}}>Conflicting versions</ThemeProvider>
3933
</Context.Provider>
4034
)
4135
expect(console.warn).toBeCalled()
@@ -104,20 +98,21 @@ describe('ThemeProvider', () => {
10498
cards: {
10599
default: {
106100
border: t => `1px solid ${t.colors.primary}`,
107-
}
108-
}
101+
},
102+
},
109103
}
110104
const json = renderJSON(
111-
jsx(ThemeProvider, { theme },
105+
jsx(
106+
ThemeProvider,
107+
{ theme },
112108
jsx('div', {
113109
sx: {
114110
variant: 'cards.default',
115-
}
111+
},
116112
})
117113
)
118114
)
119115
expect(json).toHaveStyleRule('border', '1px solid tomato')
120-
121116
})
122117
})
123118

@@ -151,13 +146,15 @@ describe('jsx', () => {
151146

152147
test('css prop accepts functions', () => {
153148
const json = renderJSON(
154-
jsx(ThemeProvider, {
155-
theme: {
156-
colors: {
157-
primary: 'tomato',
158-
}
159-
}
160-
},
149+
jsx(
150+
ThemeProvider,
151+
{
152+
theme: {
153+
colors: {
154+
primary: 'tomato',
155+
},
156+
},
157+
},
161158
jsx('div', {
162159
css: t => ({
163160
color: t.colors.primary,
@@ -338,7 +335,7 @@ describe('useThemeUI', () => {
338335
theme={{
339336
colors: {
340337
text: 'tomato',
341-
}
338+
},
342339
}}>
343340
<GetContext />
344341
</ThemeProvider>
@@ -347,4 +344,3 @@ describe('useThemeUI', () => {
347344
expect(context.theme.colors.text).toBe('tomato')
348345
})
349346
})
350-

packages/css/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"types": "dist/index.d.ts",
88
"sideEffects": false,
99
"scripts": {
10-
"prepare": "microbundle --no-compress",
11-
"watch": "microbundle watch --no-compress"
10+
"prepare": "microbundle --no-compress --tsconfig tsconfig.json",
11+
"watch": "microbundle watch --no-compress --tsconfig tsconfig.json"
1212
},
1313
"author": "Brent Jackson",
1414
"license": "MIT",

packages/css/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as CSS from 'csstype'
2-
3-
import { SystemStyleObject, UseThemeFunction, Theme } from './types'
1+
import { CSSObject, SystemStyleObject, UseThemeFunction, Theme } from './types'
42

53
export * from './types'
64

@@ -256,7 +254,7 @@ type CssPropsArgument = { theme: Theme } | Theme
256254

257255
export const css = (args: SystemStyleObject = {}) => (
258256
props: CssPropsArgument = {}
259-
): CSS.Properties => {
257+
): CSSObject => {
260258
const theme: Theme = {
261259
...defaultTheme,
262260
...('theme' in props ? props.theme : props),

packages/docs/src/pages/guides/mdx-layout-components.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ As an example, create a new component with the `ThemeProvider` and a wrapping `<
1818
import { jsx, ThemeProvider } from 'theme-ui'
1919

2020
export default props => (
21-
<ThemeProvider>
21+
<ThemeProvider theme={{}}>
2222
<div {...props} />
2323
</ThemeProvider>
2424
)

packages/theme-provider/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
"version": "0.4.0-alpha.1",
44
"main": "dist/index.js",
55
"module": "dist/index.esm.js",
6+
"source": "src/index.ts",
7+
"types": "dist/index.d.ts",
68
"sideEffects": false,
79
"scripts": {
8-
"prepare": "microbundle --no-compress",
9-
"watch": "microbundle watch --no-compress"
10+
"prepare": "microbundle --no-compress --tsconfig tsconfig.json",
11+
"watch": "microbundle watch --no-compress --tsconfig tsconfig.json"
1012
},
1113
"dependencies": {
1214
"@emotion/core": "^10.0.0",

packages/theme-provider/src/index.js renamed to packages/theme-provider/src/index.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
import { jsx, useThemeUI, ThemeProvider as CoreProvider } from '@theme-ui/core'
2-
import { css } from '@theme-ui/css'
1+
import {
2+
jsx,
3+
useThemeUI,
4+
ThemeProvider as CoreProvider,
5+
IntrinsicSxElements,
6+
} from '@theme-ui/core'
7+
import { css, Theme } from '@theme-ui/css'
38
import { ColorModeProvider } from '@theme-ui/color-modes'
49
import { MDXProvider } from '@theme-ui/mdx'
510
import { Global } from '@emotion/core'
611

712
const BodyStyles = () =>
813
jsx(Global, {
9-
styles: theme => {
10-
if (theme.useBodyStyles === false || (theme.styles && !theme.styles.root))
14+
styles: emotionTheme => {
15+
const theme = emotionTheme as Theme
16+
if (
17+
theme.useBodyStyles === false ||
18+
(theme.styles && !theme.styles.root)
19+
) {
1120
return false
12-
const boxSizing = theme.useBorderBox === false ? null : 'border-box'
21+
}
22+
const boxSizing = theme.useBorderBox === false ? undefined : 'border-box'
1323

1424
return css({
1525
'*': {
@@ -23,7 +33,17 @@ const BodyStyles = () =>
2333
},
2434
})
2535

26-
export const ThemeProvider = ({ theme, components, children }) => {
36+
interface ThemeProviderProps {
37+
theme: Theme
38+
children?: React.ReactNode
39+
components?: { [key in keyof IntrinsicSxElements]?: React.ReactNode }
40+
}
41+
42+
export const ThemeProvider: React.FC<ThemeProviderProps> = ({
43+
theme,
44+
components,
45+
children,
46+
}) => {
2747
const outer = useThemeUI()
2848

2949
if (typeof outer.setColorMode === 'function') {

0 commit comments

Comments
 (0)