Skip to content

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

3 participants