0.1.1 • Published 6 years ago

react-flexbox-view v0.1.1

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

React Flexbox View

Travis NPM Coveralls

A generic Flexbox based layout component for wrapping other components.

The API provides some abstractions over flexbox with readability and useability in mind, with the full power of flexbox still available if you need. A number of shortcut props have been provided that address common use cases when constructing user interfaces

View a demo here

WARNING: API is not 100% finalised yet and is subject to change

Install

npm install --save react-flexbox-view

Basic Usage

React Flexbox Grid exports a default base view component which can be used like so:

import React from 'react'
import View from 'react-flexbox-view'

const Example = () => (
  <View column fit>
    <View>Row 1</View>
    <View>Row 2</View>
    <View>Row 3</View>
  </View>
)

This will create 3 rows one below the other (as parent is column), with each row taking up full width of the container which has been set with 100% width and height via the fit prop.

Responsive Helpers

Responsive props can be set that can control what basis value is used at different breakpoints:

Responsive props accept the same values as the basis prop ie. a valid CSS length OR a fraction wich will be converted into a percentage.

Example

In the following example, Cell A will take up 100% of the width of the container at all screen sizes. Cells B and C will do the same on xs devices but on sm devices they will share the same row, each taking up half the width of the row. Cells D, E and F will each sit on their own row taking up 100% width on xs devices only, and in other scenarios will share the same row taking up a third of the width each.

<View wrap>
  <View xs={1}>Cell A</View
  <View xs={1} sm={1/2}>Cell B</View>
  <View xs={1} sm={1/2}>Cell C</View>
  <View xsOnly={1}>Cell D</View>
  <View xsOnly={1}>Cell E</View>
  <View xsOnly={1}>Cell F</View>
</View>

Spacing Helpers

A number of spacing helpers have been provided that can be used to easily set margins or padding.

Example

The following will set padding and margins of various sizes - which sizes depend on the provided mapping object in the scales prop. A boolean value will use the "regular" size in the scale

<View padding>
  <View margin marginTop="large" marginBottom="large" />
</View>

If you don't like the provided scales, provide your own using the included ViewProvider component

ViewProvider

The included ViewProvider component provides a way to customize the default props for all View components nested under it. This is handy if you would like to customize some of the defaults, particularly the responsive breakpoints or spacing scales.

Example

import View, { ViewProvider } from 'react-flexbox-view'

const Example = () => (
  <ViewProvider
    breakpoints={{
      xs:0,
      sm:576,
      md:768,
      lg:992,
      xl:1200,
    }}
  >
    ...
  </ViewProvider>
)

API

Core

PropertyTypeDefaultDescription
columnboolean, stringSets the flex-direction to column when True or column-reverse when "reverse" TIP You can use responsive helpers here eg. smOnly, smOnly md-reverse lgOnly
rowboolean, stringExplicitly sets the flex-direction to row when True or row-reverse when "reverse" TIP You can use responsive helpers here eg. smOnly, smOnly md-reverse lgOnly
wrapboolean, stringSets the flex-wrap property ie. whether child flex items are forced onto a single line or can be wrapped onto multiple lines.TIP: Specifying 'reverse' is a shortcut for 'wrap-reverse'
growboolean, numbertrueThe flex grow factor (flex-grow) ie. what amount of space inside the flex container the item should take up TIP: true will set it to 1 and false will set to 0
shrinkboolean, numberThe flex shrink factor (flex-shrink) ie. how much the flex item will shrink relative to the other items in the container TIP: true will set it to 1 and false will set to 0
basisnumber, stringSets the flex-basis property which is the intial main size of a flex item. Tip: You can use standard CSS length values (10px, 1rem, 100% etc.) or if a fraction is supplied this will be converted to a percentage eg. 1/4 will be set as 25%

Positioning

PropertyTypeDefaultDescription
justifyContentstringDefines how the browser distributes space between and around child items along the main axis of their container. Valid options include center, end, flex-end, flex-start, start, space-around, space-between TIP: start and end are shortcuts for flex-start and flex-end respectively
alignItemsstringDefines how the browser distributes space between and around child items along the cross axis of their container. Valid options include center, end, flex-end, flex-start, start, space-around, space-between TIP: start and end are shortcuts for flex-start and flex-end respectively
alignContentstringAligns the container's lines along the cross axis in multi-line containers. Has no effect in single line boxes Valid options include center, end, flex-end, flex-start, start, space-around, space-between TIP: start and end are shortcuts for flex-start and flex-end respectively
alignSelfstringAllows the default alignment along the cross axis specified by the container's alignItems to be overwritten for the individual component. Valid options include center, end, flex-end, flex-start, start, space-around, space-between TIP: start and end are shortcuts for flex-start and flex-end respectively
centerbooleanPositions children in the center of the component (both axes). Is a shortcut for setting alignItems="center" and justifyContent="center"

Responsive Props

PropertyTypeDefaultDescription
breakpointsObject{xs:0,sm:576,md:768,lg:992,xl:1200}A definition of the various breakpoints. Required when setting the other responsive props
xsnumber, stringA basis property that will be applied from the xs breakpoint and up
xsOnlynumber, stringA basis property that will be applied for xs devices only
smnumber, stringA basis property that will be applied from the sm breakpoint and up
smOnlynumber, stringA basis property that will be applied for sm devices only
mdnumber, stringA basis property that will be applied from the md breakpoint and up
mdOnlynumber, stringA basis property that will be applied for md devices only
lgnumber, stringA basis property that will be applied from the lg breakpoint and up
lgOnlynumber, stringA basis property that will be applied for lg devices only
xlnumber, stringA basis property that will be applied from the xl breakpoint and up

Spacing Helpers

PropertyTypeDefaultDescription
scalesobject{smallest:'0.1rem',smaller:'0.3rem',small: '0.6rem',regular: '1rem',large: '1.3rem',larger: '1.6rem',largest: '2rem'}An object of key to length value mappings. If one of these keys is provided as a margin or padding prop value then the corresponding value to use will be looked up in this object. Tip: To specify different mappings for margin and padding props, nest these individual mappings under margin and padding properties ie. { padding: { ... }, margin: { ... } }
marginboolean, stringSets all margins. Accepts a standard CSS length, the corresponding length in the provided scales prop, or the regular one if boolean true is provided
marginTopboolean, stringSame as margin but sets the top margin only
marginRightboolean, stringSame as margin but sets the right margin only
marginBottomboolean, stringSame as margin but sets the bottom margin only
marginLeftboolean, stringSame as margin but sets the left margin only
paddingboolean, stringSets total padding. Accepts a standard CSS length, the corresponding length in the provided scales prop, or the regular one if boolean true is provided
paddingTopboolean, stringSame as padding but sets the top padding only
paddingRightboolean, stringSame as padding but sets the right padding only
paddingBottomboolean, stringSame as padding but sets the bottom padding only
paddingLeftboolean, stringSame as padding but sets the left padding only

Other

PropertyTypeDefaultDescription
fitbooleanSets the width and height of the component to 100%. Overrides existing width and height props if they are set
inlinebooleanSets the display to inline-flex instead of the default flex
heightnumber, stringSets the height CSS property of the component
widthnumber, stringSets the width CSS property of the component
minHeightnumber, stringSets the min-height CSS property of the component
minWidthnumber, stringSets the min-width CSS property of the component
positionstring"relative"Sets the position CSS property of the component

License

MIT