Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix opacity modifier using CSS variables #14916

Merged
merged 5 commits into from
Nov 8, 2024

Conversation

RobinMalfait
Copy link
Member

When using an opacity modifier such as bg-black/[var(--opacity)], then this was translated to:

.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.

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%), transparent);
}
```

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/xz0tv5rbFF

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/#first-valid)
and generate both versions at the same time.
@RobinMalfait RobinMalfait requested a review from a team as a code owner November 8, 2024 12:47
CHANGELOG.md Outdated Show resolved Hide resolved
@adamwathan adamwathan enabled auto-merge (squash) November 8, 2024 15:11
@adamwathan adamwathan merged commit 7b75b1a into next Nov 8, 2024
1 check passed
@adamwathan adamwathan deleted the fix/opacity-modifier-with-variables branch November 8, 2024 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants