Skip to content

Commit 95ee4a4

Browse files
committed
ensure we don't fold to an insane precision value
100% / 3.5 = 28.571428571428573%, which is correct but not as user friendly. Especially when going from `w-[calc(100%/3.5)]` → `w-[28.571428571428573%]`
1 parent ca1887a commit 95ee4a4

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

packages/tailwindcss/src/constant-fold-declaration.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,19 @@ export function constantFoldDeclarationAst(
286286
((lhs[1] === null && rhs[1] === null) || // Unitless / Unitless, e.g.: `8 / 2`
287287
(lhs[1] !== null && rhs[1] === null)) // Unit / Unitless, e.g.: `1rem / 2`
288288
) {
289+
let computed = lhs[0] / rhs[0]
290+
291+
// Only fold with .xx precision, anything beyond that might be a bit too much.
292+
//
293+
// E.g. 100% / 3.5 = 28.571428571428573%, which is correct but not
294+
// as user friendly. Especially when going from
295+
// `w-[calc(100%/3.5)]` → `w-[28.571428571428573%]`
296+
if (Math.floor(computed * 100) / 100 !== computed) {
297+
break
298+
}
299+
289300
folded = true
290-
return WalkAction.ReplaceSkip(ValueParser.word(`${lhs[0] / rhs[0]}${lhs[1] ?? ''}`))
301+
return WalkAction.ReplaceSkip(ValueParser.word(`${computed}${lhs[1] ?? ''}`))
291302
}
292303
break
293304
}

0 commit comments

Comments
 (0)