Skip to content

fix: let a custom property set to initial override other declarations - #585

Merged
danstepanov merged 1 commit into
nativewind:mainfrom
sammoore:fix/custom-property-initial
Nov 23, 2026
Merged

danstepanov merged 1 commit into
nativewind:mainfrom
sammoore:fix/custom-property-initial

Conversation

@sammoore

Copy link
Copy Markdown
Contributor

The approach follows #391's f9caff0 (thanks @YevheniiKotyrlo). This is the part of it that fixes the Tailwind bug, written for main so it doesn't need the rest of #391.

The bug

Tailwind v4 implements via-none and the *-initial utilities by setting a variable to initial. The reader then takes its var() fallback:

.via-none        { --tw-gradient-via-stops: initial; }
.shadow-initial  { --tw-shadow-color: initial; }   /* read as var(--tw-shadow-color, rgb(0 0 0 / 0.1)) */

The compiler dropped --x: initial with a warning. So whenever another rule declared the same variable, that rule's value won. Used with a variant, these utilities did nothing:

className, at md and wider Browser main
from-red-500 via-green-500 md:via-none to-blue-500 2 stops 3 stops (green kept)
shadow-md shadow-red-500 md:shadow-initial default shadow colour red
inset-shadow-sm inset-shadow-red-500 md:inset-shadow-initial default inset colour red

On its own (shadow-md shadow-initial) the utility already worked, because the inliner turns a single initial into the fallback. Every use of these utilities also printed a spurious initial value warning, which Metro builds show since #430.

Hand-written CSS had the same bug: .a { --x: red } .b { --x: initial } gave red on class="a b", and so did a child setting --x: initial under a parent with --x: red.

The fix

  • Compiler: a custom property whose whole value is initial compiles to an initial marker instead of being dropped.
  • Runtime: when that marker is the element's own winning value, var() skips the element's other declarations, * and the parent's value. It takes the registered initial value if an @property gives one, and otherwise its fallback (css-variables-1 §2.2, css-properties-values-api-1 §2). An ancestor's marker already resolves to nothing, which main treats as the guaranteed-invalid value, so a child reading it takes its own fallback.

The marker is an ordinary declaration, so the cascade decides the same way as any other value. .a { --x: red !important } .b { --x: initial } still gives red, and --x: initial !important beats a later, normal --x: red, as in browsers (on main the second gave red).

Regression status

Not a regression per-se relative to Tailwind 3 (Nativewind v4): that has no via-none or *-initial utilities.

This is a bug that has always been in react-native-css (i.e. Nativewind v5 pre-releases), and it reaches stock Tailwind utilities used with a variant (md:, dark:, hover:…) in Tailwind 4.

Impact

  • Tailwind: a scan of 84 class sets (transforms, filters, shadows, gradients, every *-initial and via-none variant, var chains and arbitrary values) changes only the three rows above, each to the browser's result. Nothing fails React Native's validators.
  • Hand-written CSS: with variables inlined and kept, --x: initial now overrides an earlier rule's value, a parent's value, and an earlier value from a rule inside @media. When --x: initial is itself !important, it also beats a later rule. As before, a later rule overrides initial, and so does an !important value that initial isn't.
  • Tests:
    • vendor/tailwind/initial-overrides.test.tsx runs the three utilities through real Tailwind at two widths. All 6 cases fail on main: the 3 wide ones on the value, the 3 narrow ones on the spurious warning.
    • native/variables.test.tsx adds 6 cases in two modes (variables inlined and kept). On main, 10 of the 12 fail; the two that pass check that a later rule still overrides initial.
  • Performance (Hermes): the check runs only once the element's own value has resolved to nothing, so an element without an initial variable doesn't pay for it.
  • Tests: 1909/1909 pass, and typecheck and lint are clean.

Not in this PR

The compiler dropped `--x: initial` with a warning, so any other rule
declaring the same variable won: Tailwind's `md:via-none`,
`md:shadow-initial` and `md:inset-shadow-initial` kept the colour that
`via-*`, `shadow-*` and `inset-shadow-*` set.

The declaration now compiles to an `initial` marker. When it is an
element's own winning value, var() skips the element's other rules, `*`
and the parent, and takes the registered initial value or else its
fallback (css-variables-1 §2.2, css-properties-values-api-1 §2). An
ancestor's marker already resolves to nothing, which main treats as
the guaranteed-invalid value.

Co-authored-by: Yevhenii <evgeny.kotyrlo@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@danstepanov
danstepanov force-pushed the fix/custom-property-initial branch from 33fbcc3 to 193c000 Compare November 23, 2026 21:06
@danstepanov
danstepanov merged commit c467973 into nativewind:main Nov 23, 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.

2 participants