Skip to content

Commit fc45c2f

Browse files
author
Jakub Holak
committed
Add styled system to box
1 parent 46dd46d commit fc45c2f

File tree

2 files changed

+30
-53
lines changed

2 files changed

+30
-53
lines changed

src/components/box/Box.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { FunctionComponent } from "react";
22
import styled, { css } from "styled-components";
33
import theme from "../../theme";
44
import { Container } from "../container";
5+
import { space, SpaceProps } from "styled-system";
56

67
type BoxProps = {
78
dark?: boolean;
@@ -11,7 +12,7 @@ type BoxProps = {
1112
color?: string;
1213
backgroundColor?: string;
1314
radius?: "small" | "medium" | "large";
14-
padding?:
15+
spacing?:
1516
| "spacing01"
1617
| "spacing02"
1718
| "spacing03"
@@ -20,7 +21,7 @@ type BoxProps = {
2021
| "spacing06"
2122
| "spacing07"
2223
| "spacing08";
23-
margin?:
24+
innerSpacing?:
2425
| "spacing01"
2526
| "spacing02"
2627
| "spacing03"
@@ -29,13 +30,12 @@ type BoxProps = {
2930
| "spacing06"
3031
| "spacing07"
3132
| "spacing08";
32-
3333
shadow?: "shadow01" | "shadow02" | "shadow03" | "shadow04";
3434

3535
children?: React.ReactNode;
3636
};
3737

38-
const StyledBox = styled(Container)<BoxProps>`
38+
const StyledBox = styled(Container)<BoxProps & SpaceProps>`
3939
box-sizing: border-box;
4040
min-width: 0;
4141
padding: ${theme.spacing.spacing05};
@@ -57,16 +57,17 @@ const StyledBox = styled(Container)<BoxProps>`
5757
css`
5858
border-radius: ${theme.radius[radius]};
5959
`}
60-
${({ padding }) =>
61-
padding &&
62-
css`
63-
padding: ${theme.spacing[padding]};
64-
`}
6560
66-
${({ margin }) =>
67-
margin &&
61+
${({ innerSpacing }) =>
62+
innerSpacing &&
63+
css`
64+
padding: ${theme.spacing[innerSpacing]};
65+
`}
66+
67+
${({ spacing }) =>
68+
spacing &&
6869
css`
69-
margin-bottom: ${theme.spacing[margin]};
70+
margin-bottom: ${theme.spacing[spacing]};
7071
`}
7172
7273
${({ shadow }) =>
@@ -99,6 +100,7 @@ const StyledBox = styled(Container)<BoxProps>`
99100
css`
100101
background-color: ${theme.colors[backgroundColor] || backgroundColor};
101102
`}
103+
${space}
102104
`;
103105

104106
export const Box: FunctionComponent<BoxProps> = ({ children, ...props }) => (

src/stories/Box.stories.mdx

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type BoxProps = {
3838
color?: string;
3939
backgroundColor?: string;
4040
radius?: string;
41-
padding?: string;
41+
innerSpacing?: string;
4242
spacing?: string;
4343
shadow?: string;
4444
children?: React.ReactNode;
@@ -60,25 +60,15 @@ type BoxProps = {
6060
<Preview>
6161
<Story name="border">
6262
<Container>
63-
<Box border="default">
64-
Default border
65-
</Box>
63+
<Box border="default">Default border</Box>
6664
<Spacer height="1rem" />
67-
<Box border="disabled">
68-
Bold border
69-
</Box>
65+
<Box border="disabled">Bold border</Box>
7066
<Spacer height="1rem" />
71-
<Box border="danger">
72-
Danger border
73-
</Box>
67+
<Box border="danger">Danger border</Box>
7468
<Spacer height="1rem" />
75-
<Box border="notification">
76-
Notification border
77-
</Box>
69+
<Box border="notification">Notification border</Box>
7870
<Spacer height="1rem" />
79-
<Box border="1px solid violet">
80-
Custom border
81-
</Box>
71+
<Box border="1px solid violet">Custom border</Box>
8272
</Container>
8373
</Story>
8474
</Preview>
@@ -107,39 +97,30 @@ type BoxProps = {
10797

10898
### Padding
10999

110-
> `padding`: `string`[`spacing01`, `spacing02`, `spacing03`, `spacing04`, `spacing05`, `spacing06`, `spacing07`]
100+
> `innerSpacing`: `string`[`spacing01`, `spacing02`, `spacing03`, `spacing04`, `spacing05`, `spacing06`, `spacing07`]
111101
112102
<Preview>
113-
<Story name="padding">
103+
<Story name="innerSpacing">
114104
<Container>
115-
<Box padding="spacing04">
116-
Box with spacing04 padding prop
117-
</Box>
105+
<Box innerSpacing="spacing04">Box with spacing04 innerSpacing prop</Box>
118106
<Spacer height="1rem" />
119-
<Box padding="spacing07">Box with spacing07 padding prop</Box>
107+
<Box innerSpacing="spacing07">Box with spacing07 innerSpacing prop</Box>
120108
</Container>
121109
</Story>
122110
</Preview>
123111

124-
125112
### Shadow
126113

127114
> `shadow`: `string`[`shadow01`, `shadow02`, `shadow03`, `shadow04`]
128115
129116
<Preview>
130117
<Story name="shadow">
131118
<Container>
132-
<Box shadow="shadow01">
133-
Box with shadow01 shadow prop
134-
</Box>
119+
<Box shadow="shadow01">Box with shadow01 shadow prop</Box>
135120
<Spacer height="1rem" />
136-
<Box shadow="shadow02">
137-
Box with shadow02 shadow prop
138-
</Box>
121+
<Box shadow="shadow02">Box with shadow02 shadow prop</Box>
139122
<Spacer height="1rem" />
140-
<Box shadow="shadow03">
141-
Box with shadow03 shadow prop
142-
</Box>
123+
<Box shadow="shadow03">Box with shadow03 shadow prop</Box>
143124
<Spacer height="1rem" />
144125
<Box shadow="shadow04">Box with shadow04 shadow prop</Box>
145126
</Container>
@@ -153,21 +134,15 @@ type BoxProps = {
153134
<Preview>
154135
<Story name="color">
155136
<Container>
156-
<Box color="danger">
157-
Danger color (from theme)
158-
</Box>
137+
<Box color="danger">Danger color (from theme)</Box>
159138
<Spacer height="1rem" />
160-
<Box color="notification">
161-
Notification color (from theme)
162-
</Box>
139+
<Box color="notification">Notification color (from theme)</Box>
163140
<Spacer height="1rem" />
164141
<Box color="palevioletred">
165142
Palevioletred box (not exististing in theme)
166143
</Box>
167144
<Spacer height="1rem" />
168-
<Box color="#5ead9d">
169-
#5ead9d color
170-
</Box>
145+
<Box color="#5ead9d">#5ead9d color</Box>
171146
</Container>
172147
</Story>
173148
</Preview>

0 commit comments

Comments
 (0)