Permalink
Browse files

Init

  • Loading branch information...
0 parents commit 60b60aae7c9ac29ecb30a4412e15fa429def823b @jxnblk committed Jul 31, 2016
Showing with 440 additions and 0 deletions.
  1. +7 −0 .babelrc
  2. +32 −0 README.md
  3. +31 −0 demo/App.js
  4. +9 −0 demo/entry.js
  5. +16 −0 demo/index.html
  6. +15 −0 dist/Grid.js
  7. +65 −0 dist/create-styles.js
  8. +92 −0 dist/createGrid.js
  9. +5 −0 index.js
  10. +37 −0 package.json
  11. +7 −0 src/Grid.js
  12. +47 −0 src/create-styles.js
  13. +49 −0 src/createGrid.js
  14. +28 −0 webpack.config.js
@@ -0,0 +1,7 @@
+{
+ "presets": [
+ "es2015",
+ "stage-0",
+ "react"
+ ]
+}
@@ -0,0 +1,32 @@
+
+# React CSS Grid
+
+Responsive CSS-based React grid component
+
+```sh
+npm i react-css-grid
+```
+
+```js
+// Example usage
+import React from 'react'
+import Grid from 'react-css-grid'
+
+class App extends React.Component {
+ render () {
+ return (
+ <div>
+ <Grid col={6} sm={4} md={3} lg={2}>Column</Grid>
+ <Grid col={6} sm={4} md={3} lg={2}>Column</Grid>
+ <Grid col={6} sm={4} md={3} lg={2}>Column</Grid>
+ <Grid col={6} sm={4} md={3} lg={2}>Column</Grid>
+ </div>
+ )
+ }
+}
+```
+
+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.
+
+
+MIT License
@@ -0,0 +1,31 @@
+
+import React from 'react'
+import Grid from '..'
+
+
+class App extends React.Component {
+ render () {
+ return (
+ <Grid p={3}>
+ <h1>Hello react-css-grid</h1>
+ <div>
+ <Grid sm={6} md={3} p={2}>
+ sm6 md3
+ </Grid>
+ <Grid sm={6} md={3} p={2}>
+ sm6 md3
+ </Grid>
+ <Grid sm={6} md={3} p={2}>
+ sm6 md3
+ </Grid>
+ <Grid sm={6} md={3} p={2}>
+ sm6 md3
+ </Grid>
+ </div>
+ </Grid>
+ )
+ }
+}
+
+export default App
+
@@ -0,0 +1,9 @@
+
+import React from 'react'
+import ReactDOM from 'react-dom'
+import App from './App'
+
+const div = document.getElementById('app')
+
+ReactDOM.render(<App />, div)
+
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<style>
+* { box-sizing: border-box }
+body {
+ font-family: -apple-system, sans-serif;
+ margin: 0;
+}
+.debug, .debug * {
+ outline: 1px solid rgba(0, 127, 255, .5);
+}
+.debug div {
+ outline: 1px solid rgba(255, 0, 255, .5);
+}
+</style>
+<div id='app' class='debug'></div>
+<script src='bundle.js'></script>
@@ -0,0 +1,15 @@
+'use strict';
+
+Object.defineProperty(exports, "__esModule", {
+ value: true
+});
+
+var _createGrid = require('./createGrid');
+
+var _createGrid2 = _interopRequireDefault(_createGrid);
+
+function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
+
+var Grid = (0, _createGrid2.default)('div');
+
+exports.default = Grid;
@@ -0,0 +1,65 @@
+'use strict';
+
+Object.defineProperty(exports, "__esModule", {
+ value: true
+});
+
+var width = function width(col) {
+ return col / 12 * 100 + '%';
+};
+
+var createClassName = function createClassName(breakpoint) {
+ return function (col) {
+ return '_g' + breakpoint + col;
+ };
+};
+var createRule = function createRule(breakpoints) {
+ return function (breakpoint) {
+ return function (className) {
+ return function (col) {
+ if (typeof col !== 'number') {
+ return '';
+ }
+ var media = breakpoints[breakpoint];
+ var rule = '.' + className + '{width:' + width(col) + '}';
+ if (!media) {
+ return rule;
+ }
+ return '@media ' + media + '{' + rule + '}';
+ };
+ };
+ };
+};
+
+var createStyles = function createStyles(breakpoints) {
+ return function (props) {
+ var createBreakpointRule = createRule(breakpoints);
+ var styles = Object.keys(props).map(function (key) {
+ var val = props[key];
+ if (!val) {
+ return false;
+ }
+ var className = createClassName(key)(val);
+ var rule = createBreakpointRule(key)(className)(val);
+
+ return {
+ className: className,
+ rule: rule
+ };
+ }).filter(function (s) {
+ return s !== false;
+ });
+
+ var css = styles.reduce(function (a, b) {
+ return a + b.rule;
+ }, '');
+
+ var className = styles.reduce(function (a, b) {
+ return a + b.className + ' ';
+ }, '').trim();
+
+ return { css: css, className: className };
+ };
+};
+
+exports.default = createStyles;
@@ -0,0 +1,92 @@
+'use strict';
+
+Object.defineProperty(exports, "__esModule", {
+ value: true
+});
+exports.createGrid = undefined;
+
+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; };
+
+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; }; }();
+
+var _react = require('react');
+
+var _react2 = _interopRequireDefault(_react);
+
+var _robox = require('robox');
+
+var _robox2 = _interopRequireDefault(_robox);
+
+var _createStyles2 = require('./create-styles');
+
+var _createStyles3 = _interopRequireDefault(_createStyles2);
+
+function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
+
+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; }
+
+function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
+
+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; }
+
+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; }
+
+var breakpoints = {
+ sm: 'screen and (min-width:40em)',
+ md: 'screen and (min-width:52em)',
+ lg: 'screen and (min-width:64em)'
+};
+
+var createGrid = exports.createGrid = function createGrid(Comp) {
+ var Box = (0, _robox2.default)(Comp);
+
+ var Wrapped = function (_React$Component) {
+ _inherits(Wrapped, _React$Component);
+
+ function Wrapped() {
+ _classCallCheck(this, Wrapped);
+
+ return _possibleConstructorReturn(this, Object.getPrototypeOf(Wrapped).apply(this, arguments));
+ }
+
+ _createClass(Wrapped, [{
+ key: 'render',
+ value: function render() {
+ var _props = this.props;
+ var _props$col = _props.col;
+ var col = _props$col === undefined ? 12 : _props$col;
+ var sm = _props.sm;
+ var md = _props.md;
+ var lg = _props.lg;
+ var children = _props.children;
+
+ var props = _objectWithoutProperties(_props, ['col', 'sm', 'md', 'lg', 'children']);
+
+ var _createStyles = (0, _createStyles3.default)(breakpoints)({ col: col, sm: sm, md: md, lg: lg });
+
+ var css = _createStyles.css;
+ var className = _createStyles.className;
+
+
+ var cx = className + ' ' + (props.className || '');
+ var sx = _extends({
+ boxSizing: 'border-box',
+ display: 'inline-block'
+ }, props.style);
+
+ return _react2.default.createElement(
+ Box,
+ _extends({}, props, { style: sx, className: cx }),
+ _react2.default.createElement('style', { dangerouslySetInnerHTML: { __html: css } }),
+ children
+ );
+ }
+ }]);
+
+ return Wrapped;
+ }(_react2.default.Component);
+
+ return Wrapped;
+};
+
+exports.default = createGrid;
@@ -0,0 +1,5 @@
+
+module.exports = require('./dist/Grid').default
+module.exports.createGrid = require('./dist/createGrid').default
+module.exports.createStyles = require('./dist/create-styles').default
+
@@ -0,0 +1,37 @@
+{
+ "name": "react-css-grid",
+ "version": "1.0.0-beta.1",
+ "description": "Responsive CSS-based React grid component",
+ "main": "index.js",
+ "scripts": {
+ "start": "webpack-dev-server",
+ "prepublish": "babel src --out-dir dist",
+ "test": "ava -v"
+ },
+ "keywords": [
+ "react",
+ "react-component",
+ "grid",
+ "css"
+ ],
+ "author": "Brent Jackson",
+ "license": "MIT",
+ "devDependencies": {
+ "ava": "^0.15.2",
+ "babel-cli": "^6.11.4",
+ "babel-loader": "^6.2.4",
+ "babel-preset-es2015": "^6.9.0",
+ "babel-preset-react": "^6.11.1",
+ "babel-preset-stage-0": "^6.5.0",
+ "babel-register": "^6.11.6",
+ "enzyme": "^2.4.1",
+ "react": "^15.2.1",
+ "react-addons-test-utils": "^15.2.1",
+ "react-dom": "^15.2.1",
+ "webpack": "^1.13.1",
+ "webpack-dev-server": "^1.14.1"
+ },
+ "dependencies": {
+ "robox": "^1.0.0-beta.1"
+ }
+}
@@ -0,0 +1,7 @@
+
+import createGrid from './createGrid'
+
+const Grid = createGrid('div')
+
+export default Grid
+
@@ -0,0 +1,47 @@
+
+const width = col => `${col / 12 * 100}%`
+
+const createClassName = breakpoint => col => `_g${breakpoint}${col}`
+
+const createRule = breakpoints => breakpoint => className => col => {
+ if (typeof col !== 'number') {
+ return ''
+ }
+ const media = breakpoints[breakpoint]
+ const rule = `.${className}{width:${width(col)}}`
+ if (!media) {
+ return rule
+ }
+ return `@media ${media}{${rule}}`
+}
+
+const createStyles = breakpoints => props => {
+ const createBreakpointRule = createRule(breakpoints)
+
+ const styles = Object.keys(props).map(key => {
+ const val = props[key]
+ if (!val) {
+ return false
+ }
+ const className = createClassName(key)(val)
+ const rule = createBreakpointRule(key)(className)(val)
+
+ return {
+ className,
+ rule
+ }
+ }).filter(s => s !== false)
+
+ const css = styles.reduce((a, b) => {
+ return a + b.rule
+ }, '')
+
+ const className = styles.reduce((a, b) => {
+ return a + b.className + ' '
+ }, '').trim()
+
+ return { css, className }
+}
+
+export default createStyles
+
Oops, something went wrong.

0 comments on commit 60b60aa

Please sign in to comment.