Skip to content

fix(compiler): scope single-definition variable inlining to the declaring block - #592

Merged
danstepanov merged 5 commits into
nativewind:mainfrom
sammoore:fix/scope-inlining
Nov 30, 2026
Merged

danstepanov merged 5 commits into
nativewind:mainfrom
sammoore:fix/scope-inlining

Conversation

@sammoore

Copy link
Copy Markdown
Contributor

Supersedes #413: same commit, rebased onto main, and four follow-up commits leave out pieces that aren't needed for the fix:

  • Excluding declarations in @supports blocks the compiler drops from the inlining census. On its own this breaks text-shadow-*, because Tailwind declares --tw-text-shadow-color only in that block. It needs a companion fix that folds a guaranteed-invalid property to its fallback.
  • Folding an undeclared registered property to its initial value at compile time. Since feat(native): honour inherits: false on registered custom properties #567 the runtime produces the same value.
    • This might be more desirable with the @supports patch / previous bullet implemented again?
  • Writing a <ratio> read through a variable as 16/9 instead of 16 / 9. React Native reads both.
  • Dropping a transparent box-shadow on the compile path. It paints nothing either way, and saves little by way of performance. Potentially a follow-up commit if folks don't agree.

Summary

A custom property declared once in a stylesheet was folded into every var() that reads it, whichever rule the var() was in. So a variable set by one class reached elements that don't have that class:

.parent { --x: 10px; }
.child { width: var(--x); }

<View className="child" /> on its own rendered width: 10. It now renders nothing, and inside .parent it still renders width: 10.

Fix

  • A single-declaration variable folds into a var() only where every element the rule matches is guaranteed that value: in the rule that declares it, from a * declaration, or from :root/html for an inheriting property. None of these fold from inside @media or @container. Everything else resolves at runtime, as inlineVariables: false already did.
  • A declaration is removed only once nothing else reads it. That includes reads inside a colour the compiler can't resolve, so rgb(0 0 255 / var(--a)) now gets its alpha.
  • parseUnparsed treats 0 and false as values instead of absences. Without this, the scoping change breaks kept variables set to 0 (width: var(--w) over --w: 0). It also fixes ring-offset-0 and outline-offset-0, which compiled to nothing, so md:ring-offset-0 and md:outline-offset-0 now override.
  • This keeps the inherits: false handling from feat(native): honour inherits: false on registered custom properties #567: a non-inheriting property, and any value that reads one, stays at runtime unless it's only declared on *.

Regression status

  • Nativewind v4: worked. With Tailwind on v4, [--brand:red] on a parent and bg-[--brand] on a child gives the child red, and a bare bg-[--brand] gets nothing. This fixes a v4 regression.
  • v5: broken in every release since the inliner was added in feat: inline css variables #167 (3.0.0-preview.3), including 3.1.0-rc.1.

Impact on Tailwind users

  • Stock utilities are unaffected by the scoping. Each of Tailwind 4.1's 18,903 classes was compiled on its own and in 1,584 pairs where one class reads a variable the other declares. 69,745 more sheets added variants (md:, dark:, hover:, *:, @md:, ios: and others). The only differences come from the 0 change above, and nothing throws. Every --tw-* variable that one utility sets and another reads is registered inherits: false, which already stayed at runtime. The one exception is the mask stops, and mask properties aren't supported.
  • These now behave as they did in v4 and as they do on the web: arbitrary properties read by another element ([--brand:red] + bg-(--brand)), @utility blocks and class themes that set a variable, md:[--x]/dark:[--x], and a :root variable declared only for dark mode. Before, they applied everywhere, including at the wrong breakpoint or colour scheme.
  • *:[--x:…] (or .list > * { --x: … }) no longer reaches the children. Child combinators aren't matched at runtime (*:bg-red-500 doesn't apply either), so this used to work only because the value leaked onto every element. v4 behaves the same.

Not changed

A :root variable is still folded, so an ancestor's vars() or VariableContextProvider can't override it (v4 allowed this). This is unchanged from main and needs its own issue.

Verification

  • yarn test (2045 tests), yarn typecheck, yarn lint
  • New tests in variable-inlining.test.tsx, inline-variables.test.ts and variable-fold-parity.test.tsx
  • Whole-surface Tailwind compile comparison against main, and nested-tree renders on v4 and on this branch
  • Performance on Hermes against main: the full suite's 14 scenarios are within −1.3%…+1.2% (the media query scenario +0.1%), which is noise. In a per-element micro-benchmark, everything is within noise except a variable read from another class costs about 2 µs more per element, because it now resolves at runtime where it used to fold a value the element doesn't have. translate-x-4 rotate-45 scale-110 is 8% faster, because a kept --tw-translate-y: 0 no longer throws away the whole translate.

YevheniiKotyrlo and others added 5 commits November 29, 2026 21:03
…ring block

The inliner folded a custom property declared once into every var() in the
stylesheet, whether or not the reference's element can hold the declaration.
It now folds a value only into the block that declares it, or from a
declaration outside @media and @container that reaches every element: `*`,
or `:root`, `:host` and `html` for an inheriting property. An @supports block
counts as extractRule decides it, through one rulesTheCompilerKeeps, so a
dropped block is emptied before the census; a registered `*` property that
nothing declares folds to its initial value; and a declaration is removed once
no rule reads it.

Values the fold no longer normalises reach the token path, which now reads
`0` and `false` as values and gives a <ratio> the descriptor parseAspectRatio
gives it. A compiled box-shadow drops a transparent shadow, as the runtime
does.
Removes the inliner's emptying of @supports blocks the compiler drops, and
rulesTheCompilerKeeps with it, so extractRule decides them through
supportsConditionValid as on main. It is a second bug (a dropped block's
declaration still counts as one) and not the scope bug this PR fixes.

Tailwind is the only producer of such a block, its @layer properties
fallback, and it repeats each @Property initial value there, so folding the
fallback gives the same values as the registration. With the block emptied,
--tw-text-shadow-color lost its only declaration, `initial`, its var()
reached the runtime, and text-shadow-md rendered no shadow there (a
text-shadow list read through a var() renders nothing on main too). Without
it, the Tailwind scan matches main in every utility but the two arbitrary
properties this PR scopes, which render the same.
…ntime

Removes the inliner's fold of a `syntax: "*"` property's initial value where
nothing declares it. Since nativewind#567 the stylesheet carries every registration
and the runtime resolves an undeclared property to its initial value, so the
fold only moved the same value to compile time; the runtime test for it,
`a registered initial value applies where nothing declares the property`,
passes without it. The Tailwind scan is unchanged.
…rites it

Removes the token path's ratioDescriptor, so a kept `var()` over `16 / 9`
reaches the runtime as the text "16 / 9" again, not "16/9". React Native's
processAspectRatio splits on "/" and trims each side, so both render the same
ratio; the change only made the parity test's kept and written values equal.
The parity rows now name the difference as a known one. Tailwind's
aspect-* utilities read a :root theme variable, which folds, so the scan is
unchanged.
Removes parseBoxShadow's filter and the shared isTransparentColor, so a
box-shadow the compiler parses keeps its transparent shadows again, and
restores box-shadow.test.tsx to main. A transparent shadow paints nothing,
so this changes no rendering; main's own tests already pin the compile path
keeping it and the runtime dropping it. The parity rows for the two
transparent lists now name that difference. Tailwind's shadow utilities read
their shadows through variables and take the runtime path either way, so the
scan is unchanged.
@danstepanov
danstepanov merged commit 5755630 into nativewind:main Nov 30, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants