2019-12-22 15:25:34 -05:00
|
|
|
import fromPairs from 'lodash/fromPairs'
|
|
|
|
|
import toPairs from 'lodash/toPairs'
|
2019-12-24 06:52:55 -05:00
|
|
|
import castArray from 'lodash/castArray'
|
2020-10-19 11:32:22 -04:00
|
|
|
import transformThemeValue from './transformThemeValue'
|
2021-04-30 16:58:40 +02:00
|
|
|
import nameClass from '../util/nameClass'
|
|
|
|
|
import { asValue } from '../util/pluginUtils'
|
2019-12-22 15:25:34 -05:00
|
|
|
|
2020-11-11 08:27:09 -05:00
|
|
|
export default function createUtilityPlugin(
|
|
|
|
|
themeKey,
|
2021-04-30 16:58:40 +02:00
|
|
|
utilityVariations = [[themeKey, [themeKey]]],
|
|
|
|
|
{ filterDefault = false, resolveArbitraryValue = asValue } = {}
|
2020-11-11 08:27:09 -05:00
|
|
|
) {
|
2020-10-19 11:32:22 -04:00
|
|
|
const transformValue = transformThemeValue(themeKey)
|
2021-04-30 16:58:40 +02:00
|
|
|
return function ({ config, matchUtilities, addUtilities, variants, theme }) {
|
|
|
|
|
if (config('mode') === 'jit') {
|
|
|
|
|
for (let utilityVariation of utilityVariations) {
|
|
|
|
|
let group = Array.isArray(utilityVariation[0]) ? utilityVariation : [utilityVariation]
|
|
|
|
|
|
|
|
|
|
matchUtilities(
|
|
|
|
|
group.reduce((obj, [classPrefix, properties]) => {
|
|
|
|
|
return Object.assign(obj, {
|
|
|
|
|
[classPrefix]: (modifier, { theme }) => {
|
|
|
|
|
let value = resolveArbitraryValue(modifier, theme[themeKey])
|
|
|
|
|
|
|
|
|
|
if (value === undefined) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
[nameClass(classPrefix, modifier)]: properties.reduce(
|
|
|
|
|
(obj, name) => Object.assign(obj, { [name]: transformValue(value) }),
|
|
|
|
|
{}
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}, {})
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const pairs = toPairs(theme(themeKey))
|
|
|
|
|
const utilities = utilityVariations.flatMap((utilityVariation) => {
|
|
|
|
|
let group = Array.isArray(utilityVariation[0]) ? utilityVariation : [utilityVariation]
|
|
|
|
|
return group.map(([classPrefix, properties]) => {
|
|
|
|
|
return fromPairs(
|
|
|
|
|
pairs
|
|
|
|
|
.filter(([key]) => {
|
|
|
|
|
return filterDefault ? key !== 'DEFAULT' : true
|
|
|
|
|
})
|
|
|
|
|
.map(([key, value]) => {
|
|
|
|
|
return [
|
|
|
|
|
nameClass(classPrefix, key),
|
|
|
|
|
fromPairs(
|
|
|
|
|
castArray(properties).map((property) => [property, transformValue(value)])
|
|
|
|
|
),
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return addUtilities(utilities, variants(themeKey))
|
|
|
|
|
}
|
2019-12-22 15:25:34 -05:00
|
|
|
}
|
|
|
|
|
}
|