Repository navigation
Set the media features from the iOS and Android color scheme - #60125
intergalacticspacehighway wants to merge 2 commits into
Conversation
d69aa7d to
075557d
Compare
7941f82 to
549754e
Compare
549754e to
dc0086b
Compare
dc0086b to
4aeeed9
Compare
4aeeed9 to
a507adf
Compare
| fabricUIManager, | ||
| eventBeatManager, | ||
| componentFactory, | ||
| UiModeUtils.isDarkMode(context.currentActivity ?: context), |
There was a problem hiding this comment.
I think context.currentActivity is still null here when the app starts, because the activity is attached to the context a bit later. If so, this reads the app's color scheme, not the activity's one.
These can differ when the app forces a theme with AppCompatDelegate.setDefaultNightMode() (for example an in-app "dark mode" setting). useColorScheme() reads the activity first, so it could say dark while conditional styles show light, until the next configuration change.
I haven't tried it on a device. Maybe we could send the color scheme again once the activity is available (for example in FabricUIManager.onHostResume)?
There was a problem hiding this comment.
Good catch, confirmed it on the emulator. Fixed in 94fbf0c. The start value now also reads the app's night mode (AppCompatDelegate.getDefaultNightMode()), and AppearanceModule sends the color scheme again on resume, so it matches useColorScheme().
| // Register as a listener for color scheme changes if override is provided | ||
| overrideColorScheme?.addSchemeChangeListener(schemeChangeListener) | ||
| // The renderer starts from `uiMode`, so send the override. | ||
| overrideColorScheme?.let { updateMediaFeatures(it.getScheme()) } |
There was a problem hiding this comment.
AppearanceModule is only created when something uses it (lazy).
So if an app stops using Appearance / useColorScheme() (which this feature allows), this line may not run until the first configuration change, and the override would be ignored until then.
There was a problem hiding this comment.
Right, confirmed it with an app that doesn't use Appearance. A forced theme now works without the module, since the start value reads the night mode via AppCompatDelegate.getDefaultNightMode(). Fixed in 94fbf0c.
Summary:
This is PR 2 of 3 (RFC). Stack: #60124 (core) → this PR → #60126 (types and example).
This PR connects the platform color scheme to the media features from PR 1. On both platforms, the first value is set before the first surface starts, so the first render already uses it. Each later change calls
setMediaFeatures, which commits each surface. Conditional styles use the same color scheme asuseColorScheme(), including the app's overrides.RCTSurfacePresentersends the color scheme to Fabric (-[RCTScheduler setMediaFeatures:]): once at start, then on eachRCTUserInterfaceStyleDidChangeNotification.RCTColorSchemePreference, the same function asRCTAppearance, so the app's appearance settings (such asRCTOverrideAppearancePreference) apply.RCTColorSchemePreferencemoves unchanged from React-CoreModules to React-Core (React/Modules/RCTAppearancePreference), so Fabric needs no new pod dependency. This is the same split asRCTI18nUtil.RCTAppearance.himports the new header, so existing imports still work.uiMode.AppearanceModulesends each new color scheme, including the app'sOverrideColorScheme, to the newFabricUIManager.setMediaFeatures(@UnstableReactNativeAPI).Appearance.setColorScheme()works on both platforms, because it changes the window style (iOS) or the activity configuration (Android).Changelog:
[IOS] [ADDED] - Conditional style values follow the color scheme.
[ANDROID] [ADDED] - Conditional style values follow the color scheme.
Test Plan:
gh pr checkout 60126(includes all 3 PRs), build RNTester on Android and iOS, and open Media Queries.Appearance.setColorScheme(), and native overrides (OverrideColorScheme,RCTOverrideAppearancePreference) with no React render, and matchuseColorScheme().