Some example functions to map JS Object representations of CSS to CSS strings
inlineStylesToString
stringifies flat inline style object. Eg:
inlineStylesToString({
backgroundColor: '#fff',
width: '100%',
opacity: 1
})
// => 'background-color: #fff; width: 100%; opacity: 1; '
stylesheetObjectToString
expects selectors
stylesheetObjectToString({
button: {
backgroundColor: '#fff',
width: '100%',
opacity: 1,
'&:hover': {
backgroundColor: '#ccc',
}
}
})
// => 'button{background-color:#fff;width:100%;opacity:1;} button:hover{background-color:#ccc;}'
- For the most part these methods try not to be too smart apart from some simple quality of life considerations below
- I added a hook to hyphenate hook to hyphenate JS properties (to allow backgroundColor instead of 'background-color'). Assumes you do not have capital letters in your properties.
stylesheetObjectToString
tries to be a little smart to allow nested selectors and pseudo selector extensions- White space is added sparingly. More so to
inlineStylesToString
to keep the dom less cluttered.