Skip to content

Commit eb9e360

Browse files
author
Luke Bowerman
authored
fix(Grid): Default to 100% width
Grid now defaults to 100% width so that when it's composed within other layout components (Space / SpaceVertical) it will "fill" its container without developer specify it. Width can be overridden if needed. Also modernized test coverage (removed snapshots, specifically tested CSS conditions)
1 parent 7a2975a commit eb9e360

File tree

4 files changed

+55
-113
lines changed

4 files changed

+55
-113
lines changed

packages/components/src/Layout/Grid/Grid.test.tsx

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,52 @@
2626

2727
import 'jest-styled-components'
2828
import React from 'react'
29-
import { assertSnapshot } from '@looker/components-test-utils'
29+
import { renderWithTheme } from '@looker/components-test-utils'
30+
import { screen } from '@testing-library/react'
3031
import { Grid } from './Grid'
3132

32-
test('Grid default', () => {
33-
assertSnapshot(
34-
<Grid>
35-
<div>🥑</div>
36-
<div>🐛</div>
37-
<div>🦜</div>
38-
<div>🐈</div>
39-
</Grid>
40-
)
41-
})
33+
const content = (
34+
<>
35+
<div>first</div>
36+
<div>second</div>
37+
<div>third</div>
38+
<div>fourth</div>
39+
</>
40+
)
4241

43-
test('Grid with specified gap', () => {
44-
assertSnapshot(
45-
<Grid gap="xlarge">
46-
<div>🥑</div>
47-
<div>🐛</div>
48-
<div>🦜</div>
49-
<div>🐈</div>
50-
</Grid>
51-
)
52-
})
42+
describe('Grid', () => {
43+
test('default', () => {
44+
renderWithTheme(<Grid data-testid="grid">{content} </Grid>)
45+
expect(screen.getByTestId('grid')).toHaveStyleRule('display', 'grid')
46+
})
47+
48+
test('specified gap', () => {
49+
renderWithTheme(
50+
<Grid data-testid="grid" gap="xlarge">
51+
{content}
52+
</Grid>
53+
)
54+
expect(screen.getByTestId('grid')).toHaveStyleRule('grid-gap', '2rem')
55+
})
56+
57+
test('specified columns', () => {
58+
renderWithTheme(
59+
<Grid data-testid="grid" columns={4}>
60+
{content}
61+
</Grid>
62+
)
63+
expect(screen.getByTestId('grid')).toHaveStyleRule(
64+
'grid-template-columns',
65+
'repeat(4,minmax(0,1fr))'
66+
)
67+
})
5368

54-
test('Grid with specified columns', () => {
55-
assertSnapshot(
56-
<Grid columns={4}>
57-
<div>🥑</div>
58-
<div>🐛</div>
59-
<div>🦜</div>
60-
<div>🐈</div>
61-
</Grid>
62-
)
69+
test('specified width', () => {
70+
renderWithTheme(
71+
<Grid data-testid="grid" width="50%">
72+
{content}
73+
</Grid>
74+
)
75+
expect(screen.getByTestId('grid')).toHaveStyleRule('width', '50%')
76+
})
6377
})

packages/components/src/Layout/Grid/Grid.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ export interface GridProps extends SpaceHelperProps {
3737
columns?: number
3838
}
3939

40-
export const Grid = styled.div.withConfig({ shouldForwardProp })<GridProps>`
40+
/**
41+
* Grid provides a simple implementation of the CSS Grid.
42+
*
43+
* By default `Grid` has two columns with a "medium" `gap` between grid cells and "100%" `width`
44+
*/
45+
export const Grid = styled.div
46+
.withConfig({ shouldForwardProp })
47+
.attrs<GridProps>(({ width = '100%' }) => ({ width }))<GridProps>`
4148
${simpleLayoutCSS}
4249
4350
display: grid;
4451
grid-gap: ${({ gap, theme }) => theme.space[gap || defaultGap]};
45-
grid-template-columns: ${({ columns }) =>
46-
`repeat(${columns || 2}, minmax(0, 1fr))`};
52+
grid-template-columns:
53+
repeat(${({ columns }) => columns || 2}, minmax(0, 1fr));
4754
`

packages/components/src/Layout/Grid/__snapshots__/Grid.test.tsx.snap

Lines changed: 0 additions & 79 deletions
This file was deleted.

www/src/documentation/components/layout/grid.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ storybook: true
88

99
## Default
1010

11-
Out-of-the-box the `Grid` has two columns with a "medium" `gap` between grid cells.
11+
By default `Grid` has two columns with a "medium" `gap` between grid cells and "100%" `width`
1212

1313
```jsx
1414
<Grid>

0 commit comments

Comments
 (0)