-
Notifications
You must be signed in to change notification settings - Fork 4k
/
gradientColorStops.js
56 lines (51 loc) · 1.65 KB
/
gradientColorStops.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
import _ from 'lodash'
import flattenColorPalette from '../util/flattenColorPalette'
import nameClass from '../util/nameClass'
import toColorValue from '../util/toColorValue'
import { toRgba } from '../util/withAlphaVariable'
export default function() {
return function({ addUtilities, theme, variants }) {
const colors = flattenColorPalette(theme('gradientColorStops'))
const utilities = _(colors)
.map((value, modifier) => {
const transparentTo = (() => {
if (_.isFunction(value)) {
return value({ opacityValue: 0 })
}
try {
const [r, g, b] = toRgba(value)
return `rgba(${r}, ${g}, ${b}, 0)`
} catch (_error) {
return `rgba(255, 255, 255, 0)`
}
})()
return [
[
nameClass('from', modifier),
{
'--gradient-from-color': toColorValue(value, 'from'),
'--gradient-color-stops': `var(--gradient-from-color), var(--gradient-to-color, ${transparentTo})`,
},
],
[
nameClass('via', modifier),
{
'--gradient-via-color': toColorValue(value, 'via'),
'--gradient-color-stops': `var(--gradient-from-color), var(--gradient-via-color), var(--gradient-to-color, ${transparentTo})`,
},
],
[
nameClass('to', modifier),
{
'--gradient-to-color': toColorValue(value, 'to'),
},
],
]
})
.unzip()
.flatten()
.fromPairs()
.value()
addUtilities(utilities, variants('gradientColorStops'))
}
}