Skip to content

Commit 9778b52

Browse files
Provide default to <alpha-value> when using theme() (#8652)
* Ensure default alpha is 1.0 when using new `<alpha-value>` format with the theme function * Update changelog
1 parent 3cf48bf commit 9778b52

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Fix "Maximum call stack size exceeded" bug ([#8636](https://github.com/tailwindlabs/tailwindcss/pull/8636))
2222
- Allow functions returning parallel variants to mutate the container ([#8622](https://github.com/tailwindlabs/tailwindcss/pull/8622))
2323
- Remove text opacity CSS variables from `::marker` ([#8622](https://github.com/tailwindlabs/tailwindcss/pull/8622))
24+
- Provide default to `<alpha-value>` when using `theme()` ([#8622](https://github.com/tailwindlabs/tailwindcss/pull/8622))
2425

2526
## [3.1.2] - 2022-06-10
2627

src/lib/evaluateTailwindFunctions.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,15 @@ export default function ({ tailwindConfig: config }) {
183183
throw node.error(error)
184184
}
185185

186-
if (alpha !== undefined) {
187-
value = parseColorFormat(value)
188-
value = withAlphaValue(value, alpha, value)
186+
let maybeColor = parseColorFormat(value)
187+
let isColorFunction = maybeColor !== undefined && typeof maybeColor === 'function'
188+
189+
if (alpha !== undefined || isColorFunction) {
190+
if (alpha === undefined) {
191+
alpha = 1.0
192+
}
193+
194+
value = withAlphaValue(maybeColor, alpha, maybeColor)
189195
}
190196

191197
return value

tests/evaluateTailwindFunctions.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,36 @@ test('Theme function can extract alpha values for colors (8)', () => {
10251025
})
10261026
})
10271027

1028+
test('Theme functions replace the alpha value placeholder even with no alpha provided', () => {
1029+
let input = css`
1030+
.foo {
1031+
background: theme(colors.blue.400);
1032+
color: theme(colors.blue.500);
1033+
}
1034+
`
1035+
1036+
let output = css`
1037+
.foo {
1038+
background: rgb(0 0 255 / 1);
1039+
color: rgb(var(--foo) / 1);
1040+
}
1041+
`
1042+
1043+
return runFull(input, {
1044+
theme: {
1045+
colors: {
1046+
blue: {
1047+
400: 'rgb(0 0 255 / <alpha-value>)',
1048+
500: 'rgb(var(--foo) / <alpha-value>)',
1049+
},
1050+
},
1051+
},
1052+
}).then((result) => {
1053+
expect(result.css).toMatchCss(output)
1054+
expect(result.warnings().length).toBe(0)
1055+
})
1056+
})
1057+
10281058
test('Theme functions can reference values with slashes in brackets', () => {
10291059
let input = css`
10301060
.foo1 {

0 commit comments

Comments
 (0)