Skip to content

Commit 60b60aa

Browse files
committed
Init
0 parents  commit 60b60aa

File tree

14 files changed

+440
-0
lines changed

14 files changed

+440
-0
lines changed

.babelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"presets": [
3+
"es2015",
4+
"stage-0",
5+
"react"
6+
]
7+
}

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
# React CSS Grid
3+
4+
Responsive CSS-based React grid component
5+
6+
```sh
7+
npm i react-css-grid
8+
```
9+
10+
```js
11+
// Example usage
12+
import React from 'react'
13+
import Grid from 'react-css-grid'
14+
15+
class App extends React.Component {
16+
render () {
17+
return (
18+
<div>
19+
<Grid col={6} sm={4} md={3} lg={2}>Column</Grid>
20+
<Grid col={6} sm={4} md={3} lg={2}>Column</Grid>
21+
<Grid col={6} sm={4} md={3} lg={2}>Column</Grid>
22+
<Grid col={6} sm={4} md={3} lg={2}>Column</Grid>
23+
</div>
24+
)
25+
}
26+
}
27+
```
28+
29+
The Grid component creates CSS rules based on props and insert that string into an inline style tag. The component only creates the rules it needs for itself, however other Grid components may generate duplicative styles of their own.
30+
31+
32+
MIT License

demo/App.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
import React from 'react'
3+
import Grid from '..'
4+
5+
6+
class App extends React.Component {
7+
render () {
8+
return (
9+
<Grid p={3}>
10+
<h1>Hello react-css-grid</h1>
11+
<div>
12+
<Grid sm={6} md={3} p={2}>
13+
sm6 md3
14+
</Grid>
15+
<Grid sm={6} md={3} p={2}>
16+
sm6 md3
17+
</Grid>
18+
<Grid sm={6} md={3} p={2}>
19+
sm6 md3
20+
</Grid>
21+
<Grid sm={6} md={3} p={2}>
22+
sm6 md3
23+
</Grid>
24+
</div>
25+
</Grid>
26+
)
27+
}
28+
}
29+
30+
export default App
31+

demo/entry.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
import React from 'react'
3+
import ReactDOM from 'react-dom'
4+
import App from './App'
5+
6+
const div = document.getElementById('app')
7+
8+
ReactDOM.render(<App />, div)
9+

demo/index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<style>
3+
* { box-sizing: border-box }
4+
body {
5+
font-family: -apple-system, sans-serif;
6+
margin: 0;
7+
}
8+
.debug, .debug * {
9+
outline: 1px solid rgba(0, 127, 255, .5);
10+
}
11+
.debug div {
12+
outline: 1px solid rgba(255, 0, 255, .5);
13+
}
14+
</style>
15+
<div id='app' class='debug'></div>
16+
<script src='bundle.js'></script>

dist/Grid.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _createGrid = require('./createGrid');
8+
9+
var _createGrid2 = _interopRequireDefault(_createGrid);
10+
11+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12+
13+
var Grid = (0, _createGrid2.default)('div');
14+
15+
exports.default = Grid;

dist/create-styles.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var width = function width(col) {
8+
return col / 12 * 100 + '%';
9+
};
10+
11+
var createClassName = function createClassName(breakpoint) {
12+
return function (col) {
13+
return '_g' + breakpoint + col;
14+
};
15+
};
16+
var createRule = function createRule(breakpoints) {
17+
return function (breakpoint) {
18+
return function (className) {
19+
return function (col) {
20+
if (typeof col !== 'number') {
21+
return '';
22+
}
23+
var media = breakpoints[breakpoint];
24+
var rule = '.' + className + '{width:' + width(col) + '}';
25+
if (!media) {
26+
return rule;
27+
}
28+
return '@media ' + media + '{' + rule + '}';
29+
};
30+
};
31+
};
32+
};
33+
34+
var createStyles = function createStyles(breakpoints) {
35+
return function (props) {
36+
var createBreakpointRule = createRule(breakpoints);
37+
var styles = Object.keys(props).map(function (key) {
38+
var val = props[key];
39+
if (!val) {
40+
return false;
41+
}
42+
var className = createClassName(key)(val);
43+
var rule = createBreakpointRule(key)(className)(val);
44+
45+
return {
46+
className: className,
47+
rule: rule
48+
};
49+
}).filter(function (s) {
50+
return s !== false;
51+
});
52+
53+
var css = styles.reduce(function (a, b) {
54+
return a + b.rule;
55+
}, '');
56+
57+
var className = styles.reduce(function (a, b) {
58+
return a + b.className + ' ';
59+
}, '').trim();
60+
61+
return { css: css, className: className };
62+
};
63+
};
64+
65+
exports.default = createStyles;

dist/createGrid.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.createGrid = undefined;
7+
8+
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
9+
10+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
11+
12+
var _react = require('react');
13+
14+
var _react2 = _interopRequireDefault(_react);
15+
16+
var _robox = require('robox');
17+
18+
var _robox2 = _interopRequireDefault(_robox);
19+
20+
var _createStyles2 = require('./create-styles');
21+
22+
var _createStyles3 = _interopRequireDefault(_createStyles2);
23+
24+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25+
26+
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
27+
28+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
29+
30+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
31+
32+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
33+
34+
var breakpoints = {
35+
sm: 'screen and (min-width:40em)',
36+
md: 'screen and (min-width:52em)',
37+
lg: 'screen and (min-width:64em)'
38+
};
39+
40+
var createGrid = exports.createGrid = function createGrid(Comp) {
41+
var Box = (0, _robox2.default)(Comp);
42+
43+
var Wrapped = function (_React$Component) {
44+
_inherits(Wrapped, _React$Component);
45+
46+
function Wrapped() {
47+
_classCallCheck(this, Wrapped);
48+
49+
return _possibleConstructorReturn(this, Object.getPrototypeOf(Wrapped).apply(this, arguments));
50+
}
51+
52+
_createClass(Wrapped, [{
53+
key: 'render',
54+
value: function render() {
55+
var _props = this.props;
56+
var _props$col = _props.col;
57+
var col = _props$col === undefined ? 12 : _props$col;
58+
var sm = _props.sm;
59+
var md = _props.md;
60+
var lg = _props.lg;
61+
var children = _props.children;
62+
63+
var props = _objectWithoutProperties(_props, ['col', 'sm', 'md', 'lg', 'children']);
64+
65+
var _createStyles = (0, _createStyles3.default)(breakpoints)({ col: col, sm: sm, md: md, lg: lg });
66+
67+
var css = _createStyles.css;
68+
var className = _createStyles.className;
69+
70+
71+
var cx = className + ' ' + (props.className || '');
72+
var sx = _extends({
73+
boxSizing: 'border-box',
74+
display: 'inline-block'
75+
}, props.style);
76+
77+
return _react2.default.createElement(
78+
Box,
79+
_extends({}, props, { style: sx, className: cx }),
80+
_react2.default.createElement('style', { dangerouslySetInnerHTML: { __html: css } }),
81+
children
82+
);
83+
}
84+
}]);
85+
86+
return Wrapped;
87+
}(_react2.default.Component);
88+
89+
return Wrapped;
90+
};
91+
92+
exports.default = createGrid;

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
module.exports = require('./dist/Grid').default
3+
module.exports.createGrid = require('./dist/createGrid').default
4+
module.exports.createStyles = require('./dist/create-styles').default
5+

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "react-css-grid",
3+
"version": "1.0.0-beta.1",
4+
"description": "Responsive CSS-based React grid component",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "webpack-dev-server",
8+
"prepublish": "babel src --out-dir dist",
9+
"test": "ava -v"
10+
},
11+
"keywords": [
12+
"react",
13+
"react-component",
14+
"grid",
15+
"css"
16+
],
17+
"author": "Brent Jackson",
18+
"license": "MIT",
19+
"devDependencies": {
20+
"ava": "^0.15.2",
21+
"babel-cli": "^6.11.4",
22+
"babel-loader": "^6.2.4",
23+
"babel-preset-es2015": "^6.9.0",
24+
"babel-preset-react": "^6.11.1",
25+
"babel-preset-stage-0": "^6.5.0",
26+
"babel-register": "^6.11.6",
27+
"enzyme": "^2.4.1",
28+
"react": "^15.2.1",
29+
"react-addons-test-utils": "^15.2.1",
30+
"react-dom": "^15.2.1",
31+
"webpack": "^1.13.1",
32+
"webpack-dev-server": "^1.14.1"
33+
},
34+
"dependencies": {
35+
"robox": "^1.0.0-beta.1"
36+
}
37+
}

src/Grid.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
import createGrid from './createGrid'
3+
4+
const Grid = createGrid('div')
5+
6+
export default Grid
7+

src/create-styles.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
const width = col => `${col / 12 * 100}%`
3+
4+
const createClassName = breakpoint => col => `_g${breakpoint}${col}`
5+
6+
const createRule = breakpoints => breakpoint => className => col => {
7+
if (typeof col !== 'number') {
8+
return ''
9+
}
10+
const media = breakpoints[breakpoint]
11+
const rule = `.${className}{width:${width(col)}}`
12+
if (!media) {
13+
return rule
14+
}
15+
return `@media ${media}{${rule}}`
16+
}
17+
18+
const createStyles = breakpoints => props => {
19+
const createBreakpointRule = createRule(breakpoints)
20+
21+
const styles = Object.keys(props).map(key => {
22+
const val = props[key]
23+
if (!val) {
24+
return false
25+
}
26+
const className = createClassName(key)(val)
27+
const rule = createBreakpointRule(key)(className)(val)
28+
29+
return {
30+
className,
31+
rule
32+
}
33+
}).filter(s => s !== false)
34+
35+
const css = styles.reduce((a, b) => {
36+
return a + b.rule
37+
}, '')
38+
39+
const className = styles.reduce((a, b) => {
40+
return a + b.className + ' '
41+
}, '').trim()
42+
43+
return { css, className }
44+
}
45+
46+
export default createStyles
47+

0 commit comments

Comments
 (0)