Skip to content

Commit 28930f4

Browse files
committed
Merge branch 'fix/color-object-closures' of git://github.com/innocenzi/tailwindcss into innocenzi-fix/color-object-closures
2 parents 239da62 + f6fc963 commit 28930f4

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import postcss from 'postcss'
2+
import tailwind from '../../src/index'
3+
4+
test('opacity variables are given to colors defined as closures', () => {
5+
return postcss([
6+
tailwind({
7+
theme: {
8+
colors: {
9+
primary: ({ opacityVariable }) => `rgba(31,31,31,var(${opacityVariable},1))`,
10+
},
11+
},
12+
variants: {
13+
gradientColorStops: [],
14+
},
15+
corePlugins: ['gradientColorStops'],
16+
}),
17+
])
18+
.process('@tailwind utilities', { from: undefined })
19+
.then(result => {
20+
const expected = `
21+
.from-primary {
22+
--gradient-from-color: rgba(31,31,31,var(--gradient-from-opacity,1));
23+
--gradient-color-stops: var(--gradient-from-color), var(--gradient-to-color, rgba(255, 255, 255, 0))
24+
}
25+
26+
.via-primary {
27+
--gradient-via-color: rgba(31,31,31,var(--gradient-via-opacity,1));
28+
--gradient-color-stops: var(--gradient-from-color), var(--gradient-via-color), var(--gradient-to-color, rgba(255, 255, 255, 0))
29+
}
30+
31+
.to-primary {
32+
--gradient-to-color: rgba(31,31,31,var(--gradient-to-opacity,1))
33+
}
34+
`
35+
36+
expect(result.css).toMatchCss(expected)
37+
})
38+
})

src/plugins/gradientColorStops.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ export default function() {
1212

1313
const utilities = _(colors)
1414
.map((value, modifier) => {
15+
const getColorValue = (color, type) => {
16+
if (_.isFunction(color)) {
17+
return value({
18+
opacityVariable: `--gradient-${type}-opacity`,
19+
})
20+
}
21+
22+
return color
23+
}
24+
1525
const transparentTo = (() => {
1626
try {
1727
const [r, g, b] = toRgba(value)
@@ -25,21 +35,21 @@ export default function() {
2535
[
2636
`.${e(`from-${modifier}`)}`,
2737
{
28-
'--gradient-from-color': value,
38+
'--gradient-from-color': getColorValue(value, 'from'),
2939
'--gradient-color-stops': `var(--gradient-from-color), var(--gradient-to-color, ${transparentTo})`,
3040
},
3141
],
3242
[
3343
`.${e(`via-${modifier}`)}`,
3444
{
35-
'--gradient-via-color': value,
45+
'--gradient-via-color': getColorValue(value, 'via'),
3646
'--gradient-color-stops': `var(--gradient-from-color), var(--gradient-via-color), var(--gradient-to-color, ${transparentTo})`,
3747
},
3848
],
3949
[
4050
`.${e(`to-${modifier}`)}`,
4151
{
42-
'--gradient-to-color': value,
52+
'--gradient-to-color': getColorValue(value, 'to'),
4353
},
4454
],
4555
]

src/util/flattenColorPalette.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import _ from 'lodash'
33
export default function flattenColorPalette(colors) {
44
const result = _(colors)
55
.flatMap((color, name) => {
6-
if (!_.isObject(color)) {
6+
if (_.isFunction(color) || !_.isObject(color)) {
77
return [[name, color]]
88
}
99

0 commit comments

Comments
 (0)