Support nested object for textColor plugin

This commit is contained in:
Adam Wathan 2019-03-04 13:07:26 -05:00
parent c3c9cdf5d6
commit bfde7e4d6e
3 changed files with 68 additions and 15 deletions

View File

@ -0,0 +1,64 @@
import _ from 'lodash'
import escapeClassName from '../../src/util/escapeClassName'
import plugin from '../../src/plugins/textColor'
test('colors can be a nested object', () => {
const addedUtilities = []
const config = {
theme: {
textColor: {
purple: 'purple',
red: {
1: 'rgb(33,0,0)',
2: 'rgb(67,0,0)',
3: 'rgb(100,0,0)',
},
green: {
1: 'rgb(0,33,0)',
2: 'rgb(0,67,0)',
3: 'rgb(0,100,0)',
},
blue: {
1: 'rgb(0,0,33)',
2: 'rgb(0,0,67)',
3: 'rgb(0,0,100)',
},
},
},
variants: {
textColor: ['responsive'],
},
}
const pluginApi = {
config: (key, defaultValue) => _.get(config, key, defaultValue),
e: escapeClassName,
addUtilities(utilities, variants) {
addedUtilities.push({
utilities,
variants,
})
},
}
plugin()(pluginApi)
expect(addedUtilities).toEqual([
{
utilities: {
'.text-purple': { 'color': 'purple' },
'.text-red-1': { 'color': 'rgb(33,0,0)' },
'.text-red-2': { 'color': 'rgb(67,0,0)' },
'.text-red-3': { 'color': 'rgb(100,0,0)' },
'.text-green-1': { 'color': 'rgb(0,33,0)' },
'.text-green-2': { 'color': 'rgb(0,67,0)' },
'.text-green-3': { 'color': 'rgb(0,100,0)' },
'.text-blue-1': { 'color': 'rgb(0,0,33)' },
'.text-blue-2': { 'color': 'rgb(0,0,67)' },
'.text-blue-3': { 'color': 'rgb(0,0,100)' },
},
variants: ['responsive'],
},
])
})

View File

@ -1,22 +1,10 @@
import _ from 'lodash'
function buildColorPalette(colors) {
const result = _(colors)
.flatMap((color, name) => {
return _.isObject(color)
? _.map(color, (value, key) => [`${name}-${key}`, value])
: [[name, color]]
})
.fromPairs()
.value()
return result
}
import flattenColorPalette from '../util/flattenColorPalette'
export default function() {
return function({ addUtilities, e, config }) {
const utilities = _.fromPairs(
_.map(buildColorPalette(config('theme.backgroundColor')), (value, modifier) => {
_.map(flattenColorPalette(config('theme.backgroundColor')), (value, modifier) => {
return [
`.${e(`bg-${modifier}`)}`,
{

View File

@ -1,9 +1,10 @@
import _ from 'lodash'
import flattenColorPalette from '../util/flattenColorPalette'
export default function() {
return function({ addUtilities, e, config }) {
const utilities = _.fromPairs(
_.map(config('theme.textColor'), (value, modifier) => {
_.map(flattenColorPalette(config('theme.textColor')), (value, modifier) => {
return [
`.${e(`text-${modifier}`)}`,
{