Skip to content

fix: Support media query range syntax on native - #532

Open
0z5a wants to merge 1 commit into
react:mainfrom
0z5a:native-mq-range-syntax
Open

0z5a wants to merge 1 commit into
react:mainfrom
0z5a:native-mq-range-syntax

Conversation

@0z5a

@0z5a 0z5a commented Nov 18, 2026

Copy link
Copy Markdown

Fixes #531.

Summary

The native media query parser (src/native/modules/mediaQuery.js, from css-mediaquery) only understands (feature: value). A range query like '@media (width >= 600px)' hits the SyntaxError in parseQuery, and since resolveStyle does not catch it, the component throws during render. On web the same key works, because the StyleX compiler turns it into min-width/max-width.

This change keeps the existing parsing path and only falls back to a new range regex when RE_MQ_EXPRESSION does not match. The range forms become the expressions matchQuery already knows:

  • (width >= 600px) / (600px <= width) → min
  • (width <= 600px) / (600px >= width) → max
  • (width > 600px) / (width < 600px) → new gt / lt modifiers, compared with > / <
  • (width = 600px) → equality
  • (400px <= width < 800px) → two expressions on the same feature

These still throw: a min-/max- prefix combined with an operator ((min-width >= 600px)), intervals whose operators point in opposite directions ((400px < width > 800px)), and a missing feature or value.

One small difference from web: StyleX writes (width > 600px) as min-width: 600.01px, while native compares exactly (width > 600). Both agree for whole-number dimensions.

How I found it

I was reading the native style resolution path (resolveStyle → mediaQueryMatches → mediaQuery.match) and noticed that RE_MQ_EXPRESSION requires feature followed by an optional : value. Range syntax has no colon, so it cannot match. I compiled '@media (width >= 600px)' with the StyleX babel plugin from the lockfile (0.18.3) to confirm web accepts it, then rendered the same style with the native jest project, which threw Invalid CSS media query: "(width >= 600px)".

Where this can show up in practice

This is my estimate; I have not seen a field report.

  • Teams sharing styles between web and native that use range syntax, which is the newer form and the one some CSS tooling and docs now prefer. The style works in the browser and the screen crashes on iOS/Android.
  • Shared breakpoint constants written as (width >= …) for web. Any native component that uses them fails to render, not only the one rule.
  • Libraries built on react-strict-dom that adopt range syntax would break their native consumers.

Test

  • Added parse and match tests in src/native/modules/__tests__/mediaQuery-test.js: each operator at the boundary (e.g. width exactly 600 for >= vs >), the value-first form, intervals, em units, aspect-ratio, not screen and (...), and the invalid range forms that should still throw. The existing tests are unchanged.
  • Added a render-level range syntax block to tests/css/css-create-queries-test.native.js.
  • Before the change, 7 of the new tests fail, including all three render tests with the SyntaxError. After it, npm test passes on macOS (node 24): flow reports no errors, eslint is clean, and jest is 907 passed with 2 skipped, with the coverage thresholds met. prettier --check is clean on the changed files.
  • Not run: the perf/size benchmarks, the platform-tests app on a simulator or device, Linux, and CI.

The native media query parser only understood the `(feature: value)` form,
so range queries such as `(width >= 600px)`, `(600px <= width)` or
`(400px < width < 800px)` threw "Invalid CSS media query" during render,
while StyleX compiles them on web.

Parse the range forms in `parseQuery` and turn them into the existing
min/max expressions. `<` and `>` use new exclusive `lt`/`gt` modifiers.

Refs react#531
@meta-cla meta-cla Bot added the cla signed label Nov 18, 2026

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Media queries in range syntax, e.g. (width >= 600px), throw "Invalid CSS media query" on native

1 participant