react-flexbox-grid

A set of React components implementing flexboxgrid with the power of CSS Modules. 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 <span class="token operator">--</span>save react<span class="token operator">-</span>flexbox<span class="token operator">-</span>grid
JavaScript

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:

<span class="token punctuation">{</span>
  test<span class="token punctuation">:</span> <span class="token regex">/\.css$/</span><span class="token punctuation">,</span>
  loader<span class="token punctuation">:</span> <span class="token string">'style-loader!css-loader'</span><span class="token punctuation">,</span>
  include<span class="token punctuation">:</span> <span class="token regex">/flexboxgrid/</span>
<span class="token punctuation">}</span>
JavaScript
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 <span class="token operator">--</span>save<span class="token operator">-</span>dev style<span class="token operator">-</span>loader css<span class="token operator">-</span>loader
JavaScript

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

<span class="token punctuation">{</span>
  test<span class="token punctuation">:</span> <span class="token regex">/\.css$/</span><span class="token punctuation">,</span>
  loader<span class="token punctuation">:</span> <span class="token string">'style-loader!css-loader?modules'</span><span class="token punctuation">,</span>
  include<span class="token punctuation">:</span> <span class="token regex">/flexboxgrid/</span>
<span class="token punctuation">}</span>
JavaScript

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

<span class="token punctuation">{</span>
  test<span class="token punctuation">:</span> <span class="token regex">/\.css$/</span><span class="token punctuation">,</span>
  loader<span class="token punctuation">:</span> <span class="token string">'style-loader!css-loader!postcss-loader'</span><span class="token punctuation">,</span>
  include<span class="token punctuation">:</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> <span class="token string">'node_modules'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// oops, this also includes flexboxgrid</span>
  exclude<span class="token punctuation">:</span> <span class="token operator">/</span>flexboxgrid<span class="token operator">/</span> <span class="token comment">// so we have to exclude it</span>
<span class="token punctuation">}</span>
JavaScript

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

Isomorphic support

Try: this comment.

If this doesn’t work for you, use the build located in the dist directory. This build removes all .css imports and extracts the relevant css into react-flexbox-grid/dist/react-flexbox-grid.css.

Not using a bundler?

Use the pre-bundled build located in the dist directory. It contains a single umd js distributable and built css file.

Usage

Now you can import and use the components:

<span class="token keyword">import</span> React <span class="token keyword">from</span> <span class="token string">'react'</span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token punctuation">{</span> Grid<span class="token punctuation">,</span> Row<span class="token punctuation">,</span> Col <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'react-flexbox-grid'</span><span class="token punctuation">;</span>

<span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token keyword">extends</span> <span class="token class-name">React<span class="token punctuation">.</span>Component</span> <span class="token punctuation">{</span>
  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>
      
        
          

            Hello<span class="token punctuation">,</span> world<span class="token operator">!</span>
JavaScript

      
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
JavaScript

Gotcha

For the time being always use fluid for &lt;Grid&gt; to prevent horizontal overflow issues.

Subscribe to the Newsletter

Get our latest news,tutorials,guides,tips & deals delivered to your inbox.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

shares