Skip to content

Commit 24fe403

Browse files
committed
Merge pull request #39 from simonsmith/master
Switch to flexbox for layout
2 parents e34eddf + f4d4254 commit 24fe403

File tree

5 files changed

+199
-48
lines changed

5 files changed

+199
-48
lines changed

README.md

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://secure.travis-ci.org/suitcss/components-grid.png?branch=master)](http://travis-ci.org/suitcss/components-grid)
44

5-
A CSS grid component. The grid makes use of `inline-block` and `box-sizing` to
5+
A CSS grid component. The grid makes use of `flexbox` and `box-sizing` to
66
provide features that float-based layouts cannot.
77

88
N.B. This component relies on particular dimensions being applied to cells in
@@ -20,11 +20,12 @@ Read more about [SUIT CSS](https://github.com/suitcss/suit/).
2020

2121
* Fluid layout.
2222
* Intelligent cell wrapping.
23+
* Evenly fit cell spacing
24+
* Equal height columns
2325
* Horizontal centering of cells.
2426
* Custom vertical alignment of cells (top, bottom, or middle).
2527
* Cell width is controlled independently of grid gutter.
2628
* Infinite nesting.
27-
* Built-in redundancy.
2829

2930
## Available classes
3031

@@ -33,22 +34,35 @@ Read more about [SUIT CSS](https://github.com/suitcss/suit/).
3334
* `Grid--alignRight`: right-align all child `Grid-cell`
3435
* `Grid--alignMiddle`: middle-align all child `Grid-cell`
3536
* `Grid--alignBottom`: bottom-align all child `Grid-cell`
37+
* `Grid--fit`: evenly distribute space amongst all child `Grid-cell`
38+
* `Grid--equalHeight`: all child `Grid-cell` match height of the tallest
3639
* `Grid--withGutter`: adds a gutter between cells
3740
* `Grid-cell`: a child cell of `Grid` that wraps grid content
3841
* `Grid-cell--center`: center an individual `Grid-cell`
3942

4043
## Configurable variables
4144

42-
* `--Grid-font-size`: the font-size to use within the Grid (defaults to 1rem).
4345
* `--Grid-gutter-size`: the width of the gutter applied by the `Grid--withGutter` modifier class.
4446

4547
## Use
4648

4749
A simple grid is easy to create. A grid container can have any number of child
48-
cells.
50+
cells. When used with `Grid--fit` space is evenly distributed without need for
51+
sizing utilities.
4952

5053
```html
51-
<div class="Grid [Grid--alignCenter|Grid--alignRight|Grid--alignMiddle|Grid--alignBottom]">
54+
<div class="Grid Grid--fit Grid--withGutter">
55+
<div class="Grid-cell"></div>
56+
<div class="Grid-cell"></div>
57+
<div class="Grid-cell"></div>
58+
<div class="Grid-cell"></div>
59+
</div>
60+
```
61+
62+
For more granular control over layout make use of modifiers and sizing utilities.
63+
64+
```html
65+
<div class="Grid [Grid--alignCenter|Grid--alignRight|Grid--alignMiddle|Grid--alignBottom|Grid--fit|Grid--equalHeight]">
5266
<div class="Grid-cell u-size1of2 u-lg-size6of12"></div>
5367
<div class="Grid-cell u-size1of2 u-lg-size4of12"></div>
5468
<div class="Grid-cell u-size1of3 u-lg-size2of12"></div>
@@ -87,7 +101,7 @@ BAD:
87101
```
88102

89103
You can nest grids in any context, including one that uses dimension or offset
90-
utilities, but keep in mind that the the dimensions will be relative to the
104+
utilities, but keep in mind that the dimensions will be relative to the
91105
grid's width, and not the width of the whole application.
92106

93107
```html
@@ -114,18 +128,26 @@ To generate a build:
114128
npm run build
115129
```
116130

117-
To generate the testing build.
131+
To generate the testing build:
118132

119133
```
120134
npm run build-test
121135
```
122136

137+
To watch the files for making changes to test:
138+
139+
```
140+
npm run watch
141+
```
142+
123143
Basic visual tests are in `test/index.html`.
124144

125145
## Browser support
126146

127147
* Google Chrome (latest)
128148
* Opera (latest)
129-
* Firefox 4+
130-
* Safari 5+
131-
* Internet Explorer 9+
149+
* Firefox 28+
150+
* Safari 6.1+
151+
* Internet Explorer 10+
152+
153+
Refer to the [caniuse](http://caniuse.com/) page for [flexbox](http://caniuse.com/#feat=flexbox)

build.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const postcss = require('postcss');
2+
const bemLinter = require('postcss-bem-linter');
3+
const reporter = require('postcss-reporter');
4+
5+
module.exports = {
6+
use: [
7+
"postcss-import",
8+
"postcss-custom-properties",
9+
"postcss-calc",
10+
"postcss-custom-media",
11+
"autoprefixer",
12+
"postcss-reporter"
13+
],
14+
"postcss-import": {
15+
transform(css) {
16+
return postcss([bemLinter, reporter]).process(css).css;
17+
},
18+
"postcss-reporter": {
19+
clearMessages: true
20+
}
21+
}
22+
};

lib/grid.css

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
/** @define Grid; use strict */
1+
/** @define Grid */
22

33
:root {
4-
--Grid-font-size: 1rem;
54
--Grid-gutter-size: 20px;
65
}
76

@@ -20,57 +19,69 @@
2019
*
2120
* 1. Account for browser defaults of elements that might be the root node of
2221
* the component.
23-
* 2. Remove inter-cell whitespace that appears between `inline-block` child
24-
* elements.
25-
* 3. Ensure consistent default alignment.
2622
*/
2723

2824
.Grid {
29-
display: block; /* 1 */
30-
font-size: 0; /* 2 */
25+
display: flex; /* 1 */
26+
box-sizing: border-box;
27+
flex-flow: row wrap;
3128
margin: 0; /* 1 */
3229
padding: 0; /* 1 */
33-
text-align: left; /* 3 */
3430
}
3531

3632
/**
3733
* Modifier: center align all grid cells
3834
*/
3935

4036
.Grid--alignCenter {
41-
text-align: center;
37+
justify-content: center;
4238
}
4339

4440
/**
4541
* Modifier: right align all grid cells
4642
*/
4743

4844
.Grid--alignRight {
49-
text-align: right;
45+
justify-content: flex-end;
5046
}
5147

5248
/**
5349
* Modifier: middle-align grid cells
5450
*/
5551

56-
.Grid--alignMiddle > .Grid-cell {
57-
vertical-align: middle;
52+
.Grid--alignMiddle {
53+
align-items: center;
5854
}
5955

6056
/**
6157
* Modifier: bottom-align grid cells
6258
*/
6359

64-
.Grid--alignBottom > .Grid-cell {
65-
vertical-align: bottom;
60+
.Grid--alignBottom {
61+
align-items: flex-end;
6662
}
6763

6864
/**
69-
* Modifier: gutters
65+
* Modifier: allow cells to equal distribute width
7066
*
71-
* NOTE: this can trigger a horizontal scrollbar if the component is as wide as
72-
* the viewport. Use padding on a container, or `overflow-x:hidden` to protect
73-
* against it.
67+
* 1. Provide all values to avoid IE10 bug with shorthand flex - http://git.io/vllC7
68+
* Use `0%` to avoid bug in IE10/11 with unitless flex basis - http://git.io/vllWx
69+
*/
70+
71+
.Grid--fit > .Grid-cell {
72+
flex: 1 1 0%; /* 1 */
73+
}
74+
75+
/**
76+
* Modifier: all cells match height of tallest cell in a row
77+
*/
78+
79+
.Grid--equalHeight > .Grid-cell {
80+
display: flex;
81+
}
82+
83+
/**
84+
* Modifier: gutters
7485
*/
7586

7687
.Grid--withGutter {
@@ -86,24 +97,14 @@
8697

8798
/**
8899
* No explicit width by default. Rely on combining `Grid-cell` with a dimension
89-
* utility or a component class that extends 'grid'.
100+
* utility or a component class that extends 'Grid'.
90101
*
91-
* 1. Fundamentals of the non-float grid layout.
92-
* 2. Reset font size change made in `Grid`.
93-
* 3. Keeps content correctly aligned with the grid direction.
94-
* 4. Controls vertical positioning of units.
95-
* 5. Make cells full-width by default.
102+
* 1. Set flex items to full width by default
96103
*/
97104

98105
.Grid-cell {
99-
box-sizing: border-box;
100-
display: inline-block; /* 1 */
101-
font-size: var(--Grid-font-size); /* 2 */
102-
margin: 0;
103-
padding: 0;
104-
text-align: left; /* 3 */
105-
vertical-align: top; /* 4 */
106-
width: 100%; /* 5 */
106+
flex: 0 0 100%; /* 1 */
107+
box-sizing: inherit;
107108
}
108109

109110
/**
@@ -113,6 +114,5 @@
113114
*/
114115

115116
.Grid-cell--center {
116-
display: block;
117117
margin: 0 auto;
118118
}

package.json

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,26 @@
88
"lib"
99
],
1010
"devDependencies": {
11+
"autoprefixer": "^6.0.3",
12+
"postcss": "^5.0.10",
13+
"postcss-bem-linter": "^2.0.0",
14+
"postcss-calc": "^5.0.0",
15+
"postcss-cli": "^2.3.2",
16+
"postcss-custom-media": "^5.0.0",
17+
"postcss-custom-properties": "^5.0.0",
18+
"postcss-import": "^7.1.0",
19+
"postcss-reporter": "^1.3.0",
1120
"suitcss-components-test": "*",
12-
"suitcss-preprocessor": "~0.4.0",
1321
"suitcss-utils-offset": "~0.5.0",
14-
"suitcss-utils-size": "~0.7.0"
22+
"suitcss-utils-size": "~0.8.0"
1523
},
1624
"scripts": {
1725
"build": "npm run setup && npm run preprocess",
1826
"build-test": "npm run setup && npm run preprocess-test",
19-
"preprocess": "suitcss index.css build/build.css",
20-
"preprocess-test": "suitcss test/test.css build/test.css",
21-
"setup": "npm install && mkdir -p build"
27+
"preprocess": "postcss -c build.js -o build/build.css index.css",
28+
"preprocess-test": "postcss -c build.js -o build/test.css test/test.css",
29+
"setup": "npm install && mkdir -p build",
30+
"watch": "npm run preprocess-test -- -w"
2231
},
2332
"repository": {
2433
"type": "git",

0 commit comments

Comments
 (0)