⚡︎ cxs

WIP
Functional CSS for functional UI components
cxs is a css-in-js solution to dynamically create stylesheets with a functional approach
Features
- Avoids collisions with hashed classnames
- Supports pseudo-classes without JS
- Supports media queries without using
window.matchMedia
- Dedupes repeated styles
- Automatically extracts common CSS declarations like
display: block
and float: left
- Avoid maintaining and using custom syntax or classname DSLs from CSS frameworks and manually written CSS
- Scoped styles with a component-based architecture
- No separate CSS files to process or maintain
npm i cxs
Example Usage
import yo from 'yo-yo'
import cxs from 'cxs'
const Button = ({ text, onclick }) => {
const className = cxs({
fontSize: 14,
color: 'white',
backgroundColor: '#07c',
':hover': {
backgroundColor: '#06b'
},
'@media screen and (min-width:40em)': {
fontSize: 18
}
})
return yo`
<button
className=${className}
onclick=${onclick}>
${text}
</button>
`
}
const body = view(state).toString()
const css = cxs.css
const html `<!DOCTYPE html>
<html>
<head>
<style>${css}</style>
</head>
<body>${body}</body>
</html>
`
API
const className = cxs({ color: 'tomato' })
cxs.attach()
const rules = cxs.rules
const css = cxs.css
cxs.clearCache()
cxs.options.autoAttach = true
cxs.options.debounce = 0
Vendor prefixes
cxs does not handle vendor prefixing to keep the module size at a minimum.
To add vendor prefixes, use a prefixing module like inline-style-prefixer
import cxs from 'cxs'
import prefixer from 'inline-style-prefixer/static'
const prefixed = prefixer({
display: 'flex'
})
const cx = cxs(prefixed)
Other CSS-in-JS options
Compared to other, similar modules, cxs is an attempt to create a smaller and simpler API and a smaller overall module.
For more customizable and robust solutions, see the following:
Browser support
- IE9 +
- Due to the following:
CSSStyleSheet.insertRule()
Array.filter
Array.map
Array.reduce
Array.forEach
MIT License