Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rebassjs/grid
This package has moved and renamed
https://github.com/rebassjs/grid
css css-in-js emotion flexbox grid layout margin padding react responsive styled-components styled-system
Last synced: 3 months ago
JSON representation
This package has moved and renamed
- Host: GitHub
- URL: https://github.com/rebassjs/grid
- Owner: rebassjs
- License: mit
- Archived: true
- Created: 2016-10-14T02:43:02.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-08-06T15:51:01.000Z (over 5 years ago)
- Last Synced: 2024-05-02T01:13:45.255Z (8 months ago)
- Topics: css, css-in-js, emotion, flexbox, grid, layout, margin, padding, react, responsive, styled-components, styled-system
- Language: JavaScript
- Homepage: https://rebassjs.org/reflexbox
- Size: 1.88 MB
- Stars: 2,047
- Watchers: 26
- Forks: 96
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
- awesome-designops - Rebass grid - responsive grid built with styled-system. (Design System Tools / Components)
README
# Rebass Grid
Responsive React grid system built with
[styled-system][], with support for
[styled-components][] and
[emotion][] (previously called `grid-styled`)https://rebassjs.org/grid
[![Build Status][badge]][travis]
[![Downloads][downloads-badge]][npm]
[![Version][version-badge]][npm][badge]: https://flat.badgen.net/travis/rebassjs/grid/master
[travis]: https://travis-ci.org/rebassjs/grid[downloads-badge]: https://flat.badgen.net/npm/dw/@rebass/grid
[version-badge]: https://flat.badgen.net/npm/v/@rebass/grid
[npm]: https://npmjs.com/package/@rebass/grid## Getting Started
```sh
npm i @rebass/grid
``````jsx
import React from 'react'
import { Flex, Box } from '@rebass/grid'const App = () => (
Half width
Half width
)
```### Emotion
*Or* for emotion , import `@rebass/grid/emotion` (uses v10 `@emotion/styled`)
```js
import { Flex, Box } from '@rebass/grid/emotion'
```## Box
The Box component handles width, margin and padding.
```jsx
// Different widths at different breakpoints// Fixed pixel width
// CSS value width
```
```jsx
// Padding// Padding top
// Padding bottom
// Padding left
// Padding right
// x-axis padding (left and right)
// y-axis padding (top and bottom)
```
```jsx
// Margin// Margin top
// Margin bottom
// Margin left
// Margin right
// x-axis margin (left and right)
// y-axis margin (top and bottom)
```
```jsx
// margin auto// negative margins
```
## Props
All @rebass/grid components use [styled-system][] for style props,
which pick up values from a [theme](#theming) and allow for responsive styles to be passed as [array values](#responsive-styles).### `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](#responsive-styles).### Margin and Padding Props
Both margin and padding props accept numbers, strings, and arrays as values.
Using a number from `0-8` (i.e. an index of `theme.space`) 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](#responsive-styles).Margin and padding props follow a shorthand syntax for specifying direction.
- `m`: margin
- `mt`: margin-top
- `mr`: margin-right
- `mb`: margin-bottom
- `ml`: margin-left
- `mx`: margin-left and margin-right
- `my`: margin-top and margin-bottom
- `p`: padding
- `pt`: padding-top
- `pr`: padding-right
- `pb`: padding-bottom
- `pl`: padding-left
- `px`: padding-left and padding-right
- `py`: padding-top and padding-bottom### `flex` (string|array)
Sets the `flex` property.
```jsx
```
### `order` (number|string|array)
Sets the `order` property.
```jsx
```
### `alignSelf` (string|array)
Sets the `align-self` property.
```jsx
```
### `css` (string|object)
Pass styles to styled-components or emotion.
This is useful as an escape hatch for one-off styles
or as a way to extend Rebass Grid components.```jsx
```
## `Flex`
The Flex component extends the Box component and sets display flex.
```jsx
import React from 'react'
import { Flex, Box } from '@rebass/grid'const App = props =>
Flex
Box
```In addition to the Box component props,
Flex also includes the following:- `alignItems` (string|array) sets `align-items`
- `justifyContent` (string|array) sets `justify-content`
- `flexDirection` (string|array) sets `flex-direction`
- `flexWrap` (string|array) sets `flex-wrap: wrap`## Responsive Styles
Rebass Grid 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.```jsx
// 100% below the smallest breakpoint,
// 50% from the next breakpoint and up,
// and 25% from the next breakpoint and up// responsive margin
// responsive padding
```
## Extending Components
Component can be extended with React or using styled-components or emotion.
### InlineFlex
```jsx
import React from 'react'
import { Flex } from '@rebass/grid'const InlineFlex = props =>
``````jsx
// styled-components example
import styled from 'styled-components'
import { Flex } from '@rebass/grid'const InlineFlex = styled(Flex)`
display: inline-flex;
`
```### Max-Width Container
```jsx
import React from 'react'
import { Box } from '@rebass/grid'const Container = props =>
``````js
// styled-components example
import styled from 'styled-components'
import { Box } from '@rebass/grid'const Container = styled(Box)`
max-width: 1024px;
`
Container.defaultProps = {
mx: 'auto'
}
```### Auto Grid
This example creates components for a grid with set gutters where the columns expand to fill in the space.
```jsx
// Example
import React from 'react'
import { Flex, Box } from '@rebass/grid'const Row = props => (
)const Column = props => (
)
```## Changing the HTML element
Rebass Grid is intended for use with styled-components v4.
To change the underlying HTML element, use the styled-components `as` prop.```jsx
```
*Note:* Previous versions of grid-styled supported an `is` prop, which has been deprecated in favor of the styled-components `as` prop.
## Theming
Rebass Grid uses smart defaults, but to customize the values,
use the `ThemeProvider` component from styled-components or emotion.```jsx
import React from 'react'
import { ThemeProvider } from 'styled-components'
import { Box } from '@rebass/grid'const theme = {
space: [ 0, 6, 12, 18, 24 ],
breakpoints: [ '32em', '48em', '64em' ]
}const App = () => (
Box with custom spacing scale and breakpoints
)
```### Breakpoints
The Flex and Box components use 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 string values that will be converted to media queries.
### Spacing Scale
Rebass Grid 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, 4, 8, 16, 32, 64, 128, 256, 512 ]`,
which helps keep spacing consistent and elements aligned even when nesting components.## Styled Space
Rebass Grid also works with the optional [Rebass Space][] package.
```jsx
import React from 'react'
import { Flex, Box } from '@rebass/grid'
import Space from '@rebass/space'const App = () => (
Hello
Beep
)
```## Related
- [Rebass Space][]
- [styled-system][]
- [@rebass/components][]
- [Rebass](https://rebassjs.org/)
- [styled-components][]
- [emotion][][rebass space]: https://github.com/rebassjs/space
[styled-components]: https://github.com/styled-components/styled-components
[styled-system]: https://github.com/jxnblk/styled-system
[emotion]: https://github.com/emotion-js/emotion
[is-prop]: https://github.com/jxnblk/styled-system/tree/master/system-components#changing-the-underlying-html-element
[@rebass/components]: https://github.com/rebassjs/components[MIT License](LICENSE.md)