understyle

Functional style utilities for authoring JavaScript style objects

Build Status js-standard-style

  1. npm i understyle
  1. // Example
  2. import _style from 'understyle'
  3. const style = _style({
  4. m: 2,
  5. flex: true
  6. })
  7. // { margin: 16, display: 'flex' }
  1. // Individual modules
  2. import { margin, padding } from 'understyle'
  3. const style = {
  4. ...margin({ m: 2 }),
  5. ...padding({ p: 2 })
  6. }
  7. // { margin: 16, padding: 16 }

Usage in React

Understyle is intended for use in functional component-based UI systems, like React.

  1. // Example component
  2. import React from 'react'
  3. import _style from 'understyle'
  4. const MyComponent = ({ children, ...props }) => {
  5. const style = {
  6. ..._style(props),
  7. color: 'tomato'
  8. }
  9. return <div style={style}>{children}</div>
  10. }
  11. export default MyComponent
  1. // Example component instance
  2. return (
  3. <div>
  4. <MyComponent p={2} mb={4}>
  5. Hello
  6. </MyComponent>
  7. </div>
  8. )

Functions

Each function accepts its own unique set of shorthand options to create style objects

margin

  1. import { margin } from 'understyle'
  2. const style = margin({ m: 1 })
  3. // { margin: 8 }

Each option should be a number from 0–6, which will return a value based on a spacing scale array [0, 8, 16, 32, 48, 64, 96]. Negative numbers return negative values, and 'auto' will return margin: auto.

Props:

  • m: margin
  • mt: marginTop
  • mr: marginRight
  • mb: marginBottom
  • ml: marginLeft
  • mx: marginLeft & marginRight
  • my: marginTop & marginBottom
  • gutter: negative marginLeft & marginRight

padding

Padding uses the same spacing scale array

  1. import { padding } from 'understyle'
  2. const style = padding({ p: 1 })
  3. // { padding: 8 }

Props:

  • p: padding
  • pt: paddingTop
  • pr: paddingRight
  • pb: paddingBottom
  • pl: paddingLeft
  • px: paddingLeft & paddingRight
  • py: paddingTop & paddingBottom

column

  1. import { column } from 'understyle'
  2. const style = column({ col: 6 })
  3. // { width: '50%' }

The col option should be a number from 1–12 to return a percentage-based width based on a 12 column grid.

display

  1. import { display } from 'understyle'
  2. const style = display({ inlineBlock: true })
  3. // { display: 'inline-block' }

Each display option should be a boolean.

Props:

  • block
  • inlineBlock
  • inline
  • table
  • tableRow
  • tableCell
  • flex
  • inlineFlex

flex

  1. import { flex } from 'understyle'
  2. const style = flex({
  3. align: 'center',
  4. justify: 'center'
  5. })
  6. // { alignItems: 'center', justifyContent: 'center' }

The flex options are a mix of booleans and strings to set various flexbox style properties.

Props:

  • wrap boolean - sets flexWrap: 'wrap'
  • align string - sets alignItems
  • justify string - sets justifyContent
  • flexColumn boolean - sets flexDirection: 'column'
  • flexAuto boolean - sets flex: '1 1 auto'
  • flexNone boolean - sets flex: 'none'

MIT License