From 0ef9e057f6d3739a3843975660e5c7514475e621 Mon Sep 17 00:00:00 2001 From: Brent Jackson Date: Mon, 28 Aug 2017 12:43:27 -0400 Subject: [PATCH 01/14] Rewrite with CSS grid layout --- .babelrc | 3 +- .gitignore | 5 +-- .npmignore | 6 ++++ README.md | 55 ++++++++-------------------- demo/App.js | 33 ----------------- demo/entry.js | 9 ----- demo/index.html | 18 ---------- index.js | 5 --- package.json | 50 ++++++++++++++------------ src/Grid.js | 7 ---- src/create-styles.js | 70 ------------------------------------ src/createGrid.js | 56 ----------------------------- src/index.js | 36 +++++++++++++++++++ storybook/Grid.js | 27 ++++++++++++++ storybook/config.js | 29 +++++++++++++++ test.js | 29 +++++++++++++++ test.js.md | 50 ++++++++++++++++++++++++++ test.js.snap | Bin 0 -> 358 bytes test/Grid.js | 83 ------------------------------------------- test/index.js | 16 --------- webpack.config.js | 28 --------------- 21 files changed, 223 insertions(+), 392 deletions(-) delete mode 100644 demo/App.js delete mode 100644 demo/entry.js delete mode 100644 demo/index.html delete mode 100644 index.js delete mode 100644 src/Grid.js delete mode 100644 src/create-styles.js delete mode 100644 src/createGrid.js create mode 100644 src/index.js create mode 100644 storybook/Grid.js create mode 100644 storybook/config.js create mode 100644 test.js create mode 100644 test.js.md create mode 100644 test.js.snap delete mode 100644 test/Grid.js delete mode 100644 test/index.js delete mode 100644 webpack.config.js diff --git a/.babelrc b/.babelrc index 974a1ca..add9366 100644 --- a/.babelrc +++ b/.babelrc @@ -1,7 +1,6 @@ { "presets": [ - "es2015", - "stage-0", + "env", "react" ] } diff --git a/.gitignore b/.gitignore index ee09a8a..75f69c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ - dist -demo/bundle.js +.site +.nyc_output +coverage diff --git a/.npmignore b/.npmignore index e69de29..9b05c0c 100644 --- a/.npmignore +++ b/.npmignore @@ -0,0 +1,6 @@ +src +storybook +.site +.nyc_output +coverage +test.js* diff --git a/README.md b/README.md index 23a1796..79c0914 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,12 @@ # React CSS Grid -**Experimental** -Responsive CSS-based React grid component +React layout component based on [CSS Grid Layout][spec] -[![Build Status](https://travis-ci.org/jxnblk/react-css-grid.svg?branch=master)](https://travis-ci.org/jxnblk/react-css-grid) -[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) +[![Build Status][travis-badge]][travis] + +[travis-badge]: https://travis-ci.org/jxnblk/react-css-grid.svg?branch=master +[travis]: https://travis-ci.org/jxnblk/react-css-grid ```sh npm i react-css-grid @@ -19,49 +20,23 @@ import Grid from 'react-css-grid' class App extends React.Component { render () { return ( -
- Column - Column - Column - Column -
+ +
Column
+
Column
+
Column
+
Column
+
) } } ``` -```jsx -// Higher order component -import React from 'react' -import { createGrid } from 'react-css-grid' - -const CustomGrid = (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 uses `display: inline-block` to create grid layouts. -It creates CSS rules based on props and inserts 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. +- Simple, encapsulated grid layout API -## Caveats -- Produces an inline style **tag** within the body (e.g. not inline styles) -- Similar component instances create duplicative CSS rules – this may or may not affect performance -- Atomic class selectors are global +[spec]: https://www.w3.org/TR/css-grid-1/ MIT License diff --git a/demo/App.js b/demo/App.js deleted file mode 100644 index d87b66d..0000000 --- a/demo/App.js +++ /dev/null @@ -1,33 +0,0 @@ - -import React from 'react' -import Grid from '../src/Grid' - -class App extends React.Component { - render () { - return ( -
-
-

React CSS Grid Demo

- GitHub -
-
- -

sm6 md3

-
- -

sm6 md3

-
- -

sm6 md3

-
- -

sm6 md3

-
-
-
- ) - } -} - -export default App - diff --git a/demo/entry.js b/demo/entry.js deleted file mode 100644 index 7b734fa..0000000 --- a/demo/entry.js +++ /dev/null @@ -1,9 +0,0 @@ - -import React from 'react' -import ReactDOM from 'react-dom' -import App from './App' - -const div = document.getElementById('app') - -ReactDOM.render(, div) - diff --git a/demo/index.html b/demo/index.html deleted file mode 100644 index 0c2729c..0000000 --- a/demo/index.html +++ /dev/null @@ -1,18 +0,0 @@ - - -
- diff --git a/index.js b/index.js deleted file mode 100644 index b52ef3e..0000000 --- a/index.js +++ /dev/null @@ -1,5 +0,0 @@ - -module.exports = require('./dist/Grid').default -module.exports.createGrid = require('./dist/createGrid').default -module.exports.createStyles = require('./dist/create-styles').default - diff --git a/package.json b/package.json index f7e797f..f5c0a62 100644 --- a/package.json +++ b/package.json @@ -1,39 +1,43 @@ { "name": "react-css-grid", - "version": "1.0.0-beta.1", - "description": "Responsive CSS-based React grid component", - "main": "index.js", + "version": "1.0.0", + "description": "React layout component based on CSS Grid Layout", + "main": "dist/index.js", "scripts": { - "build": "webpack -p", - "start": "webpack-dev-server", - "gh-pages": "gh-pages -d demo", - "prepublish": "mkdir -p dist && babel src --out-dir dist", - "test": "standard && ava -v" + "prepublish": "babel src -d dist", + "start": "start-storybook -c storybook -p 8000", + "build": "build-storybook -c storybook -o .site", + "deploy": "storybook-to-ghpages", + "test": "nyc ava" }, "keywords": [ "react", "react-component", + "styled-components", + "layout", + "css-grid", "grid", "css" ], "author": "Brent Jackson", "license": "MIT", + "dependencies": { + "prop-types": "^15.5.10", + "styled-components": "^2.1.2" + }, "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", - "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" + "@storybook/react": "^3.2.8", + "@storybook/storybook-deployer": "^2.0.0", + "ava": "^0.22.0", + "babel-cli": "^6.26.0", + "babel-core": "^6.26.0", + "babel-preset-env": "^1.6.0", + "babel-preset-react": "^6.24.1", + "babel-register": "^6.26.0", + "nyc": "^11.1.0", + "react": "^15.6.1", + "react-dom": "^15.6.1", + "react-test-renderer": "^15.6.1" }, "ava": { "require": [ diff --git a/src/Grid.js b/src/Grid.js deleted file mode 100644 index 15a07c0..0000000 --- a/src/Grid.js +++ /dev/null @@ -1,7 +0,0 @@ - -import createGrid from './createGrid' - -const Grid = createGrid('div') - -export default Grid - diff --git a/src/create-styles.js b/src/create-styles.js deleted file mode 100644 index f300d20..0000000 --- a/src/create-styles.js +++ /dev/null @@ -1,70 +0,0 @@ - -export const width = col => `${col / 12 * 100}%` - -export const createClassName = prefix => base => `__${prefix}${base}` - -export const createWidthRule = breakpoints => breakpoint => className => col => { - if (typeof col !== 'number') { - return '' - } - const media = breakpoints[breakpoint] - const rule = `.${className}{width:${width(col)}}` - - return media ? `@media ${media}{${rule}}` : rule -} - -export const createRule = className => prop => val => { - return `.${className}{${prop}:${val}}` -} - -const createStyles = breakpoints => ({ - align = 'top', - ...rest -}) => { - const createBreakpointRule = createWidthRule(breakpoints) - - const styles = Object.keys(rest).map(key => { - const val = rest[key] - if (!val) { - return false - } - const className = createClassName(key)(val) - const rule = createBreakpointRule(key)(className)(val) - - return { - className, - rule - } - }).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 - }, '') - - const className = styles.reduce((a, b) => { - return a + b.className + ' ' - }, '').trim() - - return { css, className } -} - -export default createStyles - diff --git a/src/createGrid.js b/src/createGrid.js deleted file mode 100644 index b380912..0000000 --- a/src/createGrid.js +++ /dev/null @@ -1,56 +0,0 @@ - -import React from 'react' -import createStyles from './create-styles' - -const breakpoints = { - sm: 'screen and (min-width:40em)', - md: 'screen and (min-width:52em)', - lg: 'screen and (min-width:64em)' -} - -const cols = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] - -export const createGrid = (Comp) => { - class GridWrap extends React.Component { - render () { - const { - col = 12, - sm, - md, - lg, - align, - children, - ...props - } = this.props - - const { css, className } = createStyles(breakpoints)({ align, col, sm, md, lg }) - - const cx = className + ' ' + (props.className || '') - - return ( - - +
+ diff --git a/package.json b/package.json index f5c0a62..fa51098 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,12 @@ { "name": "react-css-grid", "version": "1.0.0", - "description": "React layout component based on CSS Grid Layout", + "description": "React layout component based on CSS Grid Layout and built with styled-components", "main": "dist/index.js", "scripts": { "prepublish": "babel src -d dist", - "start": "start-storybook -c storybook -p 8000", - "build": "build-storybook -c storybook -o .site", - "deploy": "storybook-to-ghpages", + "start": "webpack-dev-server", + "build": "webpack -p", "test": "nyc ava" }, "keywords": [ @@ -27,17 +26,22 @@ }, "devDependencies": { "@storybook/react": "^3.2.8", - "@storybook/storybook-deployer": "^2.0.0", "ava": "^0.22.0", "babel-cli": "^6.26.0", "babel-core": "^6.26.0", "babel-preset-env": "^1.6.0", "babel-preset-react": "^6.24.1", "babel-register": "^6.26.0", + "grid-styled": "^2.0.0-10", "nyc": "^11.1.0", "react": "^15.6.1", "react-dom": "^15.6.1", - "react-test-renderer": "^15.6.1" + "react-live": "^1.7.0", + "react-test-renderer": "^15.6.1", + "react-x-ray": "^1.0.0-2", + "refunk": "^1.0.0-2", + "webpack": "^3.5.5", + "webpack-dev-server": "^2.7.1" }, "ava": { "require": [ diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..38a12ff --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,37 @@ +const path = require('path') +const webpack = require('webpack') + +module.exports = { + entry: './docs/entry.js', + + output: { + path: path.join(__dirname, 'docs'), + filename: 'bundle.js' + }, + + resolve: { + alias: { + 'react-css-grid': path.join(__dirname, 'src') + } + }, + + module: { + rules: [ + { + test: /\.js$/, + exclude: /node_modules/, + loader: 'babel-loader' + } + ] + }, + + plugins: [ + new webpack.DefinePlugin({ + 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV) + }) + ], + + devServer: { + contentBase: 'docs/' + } +} From ca7148574cf330b186d53473287dea0fb277c030 Mon Sep 17 00:00:00 2001 From: Brent Jackson Date: Tue, 29 Aug 2017 21:01:24 -0400 Subject: [PATCH 05/14] Add docs --- README.md | 25 +++++- docs/App.js | 211 +++++++++++++++++++++++++++++++++++++++++++--- docs/BlockLink.js | 12 +++ docs/Code.js | 8 ++ docs/Flex.js | 8 ++ docs/Heading.js | 16 ++++ docs/Image.js | 10 +++ docs/Link.js | 7 ++ docs/List.js | 8 ++ docs/Live.js | 55 ++++++++++++ docs/Pre.js | 14 +++ docs/Text.js | 12 +++ docs/Tweet.js | 26 ++++++ docs/index.html | 16 ++++ src/index.js | 24 ++++-- 15 files changed, 431 insertions(+), 21 deletions(-) create mode 100644 docs/BlockLink.js create mode 100644 docs/Code.js create mode 100644 docs/Flex.js create mode 100644 docs/Heading.js create mode 100644 docs/Image.js create mode 100644 docs/Link.js create mode 100644 docs/List.js create mode 100644 docs/Live.js create mode 100644 docs/Pre.js create mode 100644 docs/Text.js create mode 100644 docs/Tweet.js diff --git a/README.md b/README.md index 1b1f198..acd8e11 100644 --- a/README.md +++ b/README.md @@ -43,17 +43,38 @@ class App extends React.Component { ## Props -- `width` (number or string) width at which child elements will break into columns - either a number pixel value or any valid CSS width value as a string -- `gap` (number or string) gutter (`grid-gap`) between columns - either a number pixel value or any valid CSS width value as a string +### `width` (number or string) + +Sets the width at which child elements will break into columns. +Pass a number for pixel values or a string for any other valid CSS length. + +```jsx + +``` + +### `gap` (number or string) + +Sets the gutter (`grid-gap`) between columns. +Pass a number for pixel values or a string for any other valid CSS length. + +```jsx + +``` + +### `align` (string) + +Sets `align-items` to control child element alignment. ## Browser Support See http://caniuse.com/#feat=css-grid + ## Related - [Grid Styled](https://github.com/jxnblk/grid-styled) +- [Styled System](https://github.com/jxnblk/styled-system) - [styled-components][sc] - [CSS Grid Layout Module][spec] - [CSS Grid Layout on MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout) diff --git a/docs/App.js b/docs/App.js index 6b3295f..9f221ac 100644 --- a/docs/App.js +++ b/docs/App.js @@ -1,21 +1,206 @@ import React from 'react' +import { createProvider } from 'refunk' +import XRay from 'react-x-ray' import Grid from 'react-css-grid' import { Box } from 'grid-styled' import Col from './Col' +import Heading from './Heading' +import Pre from './Pre' +import Text from './Text' +import Link from './Link' +import Flex from './Flex' +import BlockLink from './BlockLink' +import Image from './Image' +import Tweet from './Tweet' +import Code from './Code' +import List from './List' +import Live from './Live' const App = props => ( - - - -

React CSS Grid

- - -

- React layout component based on CSS Grid Layout and built with styled-components -

- -
-
+ +
+
Features @@ -125,12 +133,20 @@ const App = props => ( Gap
+
+ Align + +
- - GitHub - - Made by Jxnblk - + Footer + +
  • + GitHub +
  • +
  • + Made by Jxnblk +
  • +
    @@ -167,6 +183,13 @@ const gapExample = `

    Hi

    ` +const alignExample = ` +

    Hello

    +

    Hi

    +
    ` + const state = { width: 320, gap: 32, diff --git a/docs/Bar.js b/docs/Bar.js new file mode 100644 index 0000000..45f3ae3 --- /dev/null +++ b/docs/Bar.js @@ -0,0 +1,9 @@ +import styled from 'styled-components' +import { Box } from 'grid-styled' + +const Bar = styled(Box)` + color: white; + background-color: black; +` + +export default Bar diff --git a/docs/Pre.js b/docs/Pre.js index d8575b5..148f8ab 100644 --- a/docs/Pre.js +++ b/docs/Pre.js @@ -4,6 +4,8 @@ import { space } from 'styled-system' const Pre = styled.pre` font-family: Menlo, monospace; font-size: 14px; + max-width: 100%; + overflow: auto; ${space} ` diff --git a/docs/Slider.js b/docs/Slider.js new file mode 100644 index 0000000..52879b8 --- /dev/null +++ b/docs/Slider.js @@ -0,0 +1,33 @@ +import styled from 'styled-components' + +const Slider = styled.input` + display: block; + width: 100%; + margin: 0; + height: 2px; + cursor: pointer; + color: inherit; + background-color: rgba(255, 255, 255, .25); + border-radius: 0; + appearance: none; + + &::-webkit-slider-thumb { + width: 12px; + height: 12px; + background-color: currentcolor; + border: 0; + border-radius: 9999px; + appearance: none; + } + + &:focus { + outline: none; + background-color: rgba(255, 255, 255, .5); + } +` + +Slider.defaultProps = { + type: 'range' +} + +export default Slider diff --git a/src/index.js b/src/index.js index 61ff39f..3c8d5ef 100644 --- a/src/index.js +++ b/src/index.js @@ -4,7 +4,9 @@ import PropTypes from 'prop-types' const px = n => typeof n === 'number' ? n + 'px' : n const width = props => ({ - gridTemplateColumns: `repeat(auto-fit, minmax(${px(props.width)}, 1fr))` + // ['@media screen and (min-width:320px)']: { + gridTemplateColumns: `repeat(auto-fit, minmax(${px(props.width)}, 1fr))` + // } }) const gap = props => ({ @@ -20,7 +22,8 @@ const span = props => props.span ? ({ }) : null const Grid = styled.div([], { - display: 'grid' + display: 'grid', + maxWidth: '100%' }, width, gap, @@ -43,6 +46,8 @@ Grid.defaultProps = { gap: 32 } -Grid.Item = styled.div([], span) +Grid.Item = styled.div([], { + maxWidth: '100%' +}, span) export default Grid From d7d9e284edf57805fcbef5b7cca87e2a51f21eb4 Mon Sep 17 00:00:00 2001 From: Brent Jackson Date: Tue, 29 Aug 2017 22:23:58 -0400 Subject: [PATCH 07/14] Adjust docs styles --- docs/App.js | 32 ++++++++++++++++++-------------- docs/Button.js | 18 ++++++++++++++++++ docs/Label.js | 9 +++++++++ docs/Slider.js | 5 ++++- 4 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 docs/Button.js create mode 100644 docs/Label.js diff --git a/docs/App.js b/docs/App.js index 216b39e..5525cce 100644 --- a/docs/App.js +++ b/docs/App.js @@ -4,6 +4,8 @@ import XRay from 'react-x-ray' import Grid from 'react-css-grid' import { Box } from 'grid-styled' import Bar from './Bar' +import Button from './Button' +import Label from './Label' import Slider from './Slider' import Col from './Col' import Heading from './Heading' @@ -20,16 +22,13 @@ import Live from './Live' const App = props => ( - - -