Refactor gradient implementation to work around prettier/prettier#17058#16072
Refactor gradient implementation to work around prettier/prettier#17058#16072RobinMalfait merged 5 commits intomainfrom
Conversation
We encoded the `,` in the `--tw-gradient-position` to ensure that _if_
the `var(--tw-gradient-position)` is used, that the `,` was there. And
if the variable was _not_ used that we didn't end up with a double `,,`
rendering the gradient invalid.
However, when running prettier (there might be other tools that do this
as well), the trailing comma in the `--tw-gradient-position` was removed
which made the entire gradient invalid. E.g.:
```diff
.bg-gradient-to-r {
- --tw-gradient-position: to right in oklab,;
+ --tw-gradient-position: to right in oklab;
background-image: linear-gradient(var(--tw-gradient-stops));
}
```
Notice how the `,` is removed.
This commit fixes that, by moving the `,` to where it is used. The only
side effect is that we have to guarantee that the
`--tw-gradient-position` is always present. In our testing (and using UI
tests) this should be the case.
Thus, hopefully this fix is safe in all situations.
|
@RobinMalfait upgrading to @utility gradient-upleveled-image {
@apply from-hotPink/[0.93] via-pink/[0.93] to-purple/[0.93] bg-[linear-gradient(170deg,var(--tw-gradient-stops)),url('/bg.webp')] via-27% bg-cover [background-position:center_75%] bg-no-repeat;
}Interestingly, it's also generating the Before ( .gradient-upleveled-image {
background-image: linear-gradient(170deg,var(--tw-gradient-stops)),url(/bg.webp);
--tw-gradient-from: color-mix(in oklab,var(--color-hotPink)93%,#0000);
--tw-gradient-stops: var(--tw-gradient-via-stops,var(--tw-gradient-position,)var(--tw-gradient-from)var(--tw-gradient-from-position),var(--tw-gradient-to)var(--tw-gradient-to-position));
--tw-gradient-via: color-mix(in oklab,var(--color-pink)93%,#0000);
--tw-gradient-via-stops: var(--tw-gradient-position,)var(--tw-gradient-from)var(--tw-gradient-from-position),var(--tw-gradient-via)var(--tw-gradient-via-position),var(--tw-gradient-to)var(--tw-gradient-to-position);
--tw-gradient-via-position: 27%;
--tw-gradient-to: color-mix(in oklab,var(--color-purple)93%,#0000);
background-position: 50% 75%;
background-repeat: no-repeat;
background-size: cover
}After ( .gradient-upleveled-image {
background-image: linear-gradient(170deg,var(--tw-gradient-stops)),url('/images/bg.webp');
--tw-gradient-from: color-mix(in oklab, var(--color-hotPink) 93%, transparent);
/* 1 */
--tw-gradient-stops: var(--tw-gradient-via-stops, var(--tw-gradient-position), var(--tw-gradient-from) var(--tw-gradient-from-position), var(--tw-gradient-to) var(--tw-gradient-to-position));
--tw-gradient-via: color-mix(in oklab, var(--color-pink) 93%, transparent);
--tw-gradient-via-stops: var(--tw-gradient-position), var(--tw-gradient-from) var(--tw-gradient-from-position), var(--tw-gradient-via) var(--tw-gradient-via-position), var(--tw-gradient-to) var(--tw-gradient-to-position);
/* 2 */
--tw-gradient-stops: var(--tw-gradient-via-stops);
--tw-gradient-via-position: 27%;
--tw-gradient-to: color-mix(in oklab, var(--color-purple) 93%, transparent);
/* 3 */
--tw-gradient-stops: var(--tw-gradient-via-stops, var(--tw-gradient-position), var(--tw-gradient-from) var(--tw-gradient-from-position), var(--tw-gradient-to) var(--tw-gradient-to-position));
background-size: cover;
background-position: center 75%;
background-repeat: no-repeat;
}Maybe the If this syntax should be supported and it's actually a bug, then happy to also create a new issue with this report above. |
It appears as if this is true, at least starting with Tailwind CSS 4.0.2. Using the new @apply
from-hotPink/[0.93] via-pink/[0.93] to-purple/[0.93]
- bg-[linear-gradient(170deg,var(--tw-gradient-stops)),url('/bg.webp')]
+ bg-linear-170
+ bg-[linear-gradient(var(--tw-gradient-stops)),url('/bg.webp')]
via-27% bg-cover [background-position:center_75%] bg-no-repeat; |
This PR fixes an issue where tools like Prettier remove important trailing commas in CSS variables, making gradients invalid.
We encoded the
,in the--tw-gradient-positionto ensure that if thevar(--tw-gradient-position)is used, that the,was there. And if the variable was not used that we didn't end up with a double,,rendering the gradient invalid.However, when running Prettier (there might be other tools that do this as well), the trailing comma in the
--tw-gradient-positionwas removed which made the entire gradient invalid. E.g.:.bg-gradient-to-r { - --tw-gradient-position: to right in oklab,; + --tw-gradient-position: to right in oklab; background-image: linear-gradient(var(--tw-gradient-stops)); }Notice how the
,is removed.This PR fixes that, by moving the
,to where the variable is being used. The only side effect is that we have to guarantee that the--tw-gradient-positionis always present. In our testing (and using UI tests) this should be the case.Related Prettier issue: prettier/prettier#17058
Fixes: #16037