Skip to content

Commit 7b75b1a

Browse files
Fix opacity modifier using CSS variables (#14916)
When using an opacity modifier such as `bg-black/[var(--opacity)]`, then this was translated to: ```css .bg-black\/\[var\(--opacity\)\] { background-color: color-mix( in oklch, var(--color-black, #000) calc(var(--opacity) * 100%), transp } ``` The issue is that this part: `calc(var(--opacity) * 100%)` is invalid _if_ the `var(--opacity)` already contains a percentage value. See: https://play.tailwindcss.com/xz0t This is because this eventually resolves to `calc(20% * 100%)` and `20% 100%` is invalid in CSS. In Catalyst we use variables like that _with_ the `%` included, which means that v4 doesn't work as expected when using this. A variable with a `%` included is probably the better value to support compared to the the unit less one. This also allows you to define your variables using `@property` as a proper `<percentage>` type. Unfortunately the `var(--opacity)` is a value that can change at runtime, so we don't know the type at compile time. In the future we might be able to use `first-valid(…)` (see: https://drafts.csswg.org/css-values-5/#f and generate both versions at the same time. --------- Co-authored-by: Adam Wathan <[email protected]>
1 parent aaa32e2 commit 7b75b1a

File tree

5 files changed

+5
-11
lines changed

5 files changed

+5
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3434
- Ensure the CSS `theme()` function handles newlines and tabs in its arguments list ([#14917](https://github.com/tailwindlabs/tailwindcss/pull/14917))
3535
- Don't unset keys like `--inset-shadow-*` when unsetting keys like `--inset-*` ([#14906](https://github.com/tailwindlabs/tailwindcss/pull/14906))
3636
- Ensure spacing utilities with no value (e.g. `px` or `translate-y`) don't generate CSS ([#14911](https://github.com/tailwindlabs/tailwindcss/pull/14911))
37+
- Don't attempt to convert CSS variables (which should already be percentages) to percentages when used as opacity modifiers ([#14916](https://github.com/tailwindlabs/tailwindcss/pull/14916))
3738
- _Upgrade (experimental)_: Install `@tailwindcss/postcss` next to `tailwindcss` ([#14830](https://github.com/tailwindlabs/tailwindcss/pull/14830))
3839
- _Upgrade (experimental)_: Remove whitespace around `,` separator when print arbitrary values ([#14838](https://github.com/tailwindlabs/tailwindcss/pull/14838))
3940
- _Upgrade (experimental)_: Fix crash during upgrade when content globs escape root of project ([#14896](https://github.com/tailwindlabs/tailwindcss/pull/14896))

packages/tailwindcss/src/compat/plugin-api.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ describe('theme', async () => {
291291
color: color-mix(in oklch, #ef4444 50%, transparent);
292292
}
293293
.variable {
294-
color: color-mix(in oklch, #ef4444 calc(var(--opacity) * 100%), transparent);
294+
color: color-mix(in oklch, #ef4444 var(--opacity), transparent);
295295
}
296296
:root {
297297
--color-red-500: #ef4444;

packages/tailwindcss/src/css-functions.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ describe('theme function', () => {
215215
}
216216
217217
.red {
218-
color: color-mix(in oklch, red calc(var(--opacity) * 100%), transparent);
218+
color: color-mix(in oklch, red var(--opacity), transparent);
219219
}"
220220
`)
221221
})
@@ -237,7 +237,7 @@ describe('theme function', () => {
237237
}
238238
239239
.red {
240-
color: color-mix(in oklch, red calc(var(--opacity, 50%) * 100%), transparent);
240+
color: color-mix(in oklch, red var(--opacity, 50%), transparent);
241241
}"
242242
`)
243243
})

packages/tailwindcss/src/utilities.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9969,7 +9969,7 @@ test('bg', async () => {
99699969
}
99709970
99719971
.bg-current\\/\\[var\\(--bg-opacity\\)\\] {
9972-
background-color: color-mix(in oklch, currentColor calc(var(--bg-opacity) * 100%), transparent);
9972+
background-color: color-mix(in oklch, currentColor var(--bg-opacity), transparent);
99739973
}
99749974
99759975
.bg-inherit {

packages/tailwindcss/src/utilities.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,6 @@ export function withAlpha(value: string, alpha: string): string {
112112
alpha = `${alphaAsNumber * 100}%`
113113
}
114114

115-
// If the alpha value is a percentage, we can pass it directly to
116-
// `color-mix()`. In any other case, e.g.: `var(…)`, `round(…)`, … we need to
117-
// make sure it's a percentage.
118-
else if (alpha[alpha.length - 1] !== '%') {
119-
alpha = `calc(${alpha} * 100%)`
120-
}
121-
122115
return `color-mix(in oklch, ${value} ${alpha}, transparent)`
123116
}
124117

0 commit comments

Comments
 (0)