|
1 | 1 | const _ = require('lodash')
|
| 2 | +const flatten = require('flat') |
2 | 3 |
|
3 |
| -module.exports = function ({ indents, variants }) { |
4 |
| - return function ({ addUtilities, e }) { |
5 |
| - const utilities = _.map(indents, (size, name) => ({ |
6 |
| - [`.${size.charAt(0) === '-' ? '-' : ''}indent-${name}`]: { |
7 |
| - textIndent: size, |
8 |
| - }, |
9 |
| - })) |
10 | 4 |
|
11 |
| - addUtilities(utilities, variants) |
| 5 | +const FLATTEN_CONFIG = { delimiter: '-', maxDepth: 2 } |
| 6 | +const handleName = (name, className) => { |
| 7 | + const split = name.split(`${className}-`) |
| 8 | + const prefixedName = `${split[0]}${prefixNegativeModifiers(className, split[1])}` |
| 9 | + |
| 10 | + return prefixedName.split('-default').join('') |
| 11 | +} |
| 12 | +const prefixNegativeModifiers = function(base, modifier) { |
| 13 | + return _.startsWith(modifier, '-') |
| 14 | + ? `-${base}-${modifier.slice(1)}` |
| 15 | + : `${base}-${modifier}` |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +module.exports = function () { |
| 20 | + return function ({ |
| 21 | + addUtilities, addComponents, addBase, addVariant, |
| 22 | + e, prefix, theme, variants, config, |
| 23 | + }) { |
| 24 | + const buildConfig = (themeKey, ...fallbackKeys) => { |
| 25 | + return buildConfigFromTheme(themeKey, ...fallbackKeys) || buildConfigFromArray(themeKey) |
| 26 | + } |
| 27 | + const buildConfigFromTheme = (themeKey, ...fallbackKeys) => { |
| 28 | + const buildObject = ([ modifier, value ]) => [ modifier, { [themeKey]: value } ] |
| 29 | + const getThemeSettings = (themeKey, fallbackKeys) => { |
| 30 | + const [newThemeKey, ...newFallbackKeys] = fallbackKeys || [] |
| 31 | + |
| 32 | + return theme(themeKey, false) || (fallbackKeys.length && getThemeSettings(newThemeKey, [...newFallbackKeys])) |
| 33 | + } |
| 34 | + |
| 35 | + const themeSettings = getThemeSettings(themeKey, fallbackKeys) |
| 36 | + const themeObject = _.isArray(themeSettings) ? _.zipObject(themeSettings, themeSettings) : themeSettings |
| 37 | + const themeEntries = themeSettings && Object |
| 38 | + .entries(flatten(themeObject, FLATTEN_CONFIG)) |
| 39 | + .map(entry => buildObject(entry)) |
| 40 | + |
| 41 | + return themeSettings ? _.fromPairs(themeEntries) : false |
| 42 | + } |
| 43 | + const buildConfigFromArray = (property) => { |
| 44 | + const defaultSettings = defaultValues[property] |
| 45 | + const defaultEntries = defaultSettings && defaultSettings |
| 46 | + .map((value) => ([value, { [property]: value }])) |
| 47 | + |
| 48 | + return defaultSettings ? _.fromPairs(defaultEntries) : false |
| 49 | + } |
| 50 | + |
| 51 | + const defaultValues = {} |
| 52 | + const pluginUtilities = { |
| 53 | + 'indent': buildConfig('textIndent'), |
| 54 | + } |
| 55 | + |
| 56 | + Object.entries(pluginUtilities) |
| 57 | + .filter(([ modifier, values ]) => !_.isEmpty(values)) |
| 58 | + .forEach(([ modifier, values ]) => { |
| 59 | + const className = _.kebabCase(modifier) |
| 60 | + const variantName = Object.keys(Object.entries(values)[0][1])[0] |
| 61 | + const utilities = flatten({ [`.${e(`${className}`)}`]: values }, FLATTEN_CONFIG) |
| 62 | + |
| 63 | + addUtilities( |
| 64 | + _.mapKeys(utilities, (value, key) => handleName(key, className)), |
| 65 | + variants(variantName, ['responsive']) |
| 66 | + ) |
| 67 | + }) |
12 | 68 | }
|
13 | 69 | }
|
0 commit comments