Permalink
Browse files

Adjust tests

  • Loading branch information...
1 parent 60b60aa commit f1a43834ea23fec31b0f141cd72472e189f0b2d0 @jxnblk committed Jul 31, 2016
Showing with 220 additions and 218 deletions.
  1. +2 −0 .gitignore
  2. 0 .npmignore
  3. +3 −0 .travis.yml
  4. +33 −1 README.md
  5. +12 −13 demo/App.js
  6. +2 −0 demo/index.html
  7. +0 −15 dist/Grid.js
  8. +0 −65 dist/create-styles.js
  9. +0 −92 dist/createGrid.js
  10. +14 −7 package.json
  11. +34 −11 src/create-styles.js
  12. +21 −14 src/createGrid.js
  13. +83 −0 test/Grid.js
  14. +16 −0 test/index.js
View
@@ -0,0 +1,2 @@
+
+dist
View
No changes.
View
@@ -0,0 +1,3 @@
+language: node_js
+node_js:
+ - 6.2
View
@@ -1,13 +1,17 @@
# React CSS Grid
+**Experimental**
Responsive CSS-based React grid component
+[![Build Status](https://travis-ci.org/jxnblk/understyle.svg?branch=master)](https://travis-ci.org/jxnblk/understyle)
+[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
+
```sh
npm i react-css-grid
```
-```js
+```jsx
// Example usage
import React from 'react'
import Grid from 'react-css-grid'
@@ -26,7 +30,35 @@ class App extends React.Component {
}
```
+```jsx
+// Higher order component
+import React from 'react'
+import { createGrid } from 'react-css-grid'
+
+const CustomGrid = (props) => <div {...props} />
+
+export default createGrid(CustomGrid)
+```
+
+## Features
+- Simple, encapsulated grid layout API
+- Uses CSS for native @media-rule-based responsive styles
+- Works with server-side rendering
+
+## Grid component props
+- `col` (number 0–12) sets width across all breakpoints based on a 12 column grid.
+- `sm` (number 0–12) sets width from the `sm` breakpoint up
+- `md` (number 0–12) sets width from the `md` breakpoint up
+- `lg` (number 0–12) sets width from the `lg` breakpoint up
+- `align` (string, `top`, `middle`, `bottom`, or `baseline`) - sets vertical align
+
+## How it works
+
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.
+## Caveats
+- Produces an inline style tag within the body
+- Similar component instances create duplicative CSS rules – this may or may not affect performance
+- Atomic class selectors are global
MIT License
View
@@ -1,28 +1,27 @@
import React from 'react'
-import Grid from '..'
-
+import Grid from '../src/Grid'
class App extends React.Component {
render () {
return (
- <Grid p={3}>
- <h1>Hello react-css-grid</h1>
+ <div className=''>
+ <h1 className='p3'>React CSS Grid</h1>
<div>
- <Grid sm={6} md={3} p={2}>
- sm6 md3
+ <Grid className='p3' sm={6} md={3}>
+ <h2 className='m0'>sm6 md3</h2>
</Grid>
- <Grid sm={6} md={3} p={2}>
- sm6 md3
+ <Grid className='p3' sm={6} md={3}>
+ <h2 className='m0'>sm6 md3</h2>
</Grid>
- <Grid sm={6} md={3} p={2}>
- sm6 md3
+ <Grid className='p3' sm={6} md={3}>
+ <h2 className='m0'>sm6 md3</h2>
</Grid>
- <Grid sm={6} md={3} p={2}>
- sm6 md3
+ <Grid className='p3' sm={6} md={3}>
+ <h2 className='m0'>sm6 md3</h2>
</Grid>
</div>
- </Grid>
+ </div>
)
}
}
View
@@ -5,6 +5,8 @@
font-family: -apple-system, sans-serif;
margin: 0;
}
+.m0 { margin: 0 }
+.p3 { padding: 32px }
.debug, .debug * {
outline: 1px solid rgba(0, 127, 255, .5);
}
View
@@ -1,15 +0,0 @@
-'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;
View
@@ -1,65 +0,0 @@
-'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;
View
@@ -1,92 +0,0 @@
-'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;
View
@@ -4,9 +4,11 @@
"description": "Responsive CSS-based React grid component",
"main": "index.js",
"scripts": {
+ "build": "webpack -p",
"start": "webpack-dev-server",
- "prepublish": "babel src --out-dir dist",
- "test": "ava -v"
+ "gh-pages": "gh-pages -d demo",
+ "prepublish": "mkdir -p dist && babel src --out-dir dist",
+ "test": "standard && ava -v"
},
"keywords": [
"react",
@@ -25,13 +27,18 @@
"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",
+ "gh-pages": "^0.11.0",
+ "react": "^15.3.0",
+ "react-addons-test-utils": "^15.3.0",
+ "react-dom": "^15.3.0",
+ "standard": "^7.1.2",
"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"
},
- "dependencies": {
- "robox": "^1.0.0-beta.1"
+ "ava": {
+ "require": [
+ "babel-register"
+ ],
+ "babel": "inherit"
}
}
View
@@ -1,25 +1,30 @@
-const width = col => `${col / 12 * 100}%`
+export const width = col => `${col / 12 * 100}%`
-const createClassName = breakpoint => col => `_g${breakpoint}${col}`
+export const createClassName = prefix => base => `__${prefix}${base}`
-const createRule = breakpoints => breakpoint => className => col => {
+export const createWidthRule = 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}}`
+
+ return media ? `@media ${media}{${rule}}` : rule
+}
+
+export const createRule = className => prop => val => {
+ return `.${className}{${prop}:${val}}`
}
-const createStyles = breakpoints => props => {
- const createBreakpointRule = createRule(breakpoints)
+const createStyles = breakpoints => ({
+ align = 'top',
+ ...rest
+}) => {
+ const createBreakpointRule = createWidthRule(breakpoints)
- const styles = Object.keys(props).map(key => {
- const val = props[key]
+ const styles = Object.keys(rest).map(key => {
+ const val = rest[key]
if (!val) {
return false
}
@@ -32,6 +37,24 @@ const createStyles = breakpoints => props => {
}
}).filter(s => s !== false)
+ const boxSizingClassName = createClassName('b')('b')
+ styles.push({
+ className: boxSizingClassName,
+ rule: createRule(boxSizingClassName)('box-sizing')('border-box')
+ })
+
+ const displayClassName = createClassName('d')('ib')
+ styles.push({
+ className: displayClassName,
+ rule: createRule(displayClassName)('display')('inline-block')
+ })
+
+ const alignClassName = createClassName('a')(align)
+ styles.push({
+ className: alignClassName,
+ rule: createRule(alignClassName)('vertical-align')(align)
+ })
+
const css = styles.reduce((a, b) => {
return a + b.rule
}, '')
Oops, something went wrong.

0 comments on commit f1a4383

Please sign in to comment.