Reflexbox

Responsive React flexbox grid system higher order component

Build Status Code Climate npm version

Features

  • Uses inline-styles - no CSS dependencies or leaky global styles
  • Simple API for quickly controlling layout
  • Add layout capabilities to any component
  • Helps promote composability and separation of concerns

Getting Started

  1. npm install reflexbox
  1. // Higher order component example
  2. import React from 'react'
  3. import { withReflex } from 'reflexbox'
  4. const Button = (props) => {
  5. return <button {...props} />
  6. }
  7. export default withReflex()(Button)
  1. const App = () => {
  2. return (
  3. <div>
  4. <Button
  5. flex
  6. p={2}
  7. col={12}
  8. align='center'
  9. justify='space-between'>
  10. <span>Flex</span>
  11. <span>Button</span>
  12. </Button>
  13. </div>
  14. )
  15. }

Usage with the Flex and Box components:

  1. // Basic component example
  2. import React from 'react'
  3. import { Flex, Box } from 'reflexbox'
  4. class Component extends React.Component {
  5. render() {
  6. return (
  7. <Flex p={2} align='center'>
  8. <Box px={2}>Box A</Box>
  9. <Box px={2} flexAuto>Box B</Box>
  10. </Flex>
  11. )
  12. }
  13. }

API

Reflexbox is composed of a higher order component and three React components.

withReflex

Higher order component that accepts several layout style helper props that can handle virtually any layout composition.

Props

  • col (number 0–12) Sets width based on a 12 column grid.
  • sm (number 0-12) Sets width from the sm breakpoint and up.
  • md (number 0-12) Sets width from the md breakpoint and up.
  • lg (number 0-12) Sets width from the lg breakpoint and up.
  • align (string) Sets align-items
  • justify (string) Sets justify-content
  • wrap (boolean) Sets flex-wrap: wrap
  • flexColumn (boolean) Sets flex-direction: column
  • flexAuto (boolean) Sets flex: 1 1 auto
  • flex (boolean) Sets display: flex
  • order (boolean) Sets order

Components wrapped with the withReflex higher order component accept several layout props from the Robox higher order component, including the following:

  • gutter (number) Sets negative left and right margin to compensate for child element padding.
  • m (number) Sets margin based on a scale from 0–6.
  • mx (number) Sets x-axis margin based on a scale from 0–6.
  • my (number) Sets y-axis margin based on a scale from 0–6.
  • mt (number) Sets margin-top based on a scale from 0–6.
  • mb (number) Sets margin-bottom based on a scale from 0–6.
  • ml (number) Sets margin-left based on a scale from 0–6.
  • mr (number) Sets margin-right based on a scale from 0–6.
  • p (number) Sets padding based on a scale from 0–6.
  • px (number) Sets x-axis padding based on a scale from 0–6.
  • py (number) Sets y-axis padding based on a scale from 0–6.
  • pt (number) Sets padding-top based on a scale from 0–6.
  • pb (number) Sets padding-bottom based on a scale from 0–6.
  • pl (number) Sets padding-left based on a scale from 0–6.
  • pr (number) Sets padding-right based on a scale from 0–6.

Flex and Box components

The Flex and Box components are created with the withReflex component and use the same set of props. They are intended to help with the readability of code and to provide some backwards compatiblity with previous versions of Reflexbox. The only difference between the two is that the Flex component has flex prop set to true to set display: flex.

Grid component

The Grid component is also based on the withReflex component, but sets display inline-block for use as a more widely supported page layout component. It also includes an align prop to set vertical alignment.

  1. <div>
  2. <Grid col={6} px={2}>
  3. Left column
  4. </Grid>
  5. <Grid col={6} px={2}>
  6. Right column
  7. </Grid>
  8. </div>

Update on window resize

By default, Reflexbox listens to window.matchMedia for the configured breakpoints. To disable this, pass an options object to the withReflex higher-order component.

  1. const Box = withReflex({
  2. listen: false
  3. })(MyComponent)

Configuration

Values for the breakpoints can be configured with React Context.

To configure reflexbox, add childContextTypes and getChildContext to a container component.

  1. class App extends React.Component {
  2. static childContextTypes = {
  3. reflexbox: React.PropTypes.object
  4. }
  5. getChildContext () {
  6. return {
  7. reflexbox: {
  8. breakpoints: {
  9. sm: '(min-width: 30em)',
  10. md: '(min-width: 48em)',
  11. lg: '(min-width: 60em)'
  12. }
  13. }
  14. }
  15. }
  16. render () {
  17. return (
  18. <Flex gutter={2}>
  19. <Box sm={6} md={3}>Box</Box>
  20. <Box sm={6} md={3}>Box</Box>
  21. <Box sm={6} md={3}>Box</Box>
  22. <Box sm={6} md={3}>Box</Box>
  23. </Flex>
  24. )
  25. }
  26. }

Debug Mode

To show an 8 ⨉ 8px background grid for layout debugging, add the following to the context object:

  1. getChildContext () {
  2. return {
  3. reflexbox: {
  4. debug: true
  5. }
  6. }
  7. }

Related

MIT License