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