Skip to content

Commit 16cc9ef

Browse files
committed
feat: add SimpleGrid component and improve Stack
1 parent 46b8ee0 commit 16cc9ef

File tree

9 files changed

+144
-20
lines changed

9 files changed

+144
-20
lines changed

packages/chakra-ui-docs/pages/stack.mdx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Stack } from "@chakra-ui/core";
1111
```
1212

1313
By default, each item is stacked vertically. Stack clones it's children and
14-
passes the spacing to them via the `margin-bottom` or `margin-right`.
14+
passes the spacing to them using `margin-bottom` or `margin-right`.
1515

1616
## Usage
1717

@@ -43,10 +43,12 @@ function StackEx() {
4343
render(<StackEx />);
4444
```
4545

46-
### Inline stack
46+
### Stack items horizontally
4747

48-
The stack each item horizonally, pass the `isInline` prop. Optionally, you can
49-
use `align` and `justify` to adjust the alignment of the items.
48+
Pass the `isInline` prop or `direction` and set it to `horizontal`.
49+
50+
Optionally, you can use `align` and `justify` to adjust the alignment and
51+
distribution of the items.
5052

5153
```jsx isManual=true
5254
function Feature({ title, desc, ...rest }) {

packages/chakra-ui/src/Grid/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from "react";
22
import * as StyledSystem from "styled-system";
33
import { BoxProps } from "../Box";
44

5-
interface IGrid {
5+
export interface IGrid {
66
templateColumns?: StyledSystem.GridTemplateColumnsProps["gridTemplateColumns"];
77
gap?: StyledSystem.GridGapProps["gridGap"];
88
rowGap?: StyledSystem.GridRowGapProps["gridRowGap"];

packages/chakra-ui/src/Grid/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import Box from "../Box";
44
const Grid = forwardRef(
55
(
66
{
7-
templateColumns,
87
gap,
98
rowGap,
109
columnGap,
1110
autoFlow,
1211
autoRows,
1312
autoColumns,
1413
templateRows,
14+
templateColumns,
1515
templateAreas,
1616
area,
1717
column,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/** @jsx jsx */
2+
import { jsx } from "@emotion/core";
3+
import { storiesOf } from "@storybook/react";
4+
import Box from "../Box";
5+
import SimpleGrid from ".";
6+
7+
const stories = storiesOf("SimpleGrid", module);
8+
stories.add("Default", () => (
9+
<SimpleGrid columns={2} autoFit spacing="40px" minChildWidth="300px">
10+
<Box bg="tomato" height="200px"></Box>
11+
<Box bg="tomato" height="200px"></Box>
12+
<Box bg="tomato" height="200px"></Box>
13+
<Box bg="tomato" height="200px"></Box>
14+
<Box bg="tomato" height="200px"></Box>
15+
</SimpleGrid>
16+
));
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as React from "react";
2+
import { FlexboxProps } from "styled-system";
3+
import { BoxProps } from "../Box";
4+
5+
interface ISimpleGrid {
6+
/**
7+
* The width at which child elements will break into columns. Pass a number for pixel values or a string for any other valid CSS length.
8+
*/
9+
minChildWidth?: React.CSSProperties["minWidth"];
10+
/**
11+
* The align-items to control child element alignment.
12+
*/
13+
align?: FlexboxProps["alignItems"];
14+
/**
15+
* The justify-items to control child element alignment.
16+
*/
17+
justify?: FlexboxProps["justifyItems"];
18+
/**
19+
* The number of rows
20+
*/
21+
rows?: number;
22+
/**
23+
* The number of columns
24+
*/
25+
columns?: number;
26+
/**
27+
* The gap between the grid items
28+
*/
29+
spacing?: BoxProps["gridGap"];
30+
/**
31+
* The row gap between the grid items
32+
*/
33+
spacingX?: BoxProps["gridGap"];
34+
/**
35+
* The column gap between the grid items
36+
*/
37+
spacingY?: BoxProps["gridGap"];
38+
/**
39+
* If `true` will apply `auto-fit` to the grid-template-columns
40+
*/
41+
autoFit?: boolean;
42+
/**
43+
* If `true` will apply `auto-fill` to the grid-template-columns
44+
*/
45+
autoFill?: boolean;
46+
}
47+
48+
type SimpleGridProps = BoxProps & ISimpleGrid;
49+
50+
declare const SimpleGrid: React.FC<SimpleGridProps>;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, { forwardRef } from "react";
2+
import Grid from "../Grid";
3+
4+
const SimpleGrid = forwardRef(
5+
(
6+
{
7+
rows,
8+
columns,
9+
spacingX,
10+
spacingY,
11+
spacing,
12+
autoFit,
13+
autoFill,
14+
minChildWidth,
15+
align,
16+
justify,
17+
...props
18+
},
19+
ref,
20+
) => {
21+
let templateColumns;
22+
if (autoFit && minChildWidth) {
23+
templateColumns = `repeat(auto-fit, minmax(${minChildWidth}, 1fr))`;
24+
}
25+
if (autoFill && minChildWidth) {
26+
templateColumns = `repeat(auto-fill, minmax(${minChildWidth}, 1fr))`;
27+
}
28+
return (
29+
<Grid
30+
ref={ref}
31+
gap={spacing}
32+
columnGap={spacingX}
33+
rowGap={spacingY}
34+
templateRows={`repeat(${rows}, 1fr)`}
35+
templateColumns={templateColumns || `repeat(${columns}, 1fr)`}
36+
justifyItems={justify}
37+
alignItems={align}
38+
{...props}
39+
/>
40+
);
41+
},
42+
);
43+
44+
export default SimpleGrid;

packages/chakra-ui/src/Stack/examples.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from "react";
22
import { storiesOf } from "@storybook/react";
33
import Stack from ".";
4+
import Box from "../Box";
45

56
const stories = storiesOf("Stack", module);
67

@@ -13,9 +14,9 @@ stories.add("vertical stack", () => (
1314
));
1415

1516
stories.add("Inline Stack", () => (
16-
<Stack isInline spacing={5}>
17-
<span>ooooooo</span>
18-
<span>ahhhhh</span>
19-
<span>Woah!</span>
17+
<Stack bg="blue.500" w="100%" h="60px" direction="horizontal">
18+
<Box size="40px" background={"#fff"} rounded="full" />
19+
<Box size="40px" background={"#fff"} rounded="full" />
20+
<Box size="40px" background={"#fff"} rounded="full" />
2021
</Stack>
2122
));

packages/chakra-ui/src/Stack/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ interface IStack {
88
* If `true` the items will be stacked horizontally inline.
99
*/
1010
isInline?: boolean;
11+
/**
12+
* If `true` the items will be stacked horizontally inline.
13+
*/
14+
direction?: FlexProps["direction"];
1115
/**
1216
* The content of the stack.
1317
*/

packages/chakra-ui/src/Stack/index.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,34 @@ import Flex from "../Flex";
55
import Box from "../Box";
66

77
const Stack = ({
8+
direction,
89
isInline,
910
children,
10-
align,
11+
align = "center",
1112
justify,
1213
spacing = 2,
1314
shouldWrapChildren,
1415
...rest
1516
}) => {
17+
let flexDirection;
18+
19+
if (isInline) {
20+
flexDirection = isInline ? "row" : "column";
21+
}
22+
23+
if (direction) {
24+
flexDirection = direction;
25+
}
26+
1627
return (
17-
<Flex
18-
align={align}
19-
justify={justify}
20-
flexDir={isInline ? "row" : "column"}
21-
{...rest}
22-
>
28+
<Flex align={align} justify={justify} direction={flexDirection} {...rest}>
2329
{Children.map(children, (child, index) => {
2430
if (!isValidElement(child)) return;
2531
let isLastChild = children.length === index + 1;
26-
let spacingProps = isInline
27-
? { mr: isLastChild ? null : spacing }
28-
: { mb: isLastChild ? null : spacing };
32+
let spacingProps =
33+
isInline || direction === "horizontal"
34+
? { mr: isLastChild ? null : spacing }
35+
: { mb: isLastChild ? null : spacing };
2936

3037
if (shouldWrapChildren) {
3138
return (

0 commit comments

Comments
 (0)