Skip to content

Polyfill: Fall back to first color value when color-mix(…) contains unresolvable var(…) #17513

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

Merged

Conversation

philipp-spiess
Copy link
Member

@philipp-spiess philipp-spiess commented Apr 2, 2025

This PR further improves the color-mix(…) polyfill to create a reasonable fallback if dynamic values that can not statically be resolved are used. This refers to either the use of currentcolor or any variables that are not static theme variables.

Here are two examples that now generate a reasonable fallback instead of not showing any color at all:

.text-\\(--my-color\\)\\/\\(--my-opacity\\) {
  color: var(--my-color);
}
@supports (color: color-mix(in lab, red, red)) {
  .text-\\(--my-color\\)\\/\\(--my-opacity\\) {
    color: color-mix(in oklab, var(--my-color) var(--my-opacity), transparent);
  }
}
.text-current\\/50 {
  color: currentColor;
}

@supports (color: color-mix(in lab, red, red)) {
  .text-current\\/50 {
    color: color-mix(in oklab, currentColor 50%, transparent);
  }
}

Test plan

  • Made sure the test diffs are looking reasonable
  • Tested this on a production site with <p className="text-shadow-lg/50 [--my-color:red] text-shadow-(color:--my-color)">shadow test</p>
    • Browsers that do not support color-mix(…) will properly show a red shadow now albeit with 100% opacity: iOS 15.5 and Chrome 110
    • Browsers that I have tested to make sure it still works there with opacity: Firefox 127, Firefox 128, Latest Chrome, Safari, Firefox
    • Browsers that do show a black shadow because of var(…)var(…) being chained with no space by lightningcss: Chrome 111

@philipp-spiess philipp-spiess force-pushed the fix/color-mix-polyfills-with-unresolveable-css-vars branch from b3d75b2 to 64a3444 Compare April 3, 2025 10:11
@philipp-spiess philipp-spiess marked this pull request as ready for review April 3, 2025 10:11
@philipp-spiess philipp-spiess requested a review from a team as a code owner April 3, 2025 10:11
Comment on lines 1728 to 1718
@supports (not (-webkit-appearance: -apple-pay-button)) or (contain-intrinsic-size: 1px) {
::placeholder {
color: color-mix(in oklab, currentColor 50%, transparent);
color: currentColor;
@supports (color: color-mix(in lab, red, red)) {
color: color-mix(in oklab, currentColor 50%, transparent);
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit silly because we already have an @supports query in preflight and now we chain another one but when you think about it, the first one only makes sure that Safari versions that crash are gated out. Older versions of Chrome/Firefox _that do not support color-mix(…) would still not have a default in that case so this is a small improvement maybe?

In the future we might be able to clean this up if we use a different pass for applying that polyfill that has access to the parent path and then we can bail if this is already inside an @supprots or so.

@philipp-spiess philipp-spiess force-pushed the fix/color-mix-polyfills-with-unresolveable-css-vars branch from 980326b to 903ba82 Compare April 3, 2025 14:31
@philipp-spiess philipp-spiess force-pushed the fix/color-mix-polyfills-with-unresolveable-css-vars branch from 1a54575 to 30049fe Compare April 3, 2025 15:09
@RobinMalfait RobinMalfait enabled auto-merge (squash) April 3, 2025 15:09
@RobinMalfait RobinMalfait merged commit 60b0da9 into main Apr 3, 2025
7 checks passed
@RobinMalfait RobinMalfait deleted the fix/color-mix-polyfills-with-unresolveable-css-vars branch April 3, 2025 15:12
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