Skip to content

Commit de48a76

Browse files
authored
Ensure opacity modifier with variables work with color-mix() (tailwindlabs#13972)
* ensure opacity modifier variables work as expected We use `color-mix()` in v4 which means that we can use this for the opacity modifier. One thing we do already is convert values such as `0.5` to `50%` because that's what the `color-mix()` function expects. However, if you use a variable like this: ```html <div class="[--opacity:0.5] bg-red-500/[var(--opacity)]"></div> ``` This currently generates: ```css .bg-red-500\/\[var\(--opacity\)\] { background-color: color-mix( in srgb, var(--color-red-500, #ef4444) var(--opacity), transparent ); } .\[--opacity\:0\.5\] { --opacity: 0.5; } ``` Which won't work because the opacity variable resolves to `0.5` instead of the expected`50%`. This commit fixes that by always ensuring that we use `* 100%`. - If you already had a percentage, we would have `calc(50% * 100%)` which is `50%`. - If we have `0.5` then we would have `calc(0.5 * 100%)` which is also `50%`. * wrap everything but percentages in `calc(… * 100%)` * use `else if` * update changelog
1 parent 992cf8d commit de48a76

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Discard invalid classes such as `bg-red-[#000]` ([#13970](https://github.com/tailwindlabs/tailwindcss/pull/13970))
1313
- Fix parsing body-less at-rule without terminating semicolon ([#13978](https://github.com/tailwindlabs/tailwindcss/pull/13978))
14+
- Ensure opacity modifier with variables work with `color-mix()` ([#13972](https://github.com/tailwindlabs/tailwindcss/pull/13972))
1415

1516
## [4.0.0-alpha.17] - 2024-07-04
1617

packages/tailwindcss/src/utilities.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7673,6 +7673,7 @@ test('bg', () => {
76737673
'bg-current/50',
76747674
'bg-current/[0.5]',
76757675
'bg-current/[50%]',
7676+
'bg-current/[--bg-opacity]',
76767677
'bg-inherit',
76777678
'bg-transparent',
76787679
'bg-[#0088cc]',
@@ -7792,7 +7793,15 @@ test('bg', () => {
77927793
background-color: currentColor;
77937794
}
77947795
7795-
.bg-current\\/50, .bg-current\\/\\[0\\.5\\], .bg-current\\/\\[50\\%\\] {
7796+
.bg-current\\/50 {
7797+
background-color: color-mix(in srgb, currentColor 50%, transparent);
7798+
}
7799+
7800+
.bg-current\\/\\[--bg-opacity\\] {
7801+
background-color: color-mix(in srgb, currentColor calc(var(--bg-opacity) * 100%), transparent);
7802+
}
7803+
7804+
.bg-current\\/\\[0\\.5\\], .bg-current\\/\\[50\\%\\] {
77967805
background-color: color-mix(in srgb, currentColor 50%, transparent);
77977806
}
77987807
@@ -8091,11 +8100,11 @@ test('bg', () => {
80918100
}
80928101
80938102
.bg-current\\/custom {
8094-
background-color: color-mix(in srgb, currentColor var(--opacity-custom, var(--custom-opacity)), transparent);
8103+
background-color: color-mix(in srgb, currentColor calc(var(--opacity-custom, var(--custom-opacity)) * 100%), transparent);
80958104
}
80968105
80978106
.bg-current\\/half {
8098-
background-color: color-mix(in srgb, currentColor var(--opacity-half, .5), transparent);
8107+
background-color: color-mix(in srgb, currentColor calc(var(--opacity-half, .5) * 100%), transparent);
80998108
}"
81008109
`)
81018110
})

packages/tailwindcss/src/utilities.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ function withAlpha(value: string, alpha: string): string {
137137
alpha = `${alphaAsNumber * 100}%`
138138
}
139139

140+
// If the alpha value is a percentage, we can pass it directly to
141+
// `color-mix()`. In any other case, e.g.: `var(…)`, `round(…)`, … we need to
142+
// make sure it's a percentage.
143+
else if (alpha[alpha.length - 1] !== '%') {
144+
alpha = `calc(${alpha} * 100%)`
145+
}
146+
140147
return `color-mix(in srgb, ${value} ${alpha}, transparent)`
141148
}
142149

0 commit comments

Comments
 (0)