Commit 229121d
authored
This PR improves the canonicalization when using `text-*` and
`leading-*` utilities together.
When using classes such as:
```html
<div class="text-sm leading-7"></div>
```
Then the canonical way of writing this is:
```html
<div class="text-sm/7"></div>
```
Similarly, if you already have a modifier applied, and add a new
line-height utility. It will also combine them into the canonical form:
```html
<div class="text-sm/6 leading-7"></div>
```
becomes:
```html
<div class="text-sm/7"></div>
```
This is because the final CSS output of `text-sm/6 leading-7` is:
```css
/*! tailwindcss v4.1.16 | MIT License | https://tailwindcss.com */
.text-sm\/6 {
font-size: var(--text-sm, 0.875rem);
line-height: calc(var(--spacing, 0.25rem) * 6);
}
.leading-7 {
--tw-leading: calc(var(--spacing, 0.25rem) * 7);
line-height: calc(var(--spacing, 0.25rem) * 7);
}
@Property --tw-leading {
syntax: "*";
inherits: false;
}
```
Where the `line-height` of the `leading-7` class wins over the
`line-height` of the `text-sm/6` class.
### Implementation
#### On the fly pre-computation
Right now, we are not using any AST based transformations yet and
instead rely on a pre-computed list. However, with arbitrary values we
don't have pre-computed values for `text-sm/123` for example.
What we do instead is if we see a utility that sets `line-height` and
other utilities set `font-size` then we pre-compute those computations
on the fly.
We will prefer named font-sizes (such as `sm`, `lg`, etc). We will also
prefer bare values for line-height (such as `7`) over arbitrary values
(such as `[123px]`).
#### Canonicalization of the CSS AST
Another thing we had to do is to make sure that when multiple
declarations of the same property exist, that we only keep the last one.
In the real world, multiple declarations of the same value is typically
used for fallback values (e.g.: `background-color: #fff;
background-color: oklab(255 255 255 / 1);`).
But for our use case, I believe we can safely remove the earlier
declarations to make the most modern and thus the last declaration win.
#### Trying combinations based on `property` only
One small change we had to make is that we try combinations of utilities
based on property only instead of property _and_ value. This is
important for cases such as `text-sm/6 leading-7`. These 2 classes will
set a `lin-height` of `24px` and `28px` respectively so they will never
match.
However, once combined together, there will be 2 line-height values, and
the last one wins. The signature of `text-sm/6 leading-7` becomes:
```css
.x {
font-size: 14px; /* From text-sm/6 */
line-height: 24px; /* From text-sm/6 */
line-height: 28px; /* From leading-7 */
}
```
↓↓↓↓↓↓↓↓↓
```css
.x {
font-size: 14px; /* From text-sm/6 */
line-height: 28px; /* From leading-7 */
}
```
This now shows that just `text-sm/7` is the canonical form. Because it
produces the same final CSS output.
## Test plan
1. All existing tests pass
2. Added a bunch of new tests where we combine `text-*` and `leading-*`
utilities with named, bare and arbitrary values. Even with existing
modifiers on the text utilities.
<img width="1010" height="1099" alt="image"
src="https://github.com/user-attachments/assets/d2775692-a442-4604-8371-21dacf16ebfc"
/>
1 parent 243615e commit 229121d
File tree
4 files changed
+208
-64
lines changed- packages/tailwindcss/src
4 files changed
+208
-64
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
| |||
Lines changed: 55 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| |||
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
57 | | - | |
| 58 | + | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
| |||
1025 | 1026 | | |
1026 | 1027 | | |
1027 | 1028 | | |
1028 | | - | |
1029 | | - | |
1030 | | - | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
1031 | 1033 | | |
1032 | | - | |
1033 | | - | |
| 1034 | + | |
| 1035 | + | |
1034 | 1036 | | |
1035 | | - | |
1036 | | - | |
| 1037 | + | |
| 1038 | + | |
1037 | 1039 | | |
1038 | | - | |
1039 | | - | |
| 1040 | + | |
| 1041 | + | |
1040 | 1042 | | |
1041 | | - | |
1042 | | - | |
| 1043 | + | |
| 1044 | + | |
1043 | 1045 | | |
1044 | | - | |
1045 | | - | |
| 1046 | + | |
| 1047 | + | |
1046 | 1048 | | |
1047 | | - | |
1048 | | - | |
1049 | | - | |
1050 | | - | |
1051 | | - | |
1052 | | - | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
1053 | 1052 | | |
1054 | 1053 | | |
1055 | 1054 | | |
1056 | 1055 | | |
1057 | | - | |
1058 | | - | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
1059 | 1092 | | |
1060 | 1093 | | |
1061 | 1094 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
111 | 114 | | |
112 | 115 | | |
113 | 116 | | |
| |||
116 | 119 | | |
117 | 120 | | |
118 | 121 | | |
119 | | - | |
| 122 | + | |
120 | 123 | | |
121 | 124 | | |
122 | 125 | | |
| |||
144 | 147 | | |
145 | 148 | | |
146 | 149 | | |
147 | | - | |
| 150 | + | |
148 | 151 | | |
149 | 152 | | |
150 | 153 | | |
| |||
255 | 258 | | |
256 | 259 | | |
257 | 260 | | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
258 | 311 | | |
259 | 312 | | |
260 | 313 | | |
261 | 314 | | |
262 | 315 | | |
263 | 316 | | |
264 | 317 | | |
265 | | - | |
266 | | - | |
267 | | - | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
268 | 325 | | |
269 | | - | |
270 | | - | |
| 326 | + | |
| 327 | + | |
271 | 328 | | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
276 | 332 | | |
277 | 333 | | |
278 | 334 | | |
| |||
286 | 342 | | |
287 | 343 | | |
288 | 344 | | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
294 | 349 | | |
295 | 350 | | |
296 | 351 | | |
| |||
881 | 936 | | |
882 | 937 | | |
883 | 938 | | |
| 939 | + | |
884 | 940 | | |
885 | 941 | | |
886 | 942 | | |
887 | 943 | | |
| 944 | + | |
| 945 | + | |
888 | 946 | | |
889 | 947 | | |
890 | 948 | | |
891 | 949 | | |
892 | 950 | | |
893 | 951 | | |
894 | | - | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
895 | 958 | | |
896 | 959 | | |
897 | 960 | | |
| |||
998 | 1061 | | |
999 | 1062 | | |
1000 | 1063 | | |
1001 | | - | |
1002 | | - | |
1003 | | - | |
1004 | | - | |
1005 | | - | |
1006 | | - | |
1007 | | - | |
1008 | | - | |
1009 | | - | |
1010 | | - | |
1011 | | - | |
1012 | | - | |
1013 | | - | |
1014 | | - | |
1015 | | - | |
1016 | | - | |
1017 | | - | |
1018 | | - | |
1019 | | - | |
1020 | | - | |
1021 | | - | |
1022 | | - | |
1023 | | - | |
1024 | | - | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
1025 | 1070 | | |
1026 | 1071 | | |
1027 | 1072 | | |
| |||
2093 | 2138 | | |
2094 | 2139 | | |
2095 | 2140 | | |
| 2141 | + | |
| 2142 | + | |
| 2143 | + | |
| 2144 | + | |
| 2145 | + | |
| 2146 | + | |
| 2147 | + | |
| 2148 | + | |
| 2149 | + | |
| 2150 | + | |
| 2151 | + | |
| 2152 | + | |
| 2153 | + | |
| 2154 | + | |
| 2155 | + | |
| 2156 | + | |
| 2157 | + | |
| 2158 | + | |
| 2159 | + | |
| 2160 | + | |
2096 | 2161 | | |
2097 | 2162 | | |
2098 | 2163 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
0 commit comments