Giter VIP home page Giter VIP logo

react-flexbox-grid's Introduction

react-flexbox-grid

npm version Build Status NPM Status

react-flexbox-grid is a set of React components that implement flexboxgrid.css. It even has an optional support for CSS Modules with some extra configuration.

http://roylee0704.github.io/react-flexbox-grid/

Setup

Installation

react-flexbox-grid can be installed as an npm package:

npm install --save react-flexbox-grid

Minimal configuration

The recommended way to use react-flexbox-grid is with a tool like webpack or Meteor, make sure you set it up to support requiring CSS files. For example, the minimum required loader configuration for webpack would look like this:

{
  test: /\.css$/,
  loader: 'style-loader!css-loader',
  include: /flexboxgrid/
}

react-flexbox-grid imports the styles from flexboxgrid, that's why we're configuring the loader for it.

CSS Modules

If you want to use CSS Modules (this is mandatory for versions earlier than v1), webpack's css-loader supports this by passing modules param in the loader configuration.

First, install style-loader and css-loader as development dependencies:

npm install --save-dev style-loader css-loader

Next, add a loader for flexboxgrid with CSS Modules enabled:

{
  test: /\.css$/,
  loader: 'style-loader!css-loader?modules',
  include: /flexboxgrid/
}

Make sure you don't have another CSS loader which also affects flexboxgrid. In case you do, exclude flexboxgrid, for example:

{
  test: /\.css$/,
  loader: 'style-loader!css-loader!postcss-loader',
  include: path.join(__dirname, 'node_modules'), // oops, this also includes flexboxgrid
  exclude: /flexboxgrid/ // so we have to exclude it
}

Otherwise you would end up with an obscure error because webpack stacks loaders together, it doesn't override them.

Isomorphic support

See this comment.

Not using a bundler?

If you want to use react-flexbox-grid the old-fashioned way, you must generate a build with all the CSS and JavaScript and include it in your index.html. The components will then be exposed in the window object.

Usage

Now you can import and use the components:

import React from 'react';
import { Grid, Row, Col } from 'react-flexbox-grid';

class App extends React.Component {
  render() {
    return (
      <Grid fluid>
        <Row>
          <Col xs={6} md={3}>
            Hello, world!
          </Col>
        </Row>
      </Grid>
    );
  }
}

Gotcha

For the time being always use fluid for <Grid> to prevent horizontal overflow issues.

Example

Looking for a complete example? Head over to react-flexbox-grid-example.

Advanced composition

We also export functions for generating Row and Column class names for use in other components.

For example, suppose you're using a third party component that accepts className and you would like it to be rendered as Col. You could do so by extracting the column sizing props that MyComponent uses and then pass the generated className on to SomeComponent

import React from 'react';
import { Row, Col, getRowProps, getColumnProps } from 'react-flexbox-grid';
// a component that accepts a className
import SomeComponent from 'some-package';

export default function MyComponent(props) {
  const colProps = getColumnProps(props);
  const rowProps = getRowProps(props);

  return (
    <form className={rowProps.className}>
      <SomeComponent classname={colProps.className} />
      <input value={props.value} onChange={props.onChange} />
    </form>
  );
}

MyComponent.propTypes = Object.assign({
  onChange: React.PropTypes.func.isRequired,
  value: React.PropTypes.string.isRequired,
}, Col.propTypes, Row.propTypes);  // Re-use existing Row and Col propType validations

// Can now be rendered as: <MyComponent end="sm" sm={8} value="my input value" onChange={...} />

Contributors

Roy Lee Helder Santana Matija Marohnić
Roy Lee Helder Santana Matija Marohnić

License

MIT

react-flexbox-grid's People

Contributors

ankitduseja avatar arshsingh avatar avocadowastaken avatar eemeli avatar gingur avatar heldr avatar hycner avatar irus avatar kgregory avatar mbrookes avatar nathanstitt avatar roylee0704 avatar silvenon avatar vipoo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.