Skip to content

Add conditional style values and update them when the color scheme changes - #60124

Open
intergalacticspacehighway wants to merge 1 commit into
mainfrom
media-queries-1-core
Open

intergalacticspacehighway wants to merge 1 commit into
mainfrom
media-queries-1-core

Conversation

@intergalacticspacehighway

@intergalacticspacehighway intergalacticspacehighway commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary:

This is PR 1 of 3 for native media queries (RFC). Stack: this PR → #60125 (platforms) → #60126 (types and example).

Currently, a style that depends on the color scheme needs useColorScheme(). A scheme change then renders React again, often for large parts of the app. With this PR, a style value can carry its own conditions declaratively, and a native commit applies them without a React render:

const styles = StyleSheet.create({
  card: {
    backgroundColor: {
      default: 'white',
      '@media (prefers-color-scheme: dark)': 'black',
    },
  },
});

The last matching condition wins, otherwise default is used. Only prefers-color-scheme is supported. The design leaves room for width/height later.

How it works

  1. JS. ReactNativeAttributePayload sends a conditional value of a core style prop as {$conditionalValue: {default, conditions}} and adds $hasConditionalValues: true to the payload. Other props are not changed.
  2. Resolve. ConcreteComponentDescriptor::cloneProps calls resolveConditionalValues before it parses the props. Every parse path (classic, iterator setter, Android rawProps) gets plain values. The props keep a small record: the conditions, the media features they read, and the features they were resolved for (resolvedFor).
  3. Update. UIManager::setMediaFeatures sets a MediaFeaturesStore (one per instance, in the ContextContainer) and makes an empty commit on each surface. In every commit, ShadowTree::tryCommit finds views whose record is outdated and sends their stored conditions through cloneProps again. This runs before the commit hooks and layout. It also fixes older nodes that React commits again after a change.
  4. Optimizations. A shadow node trait (ChildrenHaveConditionalValues) limits the walk to subtrees that have conditional values. A view is cloned only if a feature it reads changed.

Cost (measured, Release build on a Mac):

  • Apps without conditional values: 1 hasProperty check per cloneProps (about 0.05 µs), and a check in each commit. A whole commit of 1000 views without conditional values, with this check, took about 0.35 µs.
  • Per commit: about +5 µs per 1000 conditional views.
  • A payload with a conditional value: about +3.5 µs to create the view, +2 µs for an update.

Changelog:

[GENERAL] [ADDED] - Style values can depend on @media (prefers-color-scheme).

Test Plan:

  • To run the example end to end: gh pr checkout 60126 (the PR 3 branch includes all 3 PRs), build RNTester, and open Media Queries. The screenshots are in Set the media features from the iOS and Android color scheme #60125.
  • New tests: C++ ConditionalValuesTest, MediaFeaturesTest and UIManagerMediaFeaturesTest; Fantom ConditionalValues-itest.js, ConditionalValuesReferences-itest.js and ReactNativeAttributePayload-itest.js.
  • ./gradlew :private:react-native-fantom:testReactCommon, yarn fantom, yarn test, yarn flow-check, ESLint, Prettier and clang-format: pass.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 18, 2026
@intergalacticspacehighway intergalacticspacehighway added the p: Expo Partner: Expo label Nov 19, 2026
@intergalacticspacehighway
intergalacticspacehighway marked this pull request as ready for review November 19, 2026 05:18
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 19, 2026
meta-codesync Bot pushed a commit that referenced this pull request Nov 19, 2026
## Summary:

On Android, `TextInput` keeps `fontWeight` as a string. So `TextInput.js` changes a numeric `fontWeight` (for example `700`) to a string before it sends the props. The commit that added this ([15f27bc](15f27bc299a)) calls it "a quick and dirty replication" of the `Text.js` logic, to fix later for Fabric.

This PR makes the native code accept a number too:
- **C++** (`AndroidTextInputProps`): a number becomes a string, in the classic parser and in the iterator setter.
- **Kotlin** (`ReactTextInputManager.setFontWeight`): the setter takes a `Dynamic`, so the raw props path also accepts a number. This changes the public signature from `String?` to `Dynamic`.

The JS conversion stays, so a new JS bundle still works with an older native build. A follow-up PR removes it after this change ships.

This also makes a numeric `fontWeight` work in a conditional style value ([RFC](react-native-community/discussions-and-proposals#1010), #60124), because the JS conversion cannot see inside one.

## Changelog:

[ANDROID] [CHANGED] - `TextInput` accepts a numeric `fontWeight` in native code.

## Test Plan:

- New Kotlin test: `ReactTextInputPropertyTest.testFontWeightAcceptsANumber`.
- RNTester on the Android emulator, with the JS conversion removed locally so the number reaches native: a `TextInput` with a numeric `fontWeight` renders with no errors in each combination of the props flags (Props 1.5, Props 2.0, exclusive props updates, the C++ iterator setter).
- `./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests ReactTextInputPropertyTest`: pass.

Differential Revision: D133209822

Pull Request resolved: #60136

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

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Expo Partner: Expo Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant