forked from bradlc/tailwindcss-alpha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (70 loc) · 2.01 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
73
74
75
76
77
const Color = require('color')
const PREFIXES = {
backgroundColors: ['bg'],
textColors: ['text'],
borderColors: ['border', 'border-t', 'border-r', 'border-b', 'border-l'],
svgFill: ['fill'],
svgStroke: ['stroke']
}
const PROPERTIES = {
backgroundColors: ['backgroundColor'],
textColors: ['color'],
borderColors: [
'borderColor',
'borderTopColor',
'borderRightColor',
'borderBottomColor',
'borderLeftColor'
],
svgFill: ['fill'],
svgStroke: ['stroke']
}
module.exports = function(opts = {}) {
return function({ e, addUtilities, config }) {
let {
alpha = config('alpha', config('opacity', {})),
modules = {
backgroundColors: true,
textColors: false,
borderColors: false,
svgFill: false,
svgStroke: false
}
} = opts
Object.entries(alpha).forEach(([alphaKey, alphaValue]) => {
let alphaValueFloat = parseFloat(alphaValue)
if (alphaValueFloat === 0 || alphaValueFloat === 1) return null
Object.entries(modules).forEach(([configKey, variants]) => {
if (variants === true) {
variants = config(`modules.${configKey}`, [])
}
if (variants === false) return
let colors = config(configKey, {})
addUtilities(
Object.entries(colors)
.map(([colorKey, color]) => {
try {
let parsed = Color(color)
if (parsed.valpha === 1) {
return PREFIXES[configKey].map((prefix, i) => {
return {
[`.${e(`${prefix}-${colorKey}-${alphaKey}`)}`]: {
[`${PROPERTIES[configKey][i]}`]: parsed
.alpha(alphaValueFloat)
.string()
}
}
})
}
} catch (err) {
return null
}
return null
})
.filter(Boolean),
variants
)
})
})
}
}