Skip to content

Commit 6b43b64

Browse files
authored
Canonicalization: limit arbitrary to bare values conversion (#20130)
This PR improves the canonicalization process by limiting the bare values to a certain amount. Before this PR, whenever we have an arbitrary value, e.g. `left-[6px]`, then we prefer to use a bare value instead e.g. `left-1.5`. In most cases, this makes sense. However, there are places where this doesn't really make sense (https://x.com/kettanaito/status/2059987396050268589) - `left-[99999px]` → `left-24999.75 ` The hard part is to figure out _why_ this feels wrong. The `.75` could feel wrong, but in the `left-[6px]` → `left-1.5`, the `.5` makes sense. If we reduce that big number to `left-[99996px]` → `left-24999`, then there is no floating point but it still feels wrong. One possibility I can think of is to analyze the incoming value and see if we find certain patterns. All repeating numbers, fun numbers like `1337`, common numbers most programmers know such as `720px`, `1280px`, etc. But instead of that, I think it's more reasonable to limit the bare value such that the `px` based value doesn't exceed a big number. We can improve the logic if there are other cases that don't really make sense. The biggest value we have in our default theme is `--breakpoint-2xl: 96rem`, which is equivalent to `1536px`. So I think any bare value that results in a value `<= 1536px` should probably be fine. In this case, `left-[99999px]` would stay as `left-[99999px]`, but `left-[6px]` is still converted to `left-1.5`. Note: this is only happening for arbitrary values being converted to bare values _if_ they use the `--spacing` variable internally. Values such as `z-[99999999999]` will still be converted to `z-99999999999`, since the intent is still clear. ## Test plan 1. Added new tests for these limitations 2. Other existing tests still pass
1 parent d4f24c5 commit 6b43b64

3 files changed

Lines changed: 48 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Fix 'Sourcemap is likely to be incorrect' warnings when using `@tailwindcss/vite` ([#20103](https://github.com/tailwindlabs/tailwindcss/pull/20103))
2222
- Ensure `@tailwindcss/webpack` can be installed in Rspack projects without requiring `webpack` as a peer dependency ([#20027](https://github.com/tailwindlabs/tailwindcss/pull/20027))
2323
- Canonicalization: don't suggest invalid `calc(…)` expressions (e.g. `px-[calc(1rem+0px)]` → `px-[calc(1rem+0)]`) ([#20127](https://github.com/tailwindlabs/tailwindcss/pull/20127))
24+
- Canonicalization: avoid suggesting large spacing-scale values for arbitrary lengths (e.g. `left-[99999px]` → `left-[99999px]`, not `left-24999.75`) ([#20130](https://github.com/tailwindlabs/tailwindcss/pull/20130))
2425

2526
## [4.3.0] - 2026-05-08
2627

‎packages/tailwindcss/src/canonicalize-candidates.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,15 @@ describe.each([['default'], ['with-variant'], ['important'], ['prefix']])('%s',
871871
['[margin:-0]', 'm-0'],
872872
['[margin:0px]', 'm-0'],
873873

874+
// Limit conversions for big values using the `--spacing` multiplier
875+
['left-[99999px]', 'left-[99999px]'], // This would otherwise result in `left-24999.75`
876+
['left-[-99999px]', 'left-[-99999px]'], // This would otherwise result in `-left-24999.75`
877+
['left-[96rem]', 'left-384'], // Within the limit
878+
['left-[-96rem]', '-left-384'], // Within the limit
879+
['left-[calc(96rem+1px)]', 'left-[calc(96rem+1px)]'], // Out of the positive limit
880+
['left-[calc(-96rem-1px)]', 'left-[calc(-96rem-1px)]'], // Out of the negative limit
881+
['z-[9999999]', 'z-9999999'], // `--spacing` multiplier is not used
882+
874883
// Not a length-unit, can't safely constant fold
875884
['[margin:0%]', 'm-[0%]'],
876885

‎packages/tailwindcss/src/canonicalize-candidates.ts‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,35 @@ function printUnprefixedCandidate(designSystem: DesignSystem, candidate: Candida
10741074
// ----
10751075

10761076
const SPACING_KEY = Symbol()
1077+
1078+
// We prefer bare values over arbitrary values such as `left-[6px]` →
1079+
// `left-1.5`.
1080+
//
1081+
// There are situations where this doesn't always make sense. E.g.:
1082+
// `left-[99999px]` → `left-24999.75`.
1083+
//
1084+
// We could try and analyze the incoming value and bail out if we detect certain
1085+
// patterns. But a first step would be to limit the bare values for large values
1086+
// and keep them low. The largest rem value we have is for the 2xl breakpoint,
1087+
// which is 96rem, which is 1536px.
1088+
const MAX_BARE_VALUE_IN_PX = 1536
1089+
const MAX_BARE_VALUE_IN_REM = MAX_BARE_VALUE_IN_PX / 16
1090+
1091+
function isReasonableBareValue(value: number, designSystem: DesignSystem, rem: number | null) {
1092+
let spacingMultiplier = designSystem.resolveThemeValue('--spacing')
1093+
if (spacingMultiplier === undefined) return false
1094+
1095+
let parsed = dimensions.get(constantFoldDeclaration(spacingMultiplier, rem))
1096+
if (parsed === null) return false
1097+
1098+
let [spacingValue, spacingUnit] = parsed
1099+
let bareValueInPixels = value * spacingValue
1100+
1101+
if (spacingUnit === 'px') return bareValueInPixels <= MAX_BARE_VALUE_IN_PX
1102+
if (spacingUnit === 'rem') return bareValueInPixels <= MAX_BARE_VALUE_IN_REM
1103+
return false
1104+
}
1105+
10771106
function createSpacingCache(
10781107
designSystem: DesignSystem,
10791108
options?: CanonicalizeOptions,
@@ -1220,7 +1249,10 @@ function arbitraryUtilities(candidate: Candidate, options: InternalCanonicalizeO
12201249
) {
12211250
let bareValue = designSystem.storage[SPACING_KEY]?.get(value) ?? null
12221251
if (bareValue !== null) {
1223-
if (isValidSpacingMultiplier(bareValue)) {
1252+
if (
1253+
isValidSpacingMultiplier(bareValue) &&
1254+
isReasonableBareValue(bareValue, designSystem, options.signatureOptions.rem)
1255+
) {
12241256
yield Object.assign({}, candidate, {
12251257
value: { kind: 'named', value: bareValue, fraction: null },
12261258
})
@@ -1261,7 +1293,11 @@ function arbitraryUtilities(candidate: Candidate, options: InternalCanonicalizeO
12611293
// Try bare value based on the `--spacing` value. E.g.:
12621294
//
12631295
// - `w-[64rem]` → `w-256`
1264-
if (spacingMultiplier !== null) {
1296+
if (
1297+
spacingMultiplier !== null &&
1298+
isValidSpacingMultiplier(spacingMultiplier) &&
1299+
isReasonableBareValue(spacingMultiplier, designSystem, options.signatureOptions.rem)
1300+
) {
12651301
for (let replacementCandidate of parseCandidate(
12661302
designSystem,
12671303
`${root}-${spacingMultiplier}`,

0 commit comments

Comments
 (0)