Fix 0px being converted to unitless 0 and breaking calc calculations#121
Open
philwolstenholme wants to merge 2 commits into
Open
Fix 0px being converted to unitless 0 and breaking calc calculations#121philwolstenholme wants to merge 2 commits into
calc calculations#121philwolstenholme wants to merge 2 commits into
Conversation
Zero pixel values (0px) are semantically equivalent to 0rem and 0 in CSS, so converting them provides no benefit. More importantly, converting 0px to a unitless 0 breaks CSS custom properties such as Tailwind's --tw-ring-offset-width when they are used in contexts that require units. https://claude.ai/code/session_01RNkSDiLSYfS94qrFXguRF2
calc calculations
More precise than * since the test is specifically about CSS custom properties being processed and zero pixel values being preserved. https://claude.ai/code/session_01RNkSDiLSYfS94qrFXguRF2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This MR preserves
0pxvalues instead of converting to unitless0.Sorry for the extra noise on the diff, I wanted to only commit my change and the new test, but Husky ran
lint-stagedwhich ran Prettier, which made quite a few unrelated formatting changes.Possibly related: I noticed this test was failing on
masterwithout any changes having been made:Why
When a CSS custom property with a
0pxvalue was processed — for example from Tailwind CSS:the plugin was rewriting it to:
This breaks any downstream
calc()expression that uses the variable, such as:CSS
calc()requires all operands to have compatible types. A unitless0is a<number>, not a<length>, socalc(1px + 0)is invalid and browsers will discard the entire declaration.calc(1px + 0px)is valid.Fix
Skip conversion entirely when the pixel value is zero. Converting
0pxto0remprovides no benefit, and converting it to a bare0is incorrect for the reason above.const pixels = parseFloat($1); +if (pixels === 0) return m; if (pixels < minPixelValue) return m;Workaround if this isn't merged
For anyone else with this issue, a workaround is to use the existing
minPixelValueconfig option to make the plugin avoid converting values less than 1:Tests
Added a regression test covering the CSS custom property case. This test might be a bit too specific but it is based on a real world test case I suppose.
All tests are green:
AI disclaimer
This code comes from a Claude Code session.