Skip to content

Commit 5058275

Browse files
RobinMalfaitBill Criswell
and
Bill Criswell
authored
Handle color transformations properly with theme(...) for all relevant plugins (#5871)
* call function for colors that are not in colors * add all color related thingies * transformThemeValue in a very verbose way * handle functions by default * cleanup, make sure we handle functions everywhere * update changelog Co-authored-by: Bill Criswell <[email protected]>
1 parent 1218b3e commit 5058275

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3232
- Cleanup duplicate properties ([#5830](https://github.com/tailwindlabs/tailwindcss/pull/5830))
3333
- Allow `_` inside `url()` when using arbitrary values ([#5853](https://github.com/tailwindlabs/tailwindcss/pull/5853))
3434
- Prevent crashes when using comments in `@layer` AtRules ([#5854](https://github.com/tailwindlabs/tailwindcss/pull/5854))
35+
- Handle color transformations properly with `theme(...)` for all relevant plugins ([#4533](https://github.com/tailwindlabs/tailwindcss/pull/4533), [#5871](https://github.com/tailwindlabs/tailwindcss/pull/5871))
3536

3637
## [3.0.0-alpha.1] - 2021-10-01
3738

src/util/transformThemeValue.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import postcss from 'postcss'
22

33
export default function transformThemeValue(themeSection) {
44
if (['fontSize', 'outline'].includes(themeSection)) {
5-
return (value) => (Array.isArray(value) ? value[0] : value)
5+
return (value) => {
6+
if (typeof value === 'function') value = value({})
7+
if (Array.isArray(value)) value = value[0]
8+
9+
return value
10+
}
611
}
712

813
if (
@@ -20,18 +25,28 @@ export default function transformThemeValue(themeSection) {
2025
'animation',
2126
].includes(themeSection)
2227
) {
23-
return (value) => (Array.isArray(value) ? value.join(', ') : value)
28+
return (value) => {
29+
if (typeof value === 'function') value = value({})
30+
if (Array.isArray(value)) value = value.join(', ')
31+
32+
return value
33+
}
2434
}
2535

2636
// For backwards compatibility reasons, before we switched to underscores
2737
// instead of commas for arbitrary values.
2838
if (['gridTemplateColumns', 'gridTemplateRows', 'objectPosition'].includes(themeSection)) {
29-
return (value) => (typeof value === 'string' ? postcss.list.comma(value).join(' ') : value)
30-
}
39+
return (value) => {
40+
if (typeof value === 'function') value = value({})
41+
if (typeof value === 'string') value = postcss.list.comma(value).join(' ')
3142

32-
if (themeSection === 'colors') {
33-
return (value) => (typeof value === 'function' ? value({}) : value)
43+
return value
44+
}
3445
}
3546

36-
return (value) => value
47+
return (value) => {
48+
if (typeof value === 'function') value = value({})
49+
50+
return value
51+
}
3752
}

tests/evaluateTailwindFunctions.test.js

+54
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,60 @@ test('it looks up values in the theme using dot notation', () => {
2626
})
2727
})
2828

29+
test('color can be a function', () => {
30+
const input = `
31+
.backgroundColor { color: theme('backgroundColor.fn') }
32+
.borderColor { color: theme('borderColor.fn') }
33+
.caretColor { color: theme('caretColor.fn') }
34+
.colors { color: theme('colors.fn') }
35+
.divideColor { color: theme('divideColor.fn') }
36+
.fill { color: theme('fill.fn') }
37+
.gradientColorStops { color: theme('gradientColorStops.fn') }
38+
.placeholderColor { color: theme('placeholderColor.fn') }
39+
.ringColor { color: theme('ringColor.fn') }
40+
.ringOffsetColor { color: theme('ringOffsetColor.fn') }
41+
.stroke { color: theme('stroke.fn') }
42+
.textColor { color: theme('textColor.fn') }
43+
`
44+
45+
const output = `
46+
.backgroundColor { color: #f00 }
47+
.borderColor { color: #f00 }
48+
.caretColor { color: #f00 }
49+
.colors { color: #f00 }
50+
.divideColor { color: #f00 }
51+
.fill { color: #f00 }
52+
.gradientColorStops { color: #f00 }
53+
.placeholderColor { color: #f00 }
54+
.ringColor { color: #f00 }
55+
.ringOffsetColor { color: #f00 }
56+
.stroke { color: #f00 }
57+
.textColor { color: #f00 }
58+
`
59+
60+
const fn = () => `#f00`
61+
62+
return run(input, {
63+
theme: {
64+
backgroundColor: { fn },
65+
borderColor: { fn },
66+
caretColor: { fn },
67+
colors: { fn },
68+
divideColor: { fn },
69+
fill: { fn },
70+
gradientColorStops: { fn },
71+
placeholderColor: { fn },
72+
ringColor: { fn },
73+
ringOffsetColor: { fn },
74+
stroke: { fn },
75+
textColor: { fn },
76+
},
77+
}).then((result) => {
78+
expect(result.css).toEqual(output)
79+
expect(result.warnings().length).toBe(0)
80+
})
81+
})
82+
2983
test('quotes are optional around the lookup path', () => {
3084
const input = `
3185
.banana { color: theme(colors.yellow); }

0 commit comments

Comments
 (0)