Skip to content

Commit 7415548

Browse files
authored
Merge pull request #578 from system-ui/next-box-sizing
Add default box-sizing styles
2 parents 3e76a8d + e380f01 commit 7415548

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

packages/docs/src/components/layout.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** @jsx jsx */
22
import { jsx, Styled, useColorMode } from 'theme-ui'
33
import { useState, useRef } from 'react'
4-
import { Global } from '@emotion/core'
54
import { Flex, Box } from '@theme-ui/components'
65
import { AccordionNav } from '@theme-ui/sidenav'
76
import { Link } from 'gatsby'
@@ -41,7 +40,10 @@ export default props => {
4140
const [menuOpen, setMenuOpen] = useState(false)
4241
const nav = useRef(null)
4342
const [mode, setMode] = useColorMode()
44-
const fullwidth = (props.pageContext.frontmatter && props.pageContext.frontmatter.fullwidth) || props.location.pathname === '/'
43+
const fullwidth =
44+
(props.pageContext.frontmatter &&
45+
props.pageContext.frontmatter.fullwidth) ||
46+
props.location.pathname === '/'
4547

4648
const cycleMode = e => {
4749
const i = modes.indexOf(mode)
@@ -52,24 +54,14 @@ export default props => {
5254
return (
5355
<Styled.root>
5456
<Head {...props} />
55-
<Global
56-
styles={{
57-
'*': {
58-
boxSizing: 'border-box',
59-
},
60-
body: {
61-
margin: 0,
62-
},
63-
}}
64-
/>
6557
<SkipLink>Skip to content</SkipLink>
6658
<Flex
6759
sx={{
6860
flexDirection: 'column',
6961
minHeight: '100vh',
7062
}}>
7163
<Flex
72-
as='header'
64+
as="header"
7365
sx={{
7466
height: 64,
7567
px: 3,
@@ -85,7 +77,9 @@ export default props => {
8577
if (navLink) navLink.focus()
8678
}}
8779
/>
88-
<Link to="/" sx={{ variant: 'links.nav' }}>Theme UI</Link>
80+
<Link to="/" sx={{ variant: 'links.nav' }}>
81+
Theme UI
82+
</Link>
8983
</Flex>
9084
<Flex>
9185
<NavLink href="https://github.com/system-ui/theme-ui">

packages/docs/src/gatsby-plugin-theme-ui/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,25 +224,26 @@ export default {
224224
},
225225
secondary: {
226226
color: 'background',
227-
bg: 'secondary'
227+
bg: 'secondary',
228228
},
229229
accent: {
230230
color: 'background',
231-
bg: 'accent'
231+
bg: 'accent',
232232
},
233233
highlight: {
234234
color: 'text',
235-
bg: 'highlight'
235+
bg: 'highlight',
236236
},
237237
},
238238
layout: {
239239
container: {
240240
p: 3,
241241
// maxWidth: 1024,
242-
}
242+
},
243243
},
244244
styles: {
245245
root: {
246+
margin: 0,
246247
fontFamily: 'body',
247248
lineHeight: 'body',
248249
fontWeight: 'body',

packages/docs/src/pages/theming.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ Flag | Default | Description
173173
`useBodyStyles` | `true` | Adds styles defined in `theme.styles.root` to the `<body>` element along with `color` and `background-color`
174174
`initialColorModeName` | `'default'` | The key used for the top-level color palette in `theme.colors`
175175
`useColorSchemeMediaQuery` | `false` | Initializes the color mode based on the `prefers-color-scheme` media query
176+
`useBoxSizing` | `true` | Adds a global `box-sizing: border-box` style
176177

177178
## Example Theme
178179

packages/theme-provider/src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const BodyStyles = () =>
99
styles: theme => {
1010
if (theme.useBodyStyles === false || (theme.styles && !theme.styles.root))
1111
return false
12+
const boxSizing = theme.useBorderBox === false ? null : 'border-box'
13+
1214
return css({
15+
'*': {
16+
boxSizing,
17+
},
1318
body: {
1419
variant: 'styles.root',
1520
},

packages/theme-provider/test/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ test('does not renders global styles', () => {
160160
fontWeights: {
161161
body: 500,
162162
},
163+
styles: {
164+
root: {
165+
fontFamily: 'body',
166+
},
167+
},
163168
}}>
164169
<h1>Hello</h1>
165170
</ThemeProvider>
@@ -169,3 +174,28 @@ test('does not renders global styles', () => {
169174
expect(style.fontWeight).toBe('')
170175
expect(style.lineHeight).toBe('')
171176
})
177+
178+
test('adds box-sizing: border-box', () => {
179+
const root = render(
180+
<ThemeProvider theme={{}}>
181+
<h1>Hello</h1>
182+
</ThemeProvider>
183+
)
184+
const style = window.getComputedStyle(root.baseElement)
185+
expect(style.boxSizing).toBe('border-box')
186+
})
187+
188+
test('does not add box-sizing: border-box', () => {
189+
const styles = [].slice.call(document.querySelectorAll('style'))
190+
styles.forEach(style => (style.innerHTML = ''))
191+
const root = render(
192+
<ThemeProvider
193+
theme={{
194+
useBorderBox: false,
195+
}}>
196+
<h1>Hello</h1>
197+
</ThemeProvider>
198+
)
199+
const style = window.getComputedStyle(root.baseElement)
200+
expect(style.boxSizing).toBe('')
201+
})

0 commit comments

Comments
 (0)