Skip to content

Commit beb1772

Browse files
committed
Adjust body styles API
1 parent 7f6331a commit beb1772

File tree

2 files changed

+36
-40
lines changed

2 files changed

+36
-40
lines changed

packages/theme-provider/src/index.js

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,46 @@
1-
import {
2-
jsx,
3-
useThemeUI,
4-
ThemeProvider as CoreProvider,
5-
} from '@theme-ui/core'
1+
import { jsx, useThemeUI, ThemeProvider as CoreProvider } from '@theme-ui/core'
2+
import { css } from '@theme-ui/css'
63
import { ColorModeProvider } from '@theme-ui/color-modes'
74
import { MDXProvider } from '@theme-ui/mdx'
85
import { Global } from '@emotion/core'
96

10-
const BodyStyles = ({ theme = {} }) => {
11-
if (theme.useBodyStyles === false) return false
12-
13-
return jsx(Global, {
7+
const BodyStyles = () =>
8+
jsx(Global, {
149
styles: theme => {
15-
const fontFamily = theme.fonts && theme.fonts.body
16-
const fontWeight = theme.fontWeights && theme.fontWeights.body
17-
const lineHeight = theme.lineHeights && theme.lineHeights.body
18-
19-
return {
10+
if (theme.useBodyStyles === false || (theme.styles && !theme.styles.root))
11+
return false
12+
return css({
2013
body: {
21-
fontFamily,
22-
fontWeight,
23-
lineHeight,
24-
}
25-
}
26-
}
14+
variant: 'styles.root',
15+
},
16+
})(theme)
17+
},
2718
})
28-
}
2919

30-
export const ThemeProvider = ({
31-
theme,
32-
components,
33-
children
34-
}) => {
20+
export const ThemeProvider = ({ theme, components, children }) => {
3521
const outer = useThemeUI()
3622

3723
if (typeof outer.setColorMode === 'function') {
38-
return jsx(CoreProvider, { theme },
24+
return jsx(
25+
CoreProvider,
26+
{ theme },
3927
jsx(MDXProvider, {
4028
components,
41-
children
29+
children,
4230
})
4331
)
4432
}
4533

46-
return jsx(CoreProvider, { theme },
47-
jsx(ColorModeProvider, null,
48-
jsx(BodyStyles, { theme }),
34+
return jsx(
35+
CoreProvider,
36+
{ theme },
37+
jsx(
38+
ColorModeProvider,
39+
null,
40+
jsx(BodyStyles),
4941
jsx(MDXProvider, {
5042
components,
51-
children
43+
children,
5244
})
5345
)
5446
)

packages/theme-provider/test/index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ test('renders with theme', () => {
2828
theme={{
2929
useCustomProperties: false,
3030
colors: {
31-
primary: 'tomato'
32-
}
31+
primary: 'tomato',
32+
},
3333
}}>
3434
<h1 sx={{ color: 'primary' }}>Hello</h1>
3535
</ThemeProvider>
@@ -71,8 +71,8 @@ test('renders with nested provider', () => {
7171
styles: {
7272
h1: {
7373
color: 'cyan',
74-
}
75-
}
74+
},
75+
},
7676
}}>
7777
{mdx('h1', null, 'Hello')}
7878
</ThemeProvider>
@@ -117,6 +117,13 @@ test('renders global styles', () => {
117117
fontWeights: {
118118
body: 500,
119119
},
120+
styles: {
121+
root: {
122+
fontFamily: 'body',
123+
fontWeight: 'body',
124+
lineHeight: 'body',
125+
},
126+
},
120127
}}>
121128
<h1>Hello</h1>
122129
</ThemeProvider>
@@ -129,9 +136,7 @@ test('renders global styles', () => {
129136

130137
test('does not render invalid global styles', () => {
131138
const root = render(
132-
<ThemeProvider
133-
theme={{
134-
}}>
139+
<ThemeProvider theme={{}}>
135140
<h1>Hello</h1>
136141
</ThemeProvider>
137142
)
@@ -164,4 +169,3 @@ test('does not renders global styles', () => {
164169
expect(style.fontWeight).toBe('')
165170
expect(style.lineHeight).toBe('')
166171
})
167-

0 commit comments

Comments
 (0)