/
BasicGrid.tsx
70 lines (61 loc) · 2.09 KB
/
BasicGrid.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import styled, { css, DefaultTheme, ThemeProps } from 'styled-components';
import { ResponsiveProperty, ResponsiveRule } from '../../theme';
import { Flex } from './Flex';
export interface BasicGridProps extends ThemeProps<DefaultTheme> {
/** Set the number of columns directly */
columns?: ResponsiveRule<number>;
/** Set the number of columns based on a required width for children */
minChildWidth?: ResponsiveProperty<'width', 'space'>;
/** The gap between children both horizontally and vertically */
spacing?: ResponsiveProperty<'gap', 'space'>;
/** The gap between children only in the horizontal direction */
spacingX?: ResponsiveProperty<'columnGap', 'space'>;
/** The gap between children only in the vertical direction */
spacingY?: ResponsiveProperty<'rowGap', 'space'>;
}
function getGridColumns(props: BasicGridProps) {
const { mapResponsive } = props.theme.mixins;
// Handle set number of columns
if (props.columns !== undefined) {
return mapResponsive(
props.columns,
columnCount =>
css`
grid-template-columns: repeat(${columnCount}, minmax(0, 1fr));
`
);
}
// Handle minimum child width
if (props.minChildWidth !== undefined) {
return mapResponsive(
props.minChildWidth,
minWidth =>
css`
grid-template-columns: repeat(auto-fit, minmax(${minWidth}, 1fr));
`
);
}
return css`
grid-auto-columns: max-content;
`;
}
/**
* A basic grid component that distributes its children evenly.
*
* Either `columns` or `minChildWidth` can be used to set the number of
* columns. `columns` sets the column count to a set number while
* `minChildWidth` sets the column count based on a minimum width the child
* components need.
*/
export const BasicGrid = styled(Flex)<BasicGridProps>`
display: grid;
${p => {
const { createRuleForProp } = p.theme.mixins;
return css`
${createRuleForProp('spacing', 'gap', 'space')}
${createRuleForProp('spacingX', 'column-gap', 'space')}
${createRuleForProp('spacingY', 'row-gap', 'space')}
`;
}}
${getGridColumns}
`;