2.2.2 • Published 6 years ago

react-configurable-grid v2.2.2

Weekly downloads
40
License
MIT
Repository
github
Last release
6 years ago

React Configurable Grid

npm GitHub tag

A simple configurable grid for react inspired on grid-styled and react-bootstrap API, but, with open settings.

1.08 kB only

Usage

React Configurable Grid is as simple as using bootstrap grid-system, where you have rows, cols and container classes to wrap components and align as you please, removing alignment and size adustment from their responsability.

Install

npm install react-configurable-grid --save

Basics

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

function MyComponent() {
    return (
        <section>
            <h1>My component</h1>
            <Row>
                <Col xs={6} md={3} lg={2}>content 1</Col>
                <Col xs={6} md={9} lg={10}>content 2</Col>
            <Row>
        </section>
    );
}

export default MyComponent;

Custom configuration

To use custom breakpoints or custom gutter, just execute configureGrid with config params before your App is mounted;

import React from 'react';
import configureGrid, { Row, Col } from 'react-configurable-grid';
import OtherComponent from './OtherComponent';

configureGrid({
    // xs media query will use default value
    sm: "576px",
    md: null, // this media query will not be rendered
    lg: "992px",
    xl: null, // this media query will not be rendered
    gutter: "30px"
});

function App() {
    return (
        <section>
            <h1>My component</h1>
            <Row>
                <Col lg={2}>content 1</Col>
                <Col lg={10}>content 2</Col>
            <Row>
            <OtherComponent />
        </section>
    );
}

export default App;

Server-Side Rendering

With express server

// server.js
...
import React from "react";
import { renderToString } from "react-dom/server";
import App from "../components/App";
...

/*
  first param - Configuration object
  second param - If true returns <style type="text/css" data-grid>grid css</style> tag, else, returns grid css only
*/
const gridStyles = configureGrid({
    gutter: "40px"
}, true);

export default () => (req, res) => {
  const componentString = renderToString(<App />);

  const generatedHTML = `
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        ${gridStyles}
    </head>
    <body>
        <div id="root">${componentString}</div>
    </body>
    </html>
  `;

  
  res.send(generatedHTML);
}

With NextJS

// pages/_document.js
...
import Document, { Head, Main, NextScript } from "next/document";
import flush from "styled-jsx/server";
import { configureGrid } from 'react-configurable-grid';
...

/*
  You can pass no arguments if you what to use default config, and return only css code
*/
const gridStyles = configureGrid();

export default class MyDocument extends Document {
  static getInitialProps({ renderPage }) {
    const { html, head, errorHtml, chunks } = renderPage();
    const styles = flush();
    return { html, head, errorHtml, chunks, styles };
  }

  render() {
    return (
      <html lang="pt-BR">
        <Head>
          <meta name="viewport" content="initial-scale=1.0, width=device-width" />
          {/* You need to add data-grid data attribute when using only css code */}
          <style type="text/css" data-grid>{gridStyles}</style>
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </html>
    );
  }
}

API Reference

Row

propdescriptiondefault
classNameclassName-
componentchanges base elementdiv
wrapflex-wrapwrap
alignalign-itemsstretch
justifyjustify-contentflex-start

Col

propdescription
classNameclassName
componentchanges base elementdiv
xsextra small cols
smsmall cols
mdmedium cols
lglarge cols
xlextra large cols

Configure grid

Changes default values. Also returns styles for server-side rendering

Media queries with null value will not be rendered

Configuration Object param:

config object attributedefault
xs0px or more
sm576px or more
md768px or more
lg992px or more
xl1200px or more
gutter30px

Return Tag param:

true - returns

<style type="text/css" data-grid>grid css string</style>

false - returns

grid css string

Signature

configureGrid([Object], [Bool]);

Special thanks

@malbernaz

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.1.11

6 years ago

1.1.10

6 years ago

1.1.9

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago