Skip color-mix(…) when opacity is 100% (#17815)

This PR improves colors with alpha values where the alpha value results
in 100%.

Before this change, a class like `bg-red-500/100` would be generated as:

```css
.bg-red-500\/100 {
  background-color: #ef4444;
}

@supports (color: color-mix(in lab, red, red)) {
  .bg-red-500\/100 {
    background-color: color-mix(in oklab, var(--color-red-500) 100%, transparent);
  }
}
```

But we don't need the `color-mix`, or the fallback styles at all in case
the alpha value is 100%.

After this change the `bg-red-500/100` class will generate as:

```css
.bg-red-500\/100 {
  background-color: var(--color-red-500);
}
```

Which is essentially the same as `bg-red-500`, but we can migrate that
away in the future. At least the generated CSS is smaller.

## Test plan

1. Added a test to ensure the generated value doesn't include color-mix
at all.
This commit is contained in:
Robin Malfait 2025-04-28 19:30:01 +02:00 committed by GitHub
parent 3a1b27e3f8
commit d2daf59524
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 16 additions and 0 deletions

View File

@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Upgrade: Bump all Tailwind CSS related dependencies during upgrade ([#17763](https://github.com/tailwindlabs/tailwindcss/pull/17763))
- PostCSS: Ensure that errors in stylesheet dependencies are recoverable ([#17754](https://github.com/tailwindlabs/tailwindcss/pull/17754))
- Upgrade: Correctly print variants starting with `@` ([#17814](https://github.com/tailwindlabs/tailwindcss/pull/17814))
- Skip `color-mix(…)` when opacity is `100%` ([#17815](https://github.com/tailwindlabs/tailwindcss/pull/17815))
## [4.1.4] - 2025-04-14

View File

@ -10695,6 +10695,8 @@ test('bg', async () => {
'bg-red-500/2.75',
'bg-red-500/[0.5]',
'bg-red-500/[50%]',
'bg-red-500/100',
'bg-red-500/[100%]',
'bg-blue-500',
'bg-current',
'bg-current/50',
@ -10992,6 +10994,10 @@ test('bg', async () => {
}
}
.bg-red-500\\/100 {
background-color: var(--color-red-500);
}
.bg-red-500\\/\\[0\\.5\\] {
background-color: #ef444480;
}
@ -11012,6 +11018,10 @@ test('bg', async () => {
}
}
.bg-red-500\\/\\[100\\%\\] {
background-color: var(--color-red-500);
}
.bg-transparent {
background-color: #0000;
}

View File

@ -173,6 +173,11 @@ export function withAlpha(value: string, alpha: string): string {
alpha = `${alphaAsNumber * 100}%`
}
// No need for `color-mix` if the alpha is `100%`
if (alpha === '100%') {
return value
}
return `color-mix(in oklab, ${value} ${alpha}, transparent)`
}