Skip to content

Commit 4076e9a

Browse files
epilandehasparus
authored andcommitted
Convert @theme-ui/theme-provider to TypeScript
1 parent 93b9cfe commit 4076e9a

File tree

7 files changed

+37
-12
lines changed

7 files changed

+37
-12
lines changed

packages/core/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export const jsx: typeof React.createElement = (type, props, ...children) =>
4040
export interface ContextValue {
4141
__EMOTION_VERSION__: string
4242
theme: Theme | null
43+
colorMode?: string
44+
setColorMode?: () => void
4345
}
4446
export const Context = React.createContext<ContextValue>({
4547
__EMOTION_VERSION__,

packages/css/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as CSS from 'csstype'
22

33
import { SystemStyleObject, UseThemeFunction, Theme } from './types'
4+
import { SerializedStyles } from '@emotion/serialize'
45

56
export * from './types'
67

@@ -256,7 +257,7 @@ type CssPropsArgument = { theme: Theme } | Theme
256257

257258
export const css = (args: SystemStyleObject = {}) => (
258259
props: CssPropsArgument = {}
259-
): CSS.Properties => {
260+
) => {
260261
const theme: Theme = {
261262
...defaultTheme,
262263
...('theme' in props ? props.theme : props),
@@ -297,5 +298,5 @@ export const css = (args: SystemStyleObject = {}) => (
297298
}
298299
}
299300

300-
return result
301+
return result as SerializedStyles
301302
}

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: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
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 => {
14+
styles: (theme: Theme) => {
1015
if (theme.useBodyStyles === false || (theme.styles && !theme.styles.root))
1116
return false
1217
const boxSizing = theme.useBorderBox === false ? null : 'border-box'
@@ -23,7 +28,17 @@ const BodyStyles = () =>
2328
},
2429
})
2530

26-
export const ThemeProvider = ({ theme, components, children }) => {
31+
interface ThemeProviderProps {
32+
theme: Theme
33+
children?: React.ReactNode
34+
components?: { [key in keyof IntrinsicSxElements]?: React.ReactNode }
35+
}
36+
37+
export const ThemeProvider: React.FC<ThemeProviderProps> = ({
38+
theme,
39+
components,
40+
children,
41+
}) => {
2742
const outer = useThemeUI()
2843

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

packages/theme-provider/test/index.js renamed to packages/theme-provider/test/index.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/** @jsx jsx */
2-
import React from 'react'
32
import { jsx } from '@theme-ui/core'
43
import { mdx } from '@mdx-js/react'
54
import renderer from 'react-test-renderer'
@@ -15,7 +14,7 @@ const renderJSON = el => renderer.create(el).toJSON()
1514

1615
test('renders', () => {
1716
const json = renderJSON(
18-
<ThemeProvider>
17+
<ThemeProvider theme={{}}>
1918
<h1>Hello</h1>
2019
</ThemeProvider>
2120
)
@@ -29,6 +28,8 @@ test('renders with theme', () => {
2928
useCustomProperties: false,
3029
colors: {
3130
primary: 'tomato',
31+
background: 'white',
32+
text: 'black',
3233
},
3334
}}>
3435
<h1 sx={{ color: 'primary' }}>Hello</h1>
@@ -82,7 +83,7 @@ test('renders with nested provider', () => {
8283
})
8384

8485
test('renders with custom components', () => {
85-
const h1 = props => <pre {...props} />
86+
const h1 = (props: any) => <pre {...props} />
8687

8788
const json = renderJSON(
8889
<ThemeProvider
@@ -136,7 +137,7 @@ test('renders global styles', () => {
136137

137138
test('resets body margin', () => {
138139
const root = render(
139-
<ThemeProvider>
140+
<ThemeProvider theme={{}}>
140141
<h1>Hello</h1>
141142
</ThemeProvider>
142143
)

packages/theme-provider/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../core/tsconfig.json",
3+
"include": ["src/**/*.ts", "src/**/*.tsx"]
4+
}

0 commit comments

Comments
 (0)