Skip to content

fix: evaluate range pairs, and answer a condition the runtime cannot decide with unknown - #608

Merged
danstepanov merged 8 commits into
nativewind:mainfrom
sammoore:fix/media-condition-unknown
Dec 4, 2026
Merged

danstepanov merged 8 commits into
nativewind:mainfrom
sammoore:fix/media-condition-unknown

Conversation

@sammoore

@sammoore sammoore commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Supersedes #426, with some patches split out for separate review. See "Changes from the original PR" for details.

Problem

On main:

  1. A range pair never matches. Both runtimes answer (400px < width < 800px) and every other interval false.
  2. A negated term the runtime cannot decide matches. Such a term answers false, so not (fictional-feature: 3) and @container not (orientation: bogus) apply everywhere. MQ5 §3.1 makes the term and its negation unknown, and Chromium, Firefox and WebKit apply neither.
  3. The compiler drops a media term it cannot resolve, so not ((width: env(safe-area-inset-top)) or (width > 9999px)) applies, which no engine does.
  4. Container conditions keep what CSS Conditional 5 §5.4 discards: not (fictional-feature > 10px) applies.
  5. A feature in a boolean context never matches: (width), (orientation), (scripting) (MQ5 §2.4.2).
  6. pointer is never answered, so (pointer: coarse) never matches on a phone.
  7. A keyword and a media type compare as written, so (prefers-color-scheme: DARK) and @media IOS never match (css-values-4 §4.1, MQ4 §3).
  8. A negative resolution compiles to a negative pixel ratio, so (min-resolution: -1dpi) applies on every screen, where css-values-4 §7.4 excludes it and the engines leave the feature unknown.
  9. A viewport length never compares: (width > 50vw) never matches and not (width: 100vw) always does.

Solution

  • Media and container conditions evaluate through one walk, evaluateCondition, and one comparison, compareMediaFeature, in three-valued logic (kleene.ts, MQ5 §3.1), with undefined as unknown. A rule applies only when its condition is true. A rule that the compiler's unknown terms leave no way to be true compiles to no rule.

  • A term is unknown when:

    • the runtime does not measure its feature;
    • its value is outside the feature's syntax: a keyword or type of value MQ5 does not give it, env(), or a negative resolution or ratio component;
    • or the compiler cannot resolve its operand, which compiles to null so JSON.stringify carries it.
  • A container condition that §5.4 discards compiles to no rule, as Chromium and WebKit drop it. A style() query compiles to an unknown term.

  • A viewport length in a condition resolves against the window (css-values-4 §6.1.2).

  • In a boolean context, zero, none and no-preference are false and every other value true.

  • The runtime answers these features:

    • pointer and any-pointer answer for the primary input (MQ5 §7.1), as Safari and Chrome do on a phone: coarse on iOS and Android, none on a TV, and fine on Mac Catalyst and other platforms.
    • scripting answers enabled.
    • forced-colors answers none, except on Windows (§12.4).
    • prefers-reduced-motion is unknown until AccessibilityInfo answers.
    • Every (hover: …) comparison holds, as on main. any-hover and a bare (hover) are unanswered.

    Under three-valued logic these answers keep Tailwind's not-pointer-*:, not-noscript: and not-forced-colors: applying.

  • Keywords and media types fold to lowercase.

I changed #451's container-rejection expectation that not ((width > 100px) and style(--theme: dark)) applies at no width. At 50px the conjunction is false whatever style() answers, so its negation applies, as in all three engines.

Tailwind impact

No stock Tailwind variant writes a range pair. I rendered 54 media variants and their not- forms on iOS, Android, Mac Catalyst and a TV, in light, dark and system schemes, with motion unset, no-preference and reduce, idle and hovered. Against main:

  • New: pointer-coarse: and any-pointer-coarse: apply on phones, and their not- forms stop applying.
  • Turned off by three-valued logic:
    • not-contrast-more:, not-contrast-less: and not-inverted-colors: never apply, since React Native reports none of those settings.
    • not-motion-safe: and not-motion-reduce: don't apply until AccessibilityInfo answers.
    • not-dark: doesn't apply after colorScheme.set("system") holds null, which fix(native): make colorScheme.set move every reader of the scheme #415 fixes.
  • Identical: width breakpoints, max-*, min-[…], portrait/landscape, print, dark:, motion-*, forced-colors:, noscript:, hover: and not-hover:.

Tests

  • compiler/condition-semantics.test.ts: what each condition that the native tests render compiles to, a value of another type, a condition lightningcss's types do not list, and the compiler's pruning against the runtime's answers.
  • native/condition-semantics.test.tsx: each condition rendered on a screen or in a container, per platform where it differs, in memory and after a JSON round trip. compare.test.ts, kleene.test.ts and units.test.tsx cover the shared helpers.
  • vendor/tailwind/states.test.tsx and groups.test.tsx: hover: and group-hover: on Mac Catalyst and on a touchscreen's platform. These and both container-queries.test.tsx assert the expected colour absent, as toHaveStyle(undefined) matched any style.
  • Each expected value is what Chromium 156, Firefox 157 and WebKit 27.2 compute, and a test that departs from one says which and why. On main, 194 of the 550 cases in the two condition-semantics files and container-rejection.test.tsx fail.

Verification

  • yarn test passes 2991 of 2991, yarn typecheck is clean, and yarn lint is clean apart from node/babel.mjs, which fails the same way on main in a fresh checkout because it imports the unbuilt dist.
  • On Hermes, every scenario in bench/hermes is within noise against main, at −1.6% to +2.0% (mount, media query +0.3%), and memory is identical.
  • Engine answers and device evidence for the original head: this comment.

Changes from the original PR

This is #426 rebased onto main (now including #480, #605 and #607), with the author's commit kept intact and our changes as follow-up commits:

  • Hover keeps main's answer, so Tailwind's hover:, group-hover: and peer-hover: apply wherever a hover arrives, as in previous Nativewind releases.
    • Answering it for the primary input is split into its own patch until we decide what hover should mean in React Native.
  • Device features no Tailwind variant reaches are split out: color, color-index, monochrome, grid, update, overflow-block, overflow-inline, scan, color-gamut, dynamic-range and their video- forms, -webkit-device-pixel-ratio, and <mq-boolean> compiling to an integer.
  • Clamping non-finite numbers is split out. It only affects operands lightningcss gives as infinite or NaN, and potentially changes output (even if barely). I want to review safeNumber on its own to be sure it's the right shape.
  • The readonly condition types are split out (DeepReadonly, readonly tuples, condition-types.test.ts). They change no emitted code, and similar to the previous bullets, I want to review this refactor independently.
  • An unknown condition is undefined, not the string "unknown", so the Truth type and the UNKNOWN constant are gone.
  • -0dppx compiles to -0. Since fix(compiler): support lightningcss 1.30.2+ visitor deserialization #605, lightningcss no longer round-trips untouched rules through its serializer, which wrote -0 as 0. It compares as 0 and JSON sends it as 0.

Each of the first four bullets is a patch in the author's name that applies on its own to this branch, ready to be its own PR. The last two are changes to this branch and have no patch.

Source diff against main: +772/−320 in 13 files as written → +650/−277 in 11 files.

Known limits

  • lightningcss has three bugs that reach conditions:
    • It rewrites (min-width) as (width) (parcel-bundler/lightningcss#1366).
    • It drops the not of an equality range, so not (width = 100vw) holds on every screen.
    • It prints a calc(NaN) as an overflowing number, which a device reads as unresolved, so (width < calc(NaN * 1vw)) is unknown there.
  • Hover is undecided. Every (hover: …) comparison holds, as on main, so (hover: none) matches too.
  • Device features no Tailwind variant reaches are unanswered, so they are unknown: color, color-index, monochrome, grid, update, overflow-*, scan, color-gamut, dynamic-range and their video- forms, and -webkit-device-pixel-ratio.
  • Changes of their own:
  • React Native reports no input device but the primary one (Platform.ios.js, Platform.android.js), so an attached mouse or stylus goes unseen.
    • isMacCatalyst is set at build time (RCTPlatform.mm), so an iPad app on a Mac answers as a touchscreen.
    • react-native-windows' isTV is always false (Platform.windows.js), so an Xbox answers fine.

YevheniiKotyrlo and others added 7 commits December 4, 2026 14:30
…decide with unknown

Media and container queries now evaluate through one walk and one comparison in
three-valued logic (MQ5 §3.1): a term the runtime cannot decide is unknown, its negation
too, and a rule applies only when its condition is true, as in Chromium, Firefox and WebKit.

- A range pair is its two comparisons, where both runtimes answered it false.
- A term is unknown when its feature is unmeasured, its value is outside the feature's
  syntax (a keyword or a type Media Queries 5 does not give it, env(), a negative
  resolution or ratio component) or its operand is unresolved, which compiles to null so
  JSON carries it. A condition the compiler's unknown terms leave no way to be true
  compiles to no rule, as does a container condition CSS Conditional 5 §5.4 discards.
- A number past the largest float compiles to the largest safe integer and a length to the
  same bound rounded, since JSON writes Infinity as null.
- A feature in a boolean context is false only for zero, none and no-preference.
- A viewport length in a condition resolves against the window.
- hover, any-hover, pointer and any-pointer answer for the primary input, as Safari and
  Chrome do on a phone; color, color-index, monochrome, grid, scripting, update,
  overflow-block, overflow-inline, scan, color-gamut, dynamic-range and their video- forms
  answer as every React Native screen does, forced-colors as none off Windows, and
  -webkit-device-pixel-ratio as resolution.
- A keyword and a media type match in any case.
- A compiled condition is readonly at every depth, and the condition compiler reads
  lightningcss's AST through DeepReadonly.
The PR answers hover for the primary input, none on a touchscreen, which
drops Tailwind's hover:, group-hover: and peer-hover: wherever a hover
does arrive on iOS or Android (an iPad trackpad, a component that calls
onHoverIn), unlike Nativewind v4 and every v5 release. Leaving hover
unanswered would be worse: under three-valued logic (hover: hover) would
be unknown, and hover: would apply nowhere.

What hover should answer is left for later, so this keeps main's
placeholder unchanged: every (hover: ...) comparison holds. any-hover and
hover's boolean context go unanswered, as no Tailwind variant writes them.
pointer and any-pointer keep answering for the primary input.
color, color-index, monochrome, grid, update, overflow-block,
overflow-inline, scan, color-gamut, dynamic-range, their video- forms and
-webkit-device-pixel-ratio go back to being unmeasured, and an
<mq-boolean> compiles as before. No Tailwind variant queries any of them, so
they are a feature of their own rather than part of evaluating a condition
in three-valued logic. scripting and forced-colors stay: Tailwind's
noscript: and forced-colors: reach them, and without an answer their not-
forms would become unknown.
Drops safeNumber: a media number, resolution or calc() operand compiles
as written again, and a length in a declaration keeps main's clamping.
safeNumber changed stock Tailwind output (rounded-full's
calc(infinity * 1px) went from 9007199254740991 to 9007199254740990) and
covers only operands past the largest float, which no Tailwind variant
writes. A viewport length lightningcss prints as an overflowing number,
such as calc(NaN * 1vw), reaches a device as null and stays unknown, so
its over-the-wire test row goes too.
Removes DeepReadonly, ReadonlyStyleDescriptor and the readonly tuples in
MediaCondition, RenderGuard and the condition walkers, with
condition-types.test.ts, which only checked them. They change no emitted
code; MediaFeatureOperand keeps the shape a condition operand has. The
container guard keeps the PR's honest type for a missing container, which
the lint the PR enables on this file requires.
A condition still takes three values, since not unknown must stay
unknown rather than become true, but undefined carries the third as
well as a string did, so the Truth type and the UNKNOWN constant go.
Since nativewind#605 returns nothing from the visitor for an untouched rule,
lightningcss no longer round-trips it through its serializer, which
wrote -0 as 0. The operand now keeps its sign. It compares as 0, and
JSON writes it as 0 for a device, so nothing renders differently.
With main's answer to every hover comparison kept, hover: and
group-hover: apply whatever the platform, so the Mac Catalyst mock
around those tests changed nothing, and the touchscreen tests repeated
them on the iOS preset. The tests go back to main's layout, keeping the
author's assertions that the colour is absent. The setup moves to the
hover patch, where the platform decides the answer.
@sammoore
sammoore force-pushed the fix/media-condition-unknown branch from 475a459 to e2c0d89 Compare December 4, 2026 20:16
@danstepanov
danstepanov merged commit cecd530 into nativewind:main Dec 4, 2026
10 of 12 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