-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
72 lines (61 loc) · 2.93 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
70
71
72
var _ = require('lodash')
var flatten = require('flat')
const FLATTEN_CONFIG = { delimiter: '-', maxDepth: 2 }
const getName = name => name.split('-default').join('')
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 = {
columnCount: [1, 2, 3],
columnRuleStyle: ['none', 'hidden', 'dotted', 'dashed', 'solid', 'double', 'groove', 'ridge', 'inset', 'outset'],
columnFill: ['auto', 'balance', 'balance-all'],
columnSpan: ['none', 'all'],
}
const pluginUtilities = {
'col-count': buildConfig('columnCount'),
'col-gap': buildConfig('columnGap', 'gap', 'gridGap'),
'col-w': buildConfig('columnWidth'),
'col-rule-color': buildConfig('columnRuleColor', 'borderColor'),
'col-rule-width': buildConfig('columnRuleWidth', 'borderWidth'),
'col-rule-style': buildConfig('columnRuleStyle'),
'col-fill': buildConfig('columnFill'),
'col-span': buildConfig('columnSpan'),
}
Object.entries(pluginUtilities)
.filter(([ modifier, values ]) => !_.isEmpty(values))
.forEach(([ modifier, values ]) => {
const className = _.kebabCase(modifier).split('-').slice(0, 2).join('-')
const variantName = Object.keys(Object.entries(values)[0][1])[0]
const escapedValues = _.fromPairs(Object.entries(values).map(([modifier, value]) => [e(modifier), value]))
const utilities = flatten({ [`.${e(`${className}`)}`]: escapedValues }, FLATTEN_CONFIG)
addUtilities(
_.mapKeys(utilities, (value, key) => getName(key)),
variants(variantName, ['responsive'])
)
})
}
}