Add basic background color generator

Supports specifying background colors as an array of colors, or an
object where the label is what you want to name that color as a
background utility, and the value is the color to use.

Everything can be specified as either camelCase or kebab-case so you
don't need to quote the shit out of everything but classes will always
be rendered as kebab-case.
This commit is contained in:
Adam Wathan 2017-08-25 09:40:46 -04:00
parent cacff2ac21
commit 8e7b2dc1fa
4 changed files with 175 additions and 24 deletions

View file

@ -0,0 +1,25 @@
const postcss = require('postcss')
const _ = require('lodash')
function findColor(colors, color) {
return _.mapKeys(colors, (value, key) => {
return _.camelCase(key)
})[_.camelCase(color)]
}
module.exports = function ({ colors, backgroundColors }) {
if (_.isArray(backgroundColors)) {
backgroundColors = _(backgroundColors).map(color => [color, color]).fromPairs()
}
return _(backgroundColors).toPairs().map(([className, colorName]) => {
const kebabClass = _.kebabCase(className)
return postcss.rule({
selector: `.bg-${kebabClass}`
}).append({
prop: 'background-color',
value: findColor(colors, colorName)
})
}).value()
}