-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
grid.tsx
181 lines (170 loc) · 4.22 KB
/
grid.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import {
chakra,
forwardRef,
ResponsiveValue,
SystemProps,
HTMLChakraProps,
} from "@chakra-ui/system"
import { filterUndefined, mapResponsive, __DEV__ } from "@chakra-ui/utils"
import * as React from "react"
import { BoxProps } from "./box"
export interface GridProps extends HTMLChakraProps<"div">, GridOptions {}
/**
* React component used to create grid layouts.
*
* It renders a `div` with `display: grid` and
* comes with helpful style shorthand.
*
* @see Docs https://chakra-ui.com/docs/layout/grid
*/
export const Grid = forwardRef<GridProps, "div">((props, ref) => {
const {
area,
templateAreas,
gap,
rowGap,
columnGap,
column,
row,
autoFlow,
autoRows,
templateRows,
autoColumns,
templateColumns,
...rest
} = props
const styles = {
display: "grid",
gridArea: area,
gridTemplateAreas: templateAreas,
gridGap: gap,
gridRowGap: rowGap,
gridColumnGap: columnGap,
gridAutoColumns: autoColumns,
gridColumn: column,
gridRow: row,
gridAutoFlow: autoFlow,
gridAutoRows: autoRows,
gridTemplateRows: templateRows,
gridTemplateColumns: templateColumns,
}
return <chakra.div ref={ref} __css={styles} {...rest} />
})
if (__DEV__) {
Grid.displayName = "Grid"
}
export interface GridOptions {
/**
* Short hand prop for `gridTemplateColumns`
* @type SystemProps["gridTemplateColumns"]
*/
templateColumns?: SystemProps["gridTemplateColumns"]
/**
* Short hand prop for `gridGap`
* @type SystemProps["gridGap"]
*/
gap?: SystemProps["gridGap"]
/**
* Short hand prop for `gridRowGap`
* @type SystemProps["gridRowGap"]
*/
rowGap?: SystemProps["gridRowGap"]
/**
* Short hand prop for `gridColumnGap`
* @type SystemProps["gridColumnGap"]
*/
columnGap?: SystemProps["gridColumnGap"]
/**
* Short hand prop for `gridAutoFlow`
* @type SystemProps["gridAutoFlow"]
*/
autoFlow?: SystemProps["gridAutoFlow"]
/**
* Short hand prop for `gridAutoRows`
* @type SystemProps["gridAutoRows"]
*/
autoRows?: SystemProps["gridAutoRows"]
/**
* Short hand prop for `gridAutoColumns`
* @type SystemProps["gridAutoColumns"]
*/
autoColumns?: SystemProps["gridAutoColumns"]
/**
* Short hand prop for `gridTemplateRows`
* @type SystemProps["gridTemplateRows"]
*/
templateRows?: SystemProps["gridTemplateRows"]
/**
* Short hand prop for `gridTemplateAreas`
* @type SystemProps["gridTemplateAreas"]
*/
templateAreas?: SystemProps["gridTemplateAreas"]
/**
* Short hand prop for `gridArea`
* @type SystemProps["gridArea"]
*/
area?: SystemProps["gridArea"]
/**
* Short hand prop for `gridColumn`
* @type SystemProps["gridColumn"]
*/
column?: SystemProps["gridColumn"]
/**
* Short hand prop for `gridRow`
* @type SystemProps["gridRow"]
*/
row?: SystemProps["gridRow"]
}
export interface GridItemProps extends BoxProps {
/**
* The number of columns the grid item should `span`.
* @type ResponsiveValue<number | "auto">
*/
colSpan?: ResponsiveValue<number | "auto">
/**
* The column number the grid item should start.
* @type ResponsiveValue<number | "auto">
*/
colStart?: ResponsiveValue<number | "auto">
/**
* @type ResponsiveValue<number | "auto">
*/
colEnd?: ResponsiveValue<number | "auto">
/**
* @type ResponsiveValue<number | "auto">
*/
rowStart?: ResponsiveValue<number | "auto">
/**
* @type ResponsiveValue<number | "auto">
*/
rowEnd?: ResponsiveValue<number | "auto">
/**
* @type ResponsiveValue<number | "auto">
*/
rowSpan?: ResponsiveValue<number | "auto">
}
function spanFn(span?: ResponsiveValue<number | "auto">) {
return mapResponsive(span, (value) =>
value === "auto" ? "auto" : `span ${value}/span ${value}`,
)
}
export const GridItem = forwardRef<GridItemProps, "div">((props, ref) => {
const {
colSpan,
colStart,
colEnd,
rowEnd,
rowSpan,
rowStart,
...rest
} = props
const styles = filterUndefined({
gridColumn: spanFn(colSpan),
gridRow: spanFn(rowSpan),
gridColumnStart: colStart,
gridColumnEnd: colEnd,
gridRowStart: rowStart,
gridRowEnd: rowEnd,
})
return <chakra.div ref={ref} __css={styles} {...rest} />
})