nano-style
React functional CSS-in-JS
npm i nano-style
- Use object literals or CSS syntax
- Pure React - no side effects
- Functional styles
- Theming support
- Universal rendering with no additional setup
- Removes props defined as propTypes from rendered HTML
Usage
Object Literal Syntax
const Button = fontFamily: 'inherit' fontSize: '14px' fontWeight: propsthemebold lineHeight: 16/14 display: 'inline-block' margin: 0 paddingLeft: propsthemespace3 + 'px' paddingRight: propsthemespace3 + 'px' paddingTop: propsthemespace2 + 'px' paddingBottom: propsthemespace2 + 'px' verticalAlign: 'middle' textAlign: 'center' textDecoration: 'none' borderRadius: propsthemeradius border: 0 appearance: 'none' color: 'white' backgroundColor: propsthemecolorsblue '&:hover': boxShadow: `inset 0 0 0 999px ` '&:focus': outline: 0 boxShadow: `0 0 0 2px ` '&:active': boxShadow: `inset 0 0 8px ` '&:disabled': opacity: 1/4
CSS Syntax
const Button = ` font-family: inherit; font-size: 14px; font-weight: ; line-height: ; display: inline-block; margin: 0; padding-left: ; padding-right: ; padding-top: ; padding-bottom: ; vertical-align: middle; text-align: center; text-decoration: none; border-radius: ; border: 0; appearance: none; color: white; background-color: props.theme.colors.blue; &:hover { box-shadow: inset 0 0 0 999px ; } &:focus { outline: 0; box-shadow: 0 0 0 2px ; } &:active { box-shadow: inset 0 0 8px ; } &:disabled { opacity: 1/4 }`
How it works
Using React 16's ability to return arrays of elements,
nano-style generates CSS during component rendering
and inserts CSS into a <style>
element inlined with the component.
The returned array looks something like this:
return <Style = /> <Component />
Caveats
Currently, this approach does not attempt to deduplicate repeated CSS when a single component is rendered in multiple instances. While this does work, it may present some slight performance issues when a component is used multiple times in a page.
Potential areas for improvement
- Caching mechanism
- Babel plugin