Skip to content

Commit 7d37635

Browse files
authored
Merge pull request #4 from magicspon/master
adds the ability to set grid row/column gap
2 parents a81ba56 + 8d3a9ca commit 7d37635

File tree

2 files changed

+87
-64
lines changed

2 files changed

+87
-64
lines changed

README.md

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,27 @@
99

1010
```js
1111
module.exports = {
12-
// ...
13-
14-
plugins: [
15-
// ...
16-
require('tailwindcss-grid')({
17-
grids: [2, 3, 5, 6, 8, 10, 12],
18-
gaps: {
19-
0: '0',
20-
4: '1rem',
21-
8: '2rem'
22-
},
23-
autoMinWidths: {
24-
'16': '4rem',
25-
'24': '6rem',
26-
'300px': '300px'
27-
},
28-
variants: ['responsive'],
29-
}),
30-
],
12+
// ...
13+
14+
plugins: [
15+
// ...
16+
require('tailwindcss-grid')({
17+
grids: [2, 3, 5, 6, 8, 10, 12],
18+
gaps: {
19+
0: '0',
20+
4: '1rem',
21+
8: '2rem',
22+
'4-x': '1rem',
23+
'4-y': '1rem'
24+
},
25+
autoMinWidths: {
26+
'16': '4rem',
27+
'24': '6rem',
28+
'300px': '300px'
29+
},
30+
variants: ['responsive']
31+
})
32+
]
3133
}
3234
```
3335

@@ -40,7 +42,7 @@ The plugin generates the following sets of classes:
4042
- `.grid` for setting `display: grid` on an element
4143
- `.grid-columns-{size}` for specifying the number of columns in the grid
4244
- `.col-span-{columns}` for specifying how many columns a cell should span
43-
- `.grid-gap-{size}` for specifying the size of the gap between columns/rows
45+
- `.grid-gap-{size}` for specifying the size of the gap between columns/rows, if the name ends with -x or -y `grid-[column/row]-gap` will be used
4446
- `.grid-automin-{size}` for applying the minimum width of the columns using auto-fit and minmax (the max is 1fr)
4547
- `.col-start-{line}` and `.col-end-{line}` for specifying a cell's start and end grid lines explicitly (useful for reordering cells or leaving gaps)
4648
- `.row-span-{columns}` for specifying how many rows a cell should span

index.js

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,75 @@
11
const _ = require('lodash')
22

3-
module.exports = function ({
3+
module.exports = function({
44
grids = _.range(1, 12),
55
gaps = {},
66
autoMinWidths = {},
7-
variants = ['responsive']
7+
variants = ['responsive'],
88
}) {
9-
return function ({ e, addUtilities }) {
10-
addUtilities([
11-
{ '.grid': { display: 'grid' } },
12-
{ '.grid-dense': { gridAutoFlow: 'dense' } },
13-
..._.map(gaps, (size, name) => ({
14-
[`.${e(`grid-gap-${name}`)}`]: { gridGap: size },
15-
})),
16-
...grids.map(columns => ({
17-
[`.grid-columns-${columns}`]: {
18-
gridTemplateColumns: `repeat(${columns}, 1fr)`,
19-
}
20-
})),
21-
..._.map(autoMinWidths, (size, name) => ({
22-
[`.${e(`grid-automin-${name}`)}`]: {
23-
gridTemplateColumns: `repeat(auto-fit, minmax(${size}, 1fr))`
9+
return function({ e, addUtilities }) {
10+
addUtilities(
11+
[
12+
{
13+
'.grid': {
14+
display: 'grid',
15+
},
2416
},
25-
})),
26-
..._.range(1, _.max(grids) + 1).map(span => ({
27-
[`.col-span-${span}`]: {
28-
gridColumnStart: `span ${span}`,
29-
}
30-
})),
31-
..._.range(1, _.max(grids) + 2).map(line => ({
32-
[`.col-start-${line}`]: {
33-
gridColumnStart: `${line}`,
17+
{
18+
'.grid-dense': {
19+
gridAutoFlow: 'dense',
20+
},
3421
},
35-
[`.col-end-${line}`]: {
36-
gridColumnEnd: `${line}`,
37-
},
38-
})),
39-
..._.range(1, _.max(grids) + 1).map(span => ({
40-
[`.row-span-${span}`]: {
41-
gridRowStart: `span ${span}`,
42-
}
43-
})),
44-
..._.range(1, _.max(grids) + 2).map(line => ({
45-
[`.row-start-${line}`]: {
46-
gridRowStart: `${line}`,
47-
},
48-
[`.row-end-${line}`]: {
49-
gridRowEnd: `${line}`,
50-
},
51-
})),
52-
], variants)
22+
..._.map(gaps, (size, name) => {
23+
const gridGap = name.endsWith('-y')
24+
? 'gridRowGap'
25+
: name.endsWith('-x')
26+
? 'gridColumnGap'
27+
: 'gridGap'
28+
29+
return {
30+
[`.${e(`grid-gap-${name}`)}`]: {
31+
[gridGap]: size,
32+
},
33+
}
34+
}),
35+
...grids.map(columns => ({
36+
[`.grid-columns-${columns}`]: {
37+
gridTemplateColumns: `repeat(${columns}, 1fr)`,
38+
},
39+
})),
40+
..._.map(autoMinWidths, (size, name) => ({
41+
[`.${e(`grid-automin-${name}`)}`]: {
42+
gridTemplateColumns: `repeat(auto-fit, minmax(${size}, 1fr))`,
43+
},
44+
})),
45+
..._.range(1, _.max(grids) + 1).map(span => ({
46+
[`.col-span-${span}`]: {
47+
gridColumnStart: `span ${span}`,
48+
},
49+
})),
50+
..._.range(1, _.max(grids) + 2).map(line => ({
51+
[`.col-start-${line}`]: {
52+
gridColumnStart: `${line}`,
53+
},
54+
[`.col-end-${line}`]: {
55+
gridColumnEnd: `${line}`,
56+
},
57+
})),
58+
..._.range(1, _.max(grids) + 1).map(span => ({
59+
[`.row-span-${span}`]: {
60+
gridRowStart: `span ${span}`,
61+
},
62+
})),
63+
..._.range(1, _.max(grids) + 2).map(line => ({
64+
[`.row-start-${line}`]: {
65+
gridRowStart: `${line}`,
66+
},
67+
[`.row-end-${line}`]: {
68+
gridRowEnd: `${line}`,
69+
},
70+
})),
71+
],
72+
variants,
73+
)
5374
}
5475
}

0 commit comments

Comments
 (0)