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
- Supports media queries
- Dedupes repeated styles
- Automatically extracts common CSS declarations like
display: block
and float: left
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 tree = view(state)
document.body.appendChild(tree)
cxs.attach()
const body = view(state).toString()
const css = cxs.getCss()
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.getRules()
const css = cxs.getCss()
cxs.clearCache()
Browser support
- IE9 +
- Currently uses CSSStyleSheet.insertRule()
MIT License