Skip to content

fix(compiler): list a stylesheet's variables in the same order on every read - #476

Open
YevheniiKotyrlo wants to merge 1 commit into
nativewind:mainfrom
YevheniiKotyrlo:fix/stylesheet-variable-order
Open

YevheniiKotyrlo wants to merge 1 commit into
nativewind:mainfrom
YevheniiKotyrlo:fix/stylesheet-variable-order

Conversation

@YevheniiKotyrlo

Copy link
Copy Markdown
Contributor

Problem

getNativeStyleSheet reverses each root and universal variable list in place to put the most specific value first. Reading the stylesheet a second time reverses the lists back. Measured on main:

const compiled = compile(
  `:root { --tone: red; }
   @media (prefers-color-scheme: dark) { :root { --tone: blue; } }`,
  { inlineVariables: false },
);
compiled.stylesheet().vr; // [["tone", [["blue", [["=", "prefers-color-scheme", "dark"]]], ["red"]]]]
compiled.stylesheet().vr; // [["tone", [["red"], ["blue", [["=", "prefers-color-scheme", "dark"]]]]]]

The second read lists the unconditioned value first, so the dark value never wins, and the first sheet's lists change under it because both reads hand out the builder's own arrays. The same holds for vu.

Solution

Both lists are reversed into a copy with toReversed(), so every read of a stylesheet is the same and a sheet already handed out stays as it was.

Tests

  • src/__tests__/compiler/compiler.test.tsx: reading the stylesheet twice gives the same vr and vu, most specific value first, and leaves the first read unchanged.
  • On main, the new case fails at the second read and the other 14 in the file pass. Restoring the in-place reverse on either list fails it, and dropping the reverse altogether also fails :root CSS variables with media queries.

Verification

On Windows with Node 26:

  • yarn lint clean
  • yarn typecheck clean
  • yarn test --coverage: every failure also fails on main on this machine (the four babel cases)
  • yarn build clean
  • yarn example expo export --platform web exported

No existing issue tracks this. I searched for getNativeStyleSheet, rootVariables reverse and variables order media.

Base

Branched off main (a5002c5).

…ry read

getNativeStyleSheet reversed each root and universal variable list in
place to put the most specific value first. Reading the stylesheet again
reversed the lists back, so a second read listed the unconditioned value
first, and the first sheet's lists changed under it.

The lists are now reversed into a copy.

This branch has not been deployed

No deployments
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.

1 participant