GithubHelp home page GithubHelp logo

dragma / styled-bootstrap-grid Goto Github PK

View Code? Open in Web Editor NEW
167.0 6.0 39.0 2.61 MB

Full Twitter Bootstrap v4 grid system implementation

License: MIT License

JavaScript 39.28% HTML 3.78% CSS 2.04% TypeScript 54.84% Shell 0.05%

styled-bootstrap-grid's Introduction

styled-bootstrap-grid

npm downloads npm version definition types

Credits

This module is based on the styled-components module.

This module is highly inspired by the awesome work done on the react-bootstrap module.

This module is also based on the Twitter Bootstrap v4.1.3 bootstrap-grid.css. The css provided to styled bootstrap grid is not mine.

For more information about how does this grid system works (I mean with classes like containers, row, col, offset, push) , please refer to the official Twitter Bootstrap layout documentation.

Install

npm i -S styled-bootstrap-grid

Prerequisites

npm i -S react styled-components

Bootstrap is developed mobile first, a strategy in which we optimize code for mobile devices first and then scale up components as necessary using CSS media queries. To ensure proper rendering and touch zooming for all devices, add the responsive viewport meta tag to your <head>. from Bootstrap documentation

<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

You also must inject the bootstrap base CSS in your application root file, like this.

// app.js

import { BaseCSS } from 'styled-bootstrap-grid';

export default (props) =>
  <Whatever>
    <BaseCSS />
  </Whatever>;

You also can inject your own css like this :

import { BaseCSS } from 'styled-bootstrap-grid';

const customCSS = `
  body {
    color: red;
  }
`;

export default (props) =>
  <Whatever>
    <BaseCSS css={customCSS} />
  </Whatever>;

Basically, BaseCSS takes a string prop, and append the default bootstrap layout base CSS with this string.

the default bootstrap layout CSS is :

html {
  -webkit-box-sizing: border-box;
          box-sizing: border-box;
  -ms-overflow-style: scrollbar;
}

*,
*::before,
*::after {
  -webkit-box-sizing: inherit;
          box-sizing: inherit;
}

Basics

import React from 'react';
import { Container, Row, Col } from 'styled-bootstrap-grid';

export default (props) =>
  <Whatever>
    <Container>
      <Row>
        <Col col xl="1" lg="2" md="3" sm="6">
            Hello Bootstrap Layout
        </Col>
      </Row>
    </Container>
    <Container fluid>
      <Row>
        <Col col={6} sm={5} md={4} mdOffset={4}>
            Hello Bootstrap Fluid Layout
        </Col>
      </Row>
    </Container>
  </Whatever>;

Advanced

Custom gutter

The package exposes a GridThemeProvider that allows few custom theming properties. With this provider you can :

  • Define custom breakpoints
  • Change the Container padding left and right default value
  • Change the Row padding left and right default value
  • Change the Col padding left and right default value

The GridThemeProvider can be wrapped (or wrapped-in) the styled-component's ThemeProvider.

Example :

import React from 'react';
import ReactDOM from 'react-dom';
import { GridThemeProvider } from 'styled-bootstrap-grid';
import { ThemeProvider } from 'styled-components';

import App from './whatever/app/folder';

const gridTheme = {
  gridColumns: 24, // default 12
  breakpoints: { // defaults below
    xxl: 1440,
    xl: 1200,
    lg: 992,
    md: 768,
    sm: 576,
    xs: 575,
    // or you can use aliases
    // veryGiant: 1440,
    // giant: 1200,
    // desktop: 992,
    // tablet: 768,
    // phone: 576,
    // smaller: 575,
  },
  row: {
    padding: 10, // default 15
  },
  col: {
    padding: 5, // default 15
  },
  container: {
    padding: 0, // default 15
    maxWidth: { // defaults below
      xxl: 1141,
      xl: 1140,
      lg: 960,
      md: 720,
      sm: 540,
      xs: 540,
      // or you can use aliases
      // veryGiant: 1141,
      // giant: 1140,
      // desktop: 960,
      // tablet: 720,
      // phone: 540,
      // smaller: 540,
    },
  },
};
const styledTheme = {
  mainColor: 'purple',
}

ReactDOM.render(
  <ThemeProvider
    theme={styledTheme}
  >
    <GridThemeProvider
      gridTheme={gridTheme}
    >
      <App />
    </GridThemeProvider>
  </ThemeProvider>,
  document.getElementById('root')
);

MediaTypes

This packages also exposes the media element. It can be used in your styled-components like this :

import styled from 'styled-components';
import { media } from 'styled-bootstrap-grid';

const CustomDiv = styled.div`
  color: black;
  ${media.smaller`
    color: green;
  `}
  ${media.xs`
    color: green;
  `}
  ${media.phone`
    color: blue;
  `}
  ${media.sm`
    color: blue;
  `}
  ${media.tablet`
    color: red;
  `}
  ${media.md`
    color: red;
  `}
  ${media.desktop`
    color: purple;
  `}
  ${media.lg`
    color: purple;
  `}
  ${media.giant`
    color: yellow;
  `}
  ${media.xl`
    color: yellow;
  `}
  ${media.veryGiant`
    color: orange;
  `}
  ${media.xxl`
    color: orange;
  `}
`;

export default CustomDiv;

Using this media object will help you to build media-queries that will fit the same way as Bootstrap do.

name aliases css generated
xs smaller all sizes: @media (max-width: 575px) { /* */ }
sm phone @media (min-width: 576px) { /* */ }
md tablet @media (min-width: 768px) { /* */ }
lg desktop @media (min-width: 992px) { /* */ }
xl giant @media (min-width: 1200px) { /* */ }
xxl veryGiant @media (min-width: 1440px) { /* */ }

Props definition

GridThemeProvider

props default type description
gridTheme undefined Object See description below(*)

(*)

const gridTheme = {
  gridColumns: 12, // default 12
  breakpoints: { // defaults below
    xxl: 1440,
    xl: 1200,
    lg: 992,
    md: 768,
    sm: 576,
    xs: 575,
    // or you can use aliases
    // veryGiant: 1440,
    // giant: 1200,
    // desktop: 992,
    // tablet: 768,
    // phone: 576,
    // smaller: 575,
  },
  row: {
    padding: 10, // default 15
  },
  col: {
    padding: 5, // default 15
  },
  container: {
    padding: 0, // default 15
    maxWidth: { // defaults below
      xxl: 1141,
      xl: 1140,
      lg: 960,
      md: 720,
      sm: 540,
      xs: 540,
      // or you can use aliases
      // veryGiant: 1141,
      // giant: 1140,
      // desktop: 960,
      // tablet: 720,
      // phone: 540,
      // smaller: 540,
    },
  },
}

Container

props default type description
fluid false boolean Equivalent to container and container-fluid

Plus the ones inherited from styled-components div.

Row

props default type description
alignItems undefined string start or end or center or baseline or stretch. Equivalent to align-items-{value}
smAlignItems undefined string start or end or center or baseline or stretch. Equivalent to align-items-sm-{value}
mdAlignItems undefined string start or end or center or baseline or stretch. Equivalent to align-items-md-{value}
lgAlignItems undefined string start or end or center or baseline or stretch. Equivalent to align-items-lg-{value}
xlAlignItems undefined string start or end or center or baseline or stretch. Equivalent to align-items-xl-{value}
justifyContent undefined string start or end or center or between or around. Equivalent to justify-content-{value}
smJustifyContent undefined string start or end or center or between or around. Equivalent to justify-content-sm-{value}
mdJustifyContent undefined string start or end or center or between or around. Equivalent to justify-content-md-{value}
lgJustifyContent undefined string start or end or center or between or around. Equivalent to justify-content-lg-{value}
xlJustifyContent undefined string start or end or center or between or around. Equivalent to justify-content-xl-{value}

Plus the ones inherited from styled-components div.

Col

props default type description
col undefined number or string or boolean Goes from 1 to 12. Equivalent to col-* (or col in case of true)
offset undefined number or string Goes from 0 to 11. Equivalent to offset-*
auto undefined boolean Equivalent to col-auto
alignSelf undefined string auto or start or end or center or baseline or stretch. Equivalent to align-self-{value}
order undefined number or string first or last or 0 to 12. Equivalent to order-{value}
noGutter undefined boolean Equivalent to no-gutter
sm undefined number or string Goes from 1 to 12. Equivalent to col-sm-* (or col-sm in case of true)
hiddenXsUp undefined boolean Hides content from xs breakpoint to infinity
hiddenXsDown undefined boolean Hides content from xs breakpoint to 0
smOffset undefined number or string Goes from 0 to 11. Equivalent to offset-sm-*
smAuto undefined boolean Equivalent to col-sm-auto
smAlignSelf undefined string auto or start or end or center or baseline or stretch. Equivalent to align-self-sm-{value}
smOrder undefined number or string first or last or 0 to 12. Equivalent to order-sm-{value}
hiddenSmUp undefined boolean Hides content from sm breakpoint to infinity
hiddenSmDown undefined boolean Hides content from sm breakpoint to 0
md undefined number or string Goes from 1 to 12. Equivalent to col-md-* (or col-md in case of true)
mdOffset undefined number or string Goes from 0 to 11. Equivalent to offset-md-*
mdAuto undefined boolean Equivalent to col-md-auto
mdAlignSelf undefined string auto or start or end or center or baseline or stretch. Equivalent to align-self-md-{value}
mdOrder undefined number or string first or last or 0 to 12. Equivalent to order-md-{value}
hiddenMdUp undefined boolean Hides content from md breakpoint to infinity
hiddenMdDown undefined boolean Hides content from md breakpoint to 0
lg undefined number or string Goes from 1 to 12. Equivalent to col-lg-* (or col-lg in case of true)
lgOffset undefined number or string Goes from 0 to 11. Equivalent to offset-lg-*
lgAuto undefined boolean Equivalent to col-lg-auto
lgAlignSelf undefined string auto or start or end or center or baseline or stretch. Equivalent to align-self-lg-{value}
lgOrder undefined number or string first or last or 0 to 12. Equivalent to order-lg-{value}
hiddenLgUp undefined boolean Hides content from lg breakpoint to infinity
hiddenLgDown undefined boolean Hides content from lg breakpoint to 0
xl undefined number or string Goes from 1 to 12. Equivalent to col-xl-* (or col-xl in case of true)
xlOffset undefined number or string Goes from 0 to 11. Equivalent to offset-xl-*
xlAuto undefined boolean Equivalent to col-xl-auto
xlAlignSelf undefined string auto or start or end or center or baseline or stretch. Equivalent to align-self-xl-{value}
xlOrder undefined number or string first or last or 0 to 12. Equivalent to order-xl-{value}
hiddenXlUp undefined boolean Hides content from xl breakpoint to infinity
hiddenXlDown undefined boolean Hides content from xl breakpoint to 0

Plus the ones inherited from styled-components div.

Run example

To run the example

  1. Open a terminal and go to example's directory with cd <styled-bootstrap-grid folder path>/example
  2. Install example's dependencies with yarn
  3. Run yarn start

Dependencies

todo

  • complete web documentation

Any other idea ? Please leave a suggestion.

styled-bootstrap-grid's People

Contributors

afzalh avatar chihfanchiu avatar damassi avatar dragma avatar fhou avatar hector-del-rio avatar igorzanelladev avatar jeppeolesen avatar jipperism avatar jnv avatar nicolaschenet avatar peterlazzarino avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

styled-bootstrap-grid's Issues

Column Width issue

I m creating 3 column. But third column goes into bottom instead of same row.

Feature request: lessThan media type

It would be nice to have "less than" functionality with the media api.

${media.lessThan.md``}  // @media (max-width: 768px)
${media.md``}  // @media (min-width: 768px)

Feature request: Support margin utilities: ml-*, mr-*

It would be nice to have support for the Bootstrap 4 margin utilities.

For example, currently to push column to the right you have to something like this.

const LeftCol = styled(Col)`
  margin-right: auto;
`;

<Row>
  <LeftCol auto>on the left</LeftCol>
  <Col auto>pushed to the right</Col>
</Row>

Nicer syntax would be:

<Row>
  <Col auto mr-auto>on the left</Col>
  <Col auto>pushed to the right</Col>
</Row>

`BaseCSS` is throwing an error

I'm getting this error. I'm simply importing the BaseCSS component and adding it to the App.js render function. Am I missing something here?

Uncaught TypeError: (0 , _styledComponents.createGlobalStyle) is not a function
    at Object../node_modules/styled-bootstrap-grid/dist/BaseCSS.js (BaseCSS.js:25)
    at __webpack_require__ (bootstrap 54c27fd1532ace9c0c1f:712)
    at fn (bootstrap 54c27fd1532ace9c0c1f:117)
    at Object../node_modules/styled-bootstrap-grid/dist/index.js (index.js:43)
    at __webpack_require__ (bootstrap 54c27fd1532ace9c0c1f:712)
    at fn (bootstrap 54c27fd1532ace9c0c1f:117)
    at Object.<anonymous> (App.js:3)
    at Object../www/events/App.js (events.bundle.js:3992)
    at __webpack_require__ (bootstrap 54c27fd1532ace9c0c1f:712)
    at fn (bootstrap 54c27fd1532ace9c0c1f:117)

Request: Number of columns prop for Row

In Bootstrap you can set the number of columns in a row like this:

<div class="container">
  <div class="row row-cols-2">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
  </div>
</div>

I would like to see that supported by styled-bootstrap-grid. The example above would preferably translate to something like:

<Container>
  <Row cols={2}>
    <Col>Column</Col>
    <Col>Column</Col>
    <Col>Column</Col>
    <Col>Column</Col>
  </Row>
</Container> 

Custom css appends a class over current css

If custom css is passed as follows:

const customCSS = `
  html, body {
     margin: 0;
  }
`;

<BaseCSS css={customCSS} />

It comes in output as

   .xyas html, xyas body {
     margin: 0;
  }

Where this random class name xyas came from? Obviously, it looks like styled-components has added the class, but then it is random in every refresh, how do I set it on my HTML? Because this custom CSS is not getting applied because of this.

Just in case if it helps
React & React DOM: 17.0.1
next: 10.0.5
styled-components: 5.2.1
styled-bootstrap-grid: 3.1.0

hidden{bp}Up media query misses 1px math

When I have
< Col col={3} hiddenXlDown>...</ Col> < Col col={6} hiddenXlUp>...</ Col>
having xl breakpoint set to 1920
on the exact window width of 1920px none of the elements are showing.

Changelog for breaking changes?

Upgrading from 1x => 3x here —

  • The 2.0.0 breaking change was forwardRef?
  • The 3.0.0 breaking change was migrating to TypeScript (?) — what broke in this release?

Thanks!

Configurable unit values

Is it possible to to overwrite the default gutter and breakpoint values?

I'd like to be able to pass in my own configuration

Usage of !important for components styling causes error on AMP pages.

I tried using this package for an AMP page and noticed that I was getting errors related to the usage of !important in my styles. After doing some research I found out this was because the package uses !important for some of its stylings as you can see here

Question:

  1. Is there any specific reason why !important is used in these cases?
  2. Is there a workaround for this?

GridThemeProvider does not work as expected

According to the documentation, I should provide theme like this:

<GridThemeProvider gridTheme={theme}>

It does not work in this way. It works only if I use grittheme instead of gridTheme. Some typo?

Another issue is that it does not work at all if I provide partly configuration. I need to provide full theme which is kind of annoying.

Flex direction support

Hello,
Is it possible to support other flex direction properties like column etc ?
Currently I'm overriding Row styles when I want to have direction other than row but would be great if library could support it.

GridThemeProvider needs children prop

I get a typescript error, because the GridThemeProvider doesn't have a children prop:

Property 'children' does not exist on type 'IntrinsicAttributes & ThemeProps'

<GridThemeProvider gridTheme={gridTheme}><ChildComponent /></GridThemeProvider>

Bug: Typescript support issues

I get An index signature parameter type cannot be a union type. Consider using a mapped object type instead. when trying to use this library with typescript.

Apparently, the issue is coming from the labels in the index.d.ts file for the media object (line 119).
Here there is a discussion about this --> microsoft/TypeScript#24220

Is it just me or am I missing something in the configuration?

Col offset breaks other breakpoints

When a value for offset={} is provided in <Col>, all the other breakpoints will not work.

Example

Working

<Row>
    <Col col={12} lg={9} lgOffset={3}>
        <Pre>Item</Pre>
    </Col>
</Row>

Broken

<Row>
    <Col col={12} offset={0} lg={9} lgOffset={3}>
        <Pre>Item</Pre>
    </Col>
</Row>

In the broken example, the values for lg={} won't be respected.

Expected Result

The broken example should work too since the offset={} should not affect lg={}.

Reproducible Test Case

CodeSandBox: resize the window to see changes.

Reproducible in the latest version 3.1.0.

Include typescript declaration

Hey, thanks for the library. Looks very good. Have you already make a pull request to @types?
Else why not include the declaration file in your package?

Media min and max

Would be nice to have media as

xs-min
xs-max
sm-min
sm-max
md-min
md-max
lg-min
lg-max
xl-min
xl-max

so you can apply styles for example like:

${media.lg-min && media.lg-max`
    color: red;
` }

Feature Request: Get current breakpoint

Hello and thank you for your lib!

My problem that i need to activate some JS components only on some breakpoints.
Could you add class with current breakpoint (like media) for importing from styled-bootstrap-grid component?

Thank you for answer!

Adding extra breakpoints trough gridThemeProvider

This is awesome. However I found 2 issues regarding breakpoint

  1. xs and xxl breakpoints are not adding to generate css
  2. when I tried to add extra breakpoints they are now adding to generated css.

const gridTheme = {
gridColumns: 12
breakpoints: {
xxxl: 1920,
xxl: 1440,
xl: 1200,
lg: 992,
md: 768,
sm: 640,
xs: 575,
},
}

it is great if we can get this fixed.

Thanks

Missing support for styled-components v4

I cannot use styled-bootstrap-grid together with styled-components v4.

I am getting TypeError: (0 , _styledComponents.injectGlobal) is not a function error as injectGlobal was removed and replaced by createGlobalStyle in styled-components v4.

Wrong data-name on hiddenMdDown/Up, hiddenLgDown/Up, hiddenXlDown/Up

The problem is in this file, on lines 38, 39, 47, 48, 56, 57.

38 p.hiddenMdDown && `hidden-xs-down`,
39 p.hiddenMdUp && `hidden-xs-up`,
...
47  p.hiddenLgDown && `hidden-xs-down`,
48 p.hiddenLgUp && `hidden-xs-up`,
...
56 p.hiddenXlDown && `hidden-xs-down`,
57 p.hiddenXlUp && `hidden-xs-up`,

I'm not sure how this affects the code elsewhere, but from a quick glance I can see that the data-names are incorrect. The correct data-names should correctly reflect the Md, Lg and Xl properties instead of referencing xs all the time.

I could create a PR for this but I'm not sure if this is enough to solve the problem. Anyone care to elaborate?

Breakpoint sensitive paddings

I would like to have different paddings between my rows for each breakpoint.
Currently only a fixed padding for rows/columns/containers can be set, which would require me to use no-gutters or to build my own styled-row-component with media querries to work around this, which seems counter-intuitive.

Would it be possible to allow something like the following for the gridTheme configuration?

    col: {
        padding: {
            xl: 72,
            lg: 72,
            md: 64,
            sm: 32,
            xs: 15,
        },
    },

Container Fluid max-width

Is there a way to set max-width for <Container fluid>?
If not, it would be nice to have an option.
Thanks

Console logs

The issues is that console outputs debugging info such as {styledBootstrapGrid: {…}}.

With yesterday's changes I see console.log(myTheme) was introduced in src/ThemeProvider.jsx. It was for debugging purposes I guess.

npm install error v3.0.3

Hi,

when I install version 3.0.3

I get the error

npm ERR! path /app/node_modules/styled-bootstrap-grid
npm ERR! code EISGIT
npm ERR! git /app/node_modules/styled-bootstrap-grid: Appears to be a git repo or submodule.
npm ERR! git /app/node_modules/styled-bootstrap-grid
npm ERR! git Refusing to remove it. Update manually,
npm ERR! git or move it out of the way first.

There is .git folder in node-modules which causes the problem.
Can you remove it please

Container width depends on configuration

Hi. Your grid have hard-coded styles

${media.phone`
    max-width: 540px;
  `}

  ${media.tablet`
    max-width: 720px;
  `}

  ${media.desktop`
    max-width: 960px;
  `}

  ${media.giant`
    max-width: 1140px;
  `}

Please, make it configurable.

Git submodule

My build process has a sad time because it thinks styled-bootstrap-grid is a git submodule, it looks like a .git directory is included inside the node module :(

Aug 30 08:34:28 PM  npm ERR! path /opt/render/project/src/node_modules/styled-bootstrap-grid
Aug 30 08:34:28 PM  npm ERR! code EISGIT
Aug 30 08:34:28 PM  npm ERR! git /opt/render/project/src/node_modules/styled-bootstrap-grid: Appears to be a git repo or submodule.
Aug 30 08:34:28 PM  npm ERR! git     /opt/render/project/src/node_modules/styled-bootstrap-grid
Aug 30 08:34:28 PM  npm ERR! git Refusing to remove it. Update manually,
Aug 30 08:34:28 PM  npm ERR! git or move it out of the way first.

Example | Can't resolve './styled-bootstrap-grid'

Hey @dragma , I was just trying to run your example, but I'm facing a little technical issue. Here's what I get when I run npm start on the example

Module not found: Can't resolve './styled-bootstrap-grid'

Node version: v9.11.2
NPM version: v5.6.0

(The library is built)

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.