Skip to content

Commit 30e094a

Browse files
Merge pull request #38 from commitd/spacing
Modifies spacing to improve compatibility.
2 parents cfbcf09 + 8eaef89 commit 30e094a

File tree

4 files changed

+115
-7
lines changed

4 files changed

+115
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@committed/components",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Committed Component Library",
55
"author": "Committed",
66
"license": "MIT",

src/stories/designPrinciples.stories.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ We have a fixed list of spacings that double with each step. Using fixed spacing
2929
feeling to the design. If a space is too small, go up a step, too large go down, try to avoid custom spacings.
3030

3131
The [positioning props][positioning] that deal with spacing (e.g. margin and padding) take a number from 0 to 6 to select the correct
32-
spacing, 0 being no space and 6 is 128px.
32+
spacing, 0 being no space and 6 is 128px. In order to be compatible with others using different sets we map less that 0 to 0,
33+
greater than 6 to 6 and floor the number, incase not an integer.
3334

3435
<Flex flexDirection="column">
3536
<Flex alignItems="center">

src/stories/spacing.stories.tsx

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import React from 'react'
2+
import { Box, Flex, Text, colors, theme } from '../'
3+
4+
export default {
5+
title: 'Spacing'
6+
}
7+
8+
export const exampleSpacing = () => (
9+
<Flex flexDirection="column">
10+
<Flex alignItems="center">
11+
<Text>0</Text>
12+
<Box
13+
m={1}
14+
py={1}
15+
bgcolor={colors.committedYellow[200]}
16+
width={theme.spacing(0)}
17+
/>
18+
</Flex>
19+
<Flex alignItems="center">
20+
<Text>1</Text>
21+
<Box
22+
m={1}
23+
py={1}
24+
bgcolor={colors.committedYellow[300]}
25+
width={theme.spacing(1)}
26+
/>
27+
</Flex>
28+
<Flex alignItems="center">
29+
<Text>2</Text>
30+
<Box
31+
m={1}
32+
py={1}
33+
bgcolor={colors.committedYellow[400]}
34+
width={theme.spacing(2)}
35+
/>
36+
</Flex>
37+
<Flex alignItems="center">
38+
<Text>3</Text>
39+
<Box
40+
m={1}
41+
py={1}
42+
bgcolor={colors.committedYellow[500]}
43+
width={theme.spacing(3)}
44+
/>
45+
</Flex>
46+
<Flex alignItems="center">
47+
<Text>4</Text>
48+
<Box
49+
m={1}
50+
py={1}
51+
bgcolor={colors.committedYellow[600]}
52+
width={theme.spacing(4)}
53+
/>
54+
</Flex>
55+
<Flex alignItems="center">
56+
<Text>5</Text>
57+
<Box
58+
m={1}
59+
py={1}
60+
bgcolor={colors.committedYellow[700]}
61+
width={theme.spacing(5)}
62+
/>
63+
</Flex>
64+
<Flex alignItems="center">
65+
<Text>6</Text>
66+
<Box
67+
m={1}
68+
py={1}
69+
bgcolor={colors.committedYellow[800]}
70+
width={theme.spacing(6)}
71+
/>
72+
</Flex>
73+
</Flex>
74+
)
75+
76+
export const egdeCases = () => (
77+
<Flex flexDirection="column">
78+
<Flex alignItems="center">
79+
<Text>-1</Text>
80+
<Box
81+
m={1}
82+
py={1}
83+
bgcolor={colors.committedYellow[200]}
84+
width={theme.spacing(-1)}
85+
/>
86+
</Flex>
87+
<Flex alignItems="center">
88+
<Text>3.5</Text>
89+
<Box
90+
m={1}
91+
py={1}
92+
bgcolor={colors.committedYellow[300]}
93+
width={theme.spacing(3.5)}
94+
/>
95+
</Flex>
96+
<Flex alignItems="center">
97+
<Text>7</Text>
98+
<Box
99+
m={1}
100+
py={1}
101+
bgcolor={colors.committedYellow[400]}
102+
width={theme.spacing(7)}
103+
/>
104+
</Flex>
105+
</Flex>
106+
)

src/theme/theme.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ declare module '@material-ui/core/styles/createPalette' {
6464
const addTransparency = (color: string) => `${color}88`
6565

6666
export const spacing = (factor: number) => {
67-
if (factor < 0 || factor > 6) {
68-
throw new Error(
69-
`Spacing ${factor} invalid, must be between 0 and 6 inclusive`
70-
)
67+
// should be an int, but just incase
68+
var index = Math.floor(factor)
69+
if (index < 0) index = 0
70+
if (index > 6) {
71+
index = 6
7172
}
72-
return [0, 4, 8, 16, 32, 64, 128][factor]
73+
return [0, 4, 8, 16, 32, 64, 128][index]
7374
}
7475

7576
export const defaultPaletteColors = {

0 commit comments

Comments
 (0)