Grid Styled
Added support for styled-components 3.1.x
npm i grid-sc
Original repo
Responsive React grid system built with styled-components
Getting Started
npm i grid-styled
import React from 'react'import Flex Box from 'grid-styled' const App = <Flex> <Box = => Half width </Box> <Box = => Half width </Box> </Flex>
// Different widths at different breakpoints<Box =/> // Fixed pixel width<Box = /> // CSS value width<Box ='40em' />
// Padding<Box = /> // Padding top<Box = /> // Padding bottom<Box = /> // Padding left<Box = /> // Padding right<Box = /> // x-axis padding (left and right)<Box = /> // y-axis padding (top and bottom)<Box = />
// Margin<Box = /> // Margin top<Box = /> // Margin bottom<Box = /> // Margin left<Box = /> // Margin right<Box = /> // x-axis margin (left and right)<Box = /> // y-axis margin (top and bottom)<Box = />
// margin auto<Box ='auto' /> // negative margins<Box = />
// Display inline-block gridimport Grid from 'grid-styled' <div> <Grid =>Half</Grid> <Grid =>Half</Grid></div>
<Box />
The Box component handles width, margin and padding.
Props
width
(number|string|array)
Sets width, where numbers 0-1
are percentage values, larger numbers are pixel values, and strings are raw CSS values with units.
Pass an array to set different widths at different breakpoints for
responsive styles.
Margin and Padding Props
Both margin and padding props accept numbers, strings, and arrays as values.
Using a number from 0-4
will reference a step on the spacing scale.
Larger numbers are converted to pixel values.
Negative Numbers can be used to set negative margins and compensate for grid gutters.
Strings are passed directly for other valid CSS values.
Use array values to set different margin or padding values per breakpoint for responsive styles.
Margin and padding props follow a shorthand syntax for specifying direction.
m
: marginmt
: margin-topmr
: margin-rightmb
: margin-bottomml
: margin-leftmx
: margin-left and margin-rightmy
: margin-top and margin-bottomp
: paddingpt
: padding-toppr
: padding-rightpb
: padding-bottompl
: padding-leftpx
: padding-left and padding-rightpy
: padding-top and padding-bottom
flex
(string|array)
Sets the flex
property.
<Box ="1 1 auto" />
order
(number|string|array)
Sets the order
property.
<Box = />
is
(Component|string)
Sets the underlying HTML element.
<Box ="section" />
<Flex />
The Flex component extends the Box component and sets display flex. It also includes the following props:
align
(string|array) setsalign-items
justify
(string|array) setsjustify-content
direction
(string|array) setsflex-direction
wrap
(boolean|array) setsflex-wrap: wrap
column
(boolean) shortcut forflex-direction: column
<Grid />
The Grid component extends the Box component and sets display inline-block for an alternative to flexbox layout.
Responsive Styles
Most props accept arrays as values for mobile-first responsive styles, where the first value is for all breakpoints, then each value after is for a min-width media query from that breakpoint and up. The Box component uses styled-system for these props.
// 100% below the smallest breakpoint,// 50% from the next breakpoint and up,// and 25% from the next breakpoint and up<Box = /> // responsive margin<Box = /> // responsive padding<Box = />
Extending Components
Using styled-components, you can customize any of the grid-styled components' styles.
InlineFlex
// Example const InlineFlex = ` display: inline-flex;`
Max-Width Container
// Example const Container = ` max-width: 1024px; margin-left: auto; margin-right: auto;`
Auto Grid
Components can also be extended with React. This example creates components for a grid with set gutters where the columns expand to fill in the space.
// Exampleimport React from 'react'import Flex Box from 'grid-styled' const Row = <Flex = /> const Column = <Box = ="1 1 auto" />
Theming
Grid Styled uses smart defaults, but to customize the values,
use styled-components’ ThemeProvider
component.
import React from 'react'import ThemeProvider from 'styled-components' const App = <ThemeProvider = > <div> <Grid>Grid with custom spacing scale and breakpoints</Grid> </div> </ThemeProvider>
Breakpoints
The Grid component uses a mobile-first responsive approach,
where any value set works from that breakpoint and wider.
Breakpoints are hard-coded to the following min-widths: 40em
, 52em
, 64em
.
To customize, provide an array of numbers that will be converted to ems.
Spacing Scale
Grid Styled components' margin and padding props use a 4 step spacing scale to help keep things aligned and keep layouts consistent.
The default scale is based on an 8px/powers-of-two grid: [ 0, 8, 16, 32, 64 ]
,
which helps keep spacing consistent and elements aligned even when nesting components.