Skip to content

Commit 611df50

Browse files
authored
Merge pull request #541 from lachlanjc/components-grid-columns
Add custom columns support to Grid component
2 parents 274514e + fae091e commit 611df50

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- `@theme-ui/components`: on Grid component, allow custom `columns` definitions via strings #541
6+
57
## v0.2.53 2019-12-19
68

79
- `@theme-ui/color`: add `transparentize` function #370

packages/components/src/Grid.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ const widthToColumns = width =>
99
: !!width && `repeat(auto-fit, minmax(${px(width)}, 1fr))`
1010

1111
const countToColumns = n =>
12-
Array.isArray(n) ? n.map(countToColumns) : !!n && `repeat(${n}, 1fr)`
12+
Array.isArray(n)
13+
? n.map(countToColumns)
14+
: !!n && (typeof n === 'number' ? `repeat(${n}, 1fr)` : n)
1315

1416
export const Grid = React.forwardRef(
1517
({ width, columns, gap = 3, ...props }, ref) => {

packages/components/test/__snapshots__/index.js.snap

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,41 @@ exports[`Grid renders with columns prop 1`] = `
584584
/>
585585
`;
586586

587+
exports[`Grid renders with mixed columns prop 1`] = `
588+
.emotion-0 {
589+
box-sizing: border-box;
590+
margin: 0;
591+
min-width: 0;
592+
display: grid;
593+
grid-gap: 16px;
594+
grid-template-columns: 1fr 2fr;
595+
}
596+
597+
<div
598+
className="emotion-0"
599+
/>
600+
`;
601+
602+
exports[`Grid renders with mixed columns prop 2`] = `
603+
.emotion-0 {
604+
box-sizing: border-box;
605+
margin: 0;
606+
min-width: 0;
607+
display: grid;
608+
grid-gap: 16px;
609+
}
610+
611+
@media screen and (min-width:40em) {
612+
.emotion-0 {
613+
grid-template-columns: 1fr 2fr;
614+
}
615+
}
616+
617+
<div
618+
className="emotion-0"
619+
/>
620+
`;
621+
587622
exports[`Grid renders with responsive columns prop 1`] = `
588623
.emotion-0 {
589624
box-sizing: border-box;

packages/components/test/index.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,20 @@ describe('Grid', () => {
220220
expect(json).toMatchSnapshot()
221221
})
222222

223+
test('renders with mixed columns prop', () => {
224+
const json = renderJSON(<Grid columns="1fr 2fr" />)
225+
expect(json).toMatchSnapshot()
226+
})
227+
223228
test('renders with responsive columns prop', () => {
224229
const json = renderJSON(<Grid columns={[2, 3, 4]} />)
225230
expect(json).toMatchSnapshot()
226231
})
232+
233+
test('renders with mixed columns prop', () => {
234+
const json = renderJSON(<Grid columns={[null, '1fr 2fr']} />)
235+
expect(json).toMatchSnapshot()
236+
})
227237
})
228238

229239
describe('Heading', () => {
@@ -305,10 +315,7 @@ describe('Select', () => {
305315
test('renders with style props', () => {
306316
const json = renderJSON(
307317
<ThemeProvider theme={theme}>
308-
<Select
309-
mb={3}
310-
value='hello'
311-
/>
318+
<Select mb={3} value="hello" />
312319
</ThemeProvider>
313320
)
314321
expect(json).toMatchSnapshot()

packages/docs/src/pages/components/grid.mdx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ import { Grid } from '@theme-ui/components'
3030
</Grid>
3131
```
3232
33+
```jsx live=true
34+
<Grid
35+
gap={2}
36+
columns={[2, '1fr 2fr']}>
37+
<Box bg='primary'>Box</Box>
38+
<Box bg='muted'>Box</Box>
39+
<Box bg='primary'>Box</Box>
40+
<Box bg='muted'>Box</Box>
41+
</Grid>
42+
```
43+
3344
## Props
3445
3546
The `Grid` component includes custom props for adjusting the grid layout.
@@ -38,8 +49,8 @@ Each prop can use the [responsive array][] syntax for mobile-first responsive st
3849
Prop | Description
3950
---|---
4051
`width` | Minimum width of child elements
41-
`columns` | Number of columns to use for the layout (cannot be used in conjunction with the `width` prop)
4252
`gap` | Space between child elements
53+
`columns` | Number of (equally-sized) columns, or string with [grid syntax](https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns) for the layout (cannot be used in conjunction with the `width` prop)
4354
4455
[responsive array]: /sx-prop#responsive-values
4556

0 commit comments

Comments
 (0)