_understyle
Functional style utilities for authoring JavaScript style objects
npm i understyle
// Example const style = // { margin: 16, display: 'flex' }
Usage in React, etc.
Understyle is intended for use in functional component-based UI systems, like React. It can be used in any framework and for either inline styles or with any CSS-in-JS library.
// Example component const MyComponent = { const style = ... color: 'tomato' return <div style=style>children</div>}
// Example component instancereturn <div> <MyComponent p=2 mb=4> Hello </MyComponent> </div>
Props
The following keys can be passed in to understyle.
Display
Width
The width
prop accepts number values.
Any number larger than 1 is returned as a number, to be handled with React inline styles or other CSS-in-JS solutions.
Any number between 0 and 1 will become a percentage width.
// { width: '50%' } // { width: '100%' } // { width: 64 }
Margin
Margin props are set based on a spacing scale for numbers 1–4. Any number above 4 will return the raw number value.
Shorthand props are also available.
Padding
Padding also supports longform and shorthand keys.
Typography
Font size also accepts numbers representing steps on the configured type scale, for any number below 7
// fontSize: 48
Flexbox
Color
Color props accept strings as either keys from the configured color object or raw color values.
Border
// border: '1px solid' // border: 0 // border: '3px solid' // borderTop: '1px solid' // borderRight: '1px solid' // borderBottom: '1px solid' // borderLeft: '1px solid'
Border Radius
// borderRadius: 2 // borderRadius: 0 // borderRadius: '2px 2px 0 0' // borderRadius: '0 2px 2px 0' // borderRadius: '0 0 2px 2px' // borderRadius: '2px 0 0 2px'
Responsive Styles
All props accept arrays to set styles for multiple breakpoints, where the first value is no breakpoint, then the following values are from the small breakpoint up, for a mobile-first styling approach.
// {// width: '100%',// '@media screen and (min-width:40em)': {// width: '50%'// },// '@media screen and (min-width:52em)': {// width: '33.333333%'// },// '@media screen and (min-width:64em)': {// width: '25%'// }// }
This helps create a succinct syntax for use in React components.
// Example<Box width= 1 1/2 1/3 1/4 />
Arrays work for any understyle property, and null values can be passed to the array to skip breakpoints.
Shorthand Props
Understyle provides shorthand props for setting common styles. Each shorthand prop accepts a boolean value.
These are provided as a convenience for use in React.
// Example<Button />
The following boolean keys can be passed to understyle.
block
- display blockinlineBlock
- display inline-blockinline
table
tableRow
tableCell
flex
inlineFlex
m0
-m4
- marginmt0
-mt4
- margin-topmr0
-mr4
- margin-rightmb0
-mb4
- margin-bottomml0
-ml4
- margin-leftp0
-p4
- paddingpt0
-pt4
- padding-toppr0
-pr4
- padding-rightpb0
-pb4
- padding-bottompl0
-pl4
- padding-leftleft
- text align leftcenter
- text align centerright
- text align rightjustify
- text align justifybg
- background color<COLOR>
- foreground color - e.g.red
bg<COLOR>
- background color - e.g.bgRed
border<COLOR>
- border color - e.g.borderRed