Skip to content

Commit 4ac3120

Browse files
committed
✨ Add offset prop to FlexGrid
1 parent 46b5bf2 commit 4ac3120

File tree

2 files changed

+77
-23
lines changed

2 files changed

+77
-23
lines changed

src/flex-grid/about.mdx

+50-16
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,31 @@ import { FlexGrid } from './index'
1414

1515
## Description
1616

17-
FlexGrid component created with [`FlexBox`](#/flex-box).
17+
Grid component based on [`FlexBox`](#/flex-box).
1818

19-
- Default to `12` columns. Must be defined in `theme.grid`
20-
- Item size can be set with `col` or `size` prop in `FlexGrid.Item` component
21-
- Space between items can be set with `space`, `spacex` and `spacey` props
19+
- Default to `12` columns. Can be defined in `theme.grid`
20+
- Item size conrolled with `col` or `size` prop in `FlexGrid.Item` component
21+
- Space between items conrolled with `space`, `spacex` and `spacey` props
2222

23-
Components:
23+
### Components:
2424

25-
- `FlexGrid` — Container component. Control space between items with props:
26-
+ Possible values can be set in `theme.space`, see [space docs](https://github.com/exah/pss/blob/master/docs/api.md#space)
27-
+ `space` — setting horizontal and vertical space
28-
+ `spacex` — setting horizontal space
29-
+ `spacey` — setting vertical space
30-
+ `spaceContent` — apply space to `FlexGrid.Content` instead of `FlexGrid.Item`
31-
- `FlexGrid.Item` — Item / column component, size can be set with props:
32-
+ `col` — Number in range `1...theme.grid`, result is precentage size. (`col / theme.grid`, i.e. `3 / 12 = 25%`)
33-
+ `size` — Number from `0...1`, result is percentage (`0.5` is `50%`)
34-
- `FlexGrid.Content` — Content wrapper same as [`Box`](#/box)
25+
26+
#### `FlexGrid` — Container component. Control space between items with props:
27+
28+
- Possible values in `theme.space`. See [space docs](https://github.com/exah/pss/blob/master/docs/api.md#space)
29+
- `space` — horizontal and vertical space
30+
- `spacex` — horizontal space
31+
- `spacey` — vertical space
32+
- `spaceContent` — apply space to `FlexGrid.Content` instead of `FlexGrid.Item`
33+
34+
#### `FlexGrid.Item` — Item / column component, size can be set with props:
35+
36+
- `col` — Number in range `1...theme.grid`
37+
- `size` — Number in range `0...1`, result is percentage (`0.5` is `50%`)
38+
- `offset` — Number in range `1...theme.grid` — move item by number of columns
39+
40+
41+
#### `FlexGrid.Content` — Content wrapper same as [`Box`](#/box)
3542

3643
## Usage
3744

@@ -150,7 +157,34 @@ import { FlexGrid } from 'pss-components'
150157
</ThemeProvider>
151158
</Playground>
152159

153-
### Dynamic 'size' prop
160+
### Offset items
161+
162+
<Playground>
163+
<FlexGrid space={1}>
164+
<FlexGrid.Item col={3} mgr='75%'>
165+
<FlexGrid.Content bg='gray.0'>
166+
3
167+
</FlexGrid.Content>
168+
</FlexGrid.Item>
169+
<FlexGrid.Item col={3} offset={3}>
170+
<FlexGrid.Content bg='gray.0'>
171+
3
172+
</FlexGrid.Content>
173+
</FlexGrid.Item>
174+
<FlexGrid.Item col={3} offset={6}>
175+
<FlexGrid.Content bg='gray.0'>
176+
3
177+
</FlexGrid.Content>
178+
</FlexGrid.Item>
179+
<FlexGrid.Item col={3} offset={9}>
180+
<FlexGrid.Content bg='gray.0'>
181+
3
182+
</FlexGrid.Content>
183+
</FlexGrid.Item>
184+
</FlexGrid>
185+
</Playground>
186+
187+
### Dynamic size
154188

155189
<Playground>
156190
<FlexGrid space={1}>

src/flex-grid/styles.js

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
import { themePath, getSpace, sizeProp, createPropStyles } from 'pss'
2-
import { fallbackTo } from '@exah/utils'
2+
import { isNum, fallbackTo } from '@exah/utils'
3+
4+
function getSize (value, props) {
5+
const grid = themePath('grid', 12)(props)
6+
const cols = Number(fallbackTo(value, 1))
7+
8+
if (!isNum(cols) && !isNum(grid)) {
9+
return null
10+
}
11+
12+
return ((cols / grid) * 100) + '%'
13+
}
314

415
const gridItem = createPropStyles({
516
size: [
617
sizeProp('flexBasis', 'auto'),
718
sizeProp('maxWidth', '100%')
819
],
9-
col (value, props, mediaKey) {
20+
offset (value, props) {
21+
const size = getSize(value, props)
22+
23+
if (size == null) {
24+
return {}
25+
}
26+
27+
return {
28+
marginLeft: size
29+
}
30+
},
31+
col (value, props) {
1032
if (value === 'auto' || value === true) {
1133
return {
1234
flexGrow: 1,
@@ -16,14 +38,12 @@ const gridItem = createPropStyles({
1638
}
1739
}
1840

19-
if (typeof value !== 'number') {
41+
const size = getSize(value, props)
42+
43+
if (size == null) {
2044
return {}
2145
}
2246

23-
const grid = themePath('grid', 12)(props.theme)
24-
const cols = fallbackTo(value, 1)
25-
const size = ((cols / grid) * 100) + '%'
26-
2747
return {
2848
flexBasis: size,
2949
maxWidth: size

0 commit comments

Comments
 (0)