Skip to content

Commit 94fea18

Browse files
alanjhughesfacebook-github-bot
authored andcommitted
fix: return the correct default trait collection (#38214)
Summary: Currently, when the `_currentColorScheme` is nil the default comes from `[UITraitCollection currentTraitCollection]` which provides the trait collection of the context it was called in. Generally, this will work but in some cases the context can be different and this will return the wrong value also if the `Appearance` property is set in the plist, then initially that value is returned, regardless of if you have overridden it. Seen as the `userInterfaceStyle` of the window is overridden when the value is changed, then the default should be whatever the current style of the window is and not dependent on the calling context. <table> <tr><th>Before</th><th>After</th></tr> <tr> <td> <video src="https://github.com/facebook/react-native/assets/30924086/3bd2d217-00f2-41d5-9229-05c31da2ecf5"/> </td> <td> <video src="https://github.com/facebook/react-native/assets/30924086/6ed67e4c-dd01-40ff-84fd-0d7ffdf6534c" /> </td> </tr> </table> ## Changelog: [IOS] [FIXED] - Fix the default trait collection to always return the value of the window Pull Request resolved: #38214 Test Plan: Tested on rn-tester and verified the current behaviour is unaffected and also in our own repo to make sure it addresses the problem. Video provided above Reviewed By: dmytrorykun Differential Revision: D49186069 Pulled By: cipolleschi fbshipit-source-id: f069fea8918fe17c53429564ed2a52e316ce893b
1 parent a8d2685 commit 94fea18

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

packages/react-native/React/CoreModules/RCTAppearance.mm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride)
3636
return sColorSchemeOverride;
3737
}
3838

39+
static UITraitCollection *getKeyWindowTraitCollection()
40+
{
41+
__block UITraitCollection *traitCollection = nil;
42+
RCTExecuteOnMainQueue(^{
43+
traitCollection = RCTSharedApplication().delegate.window.traitCollection;
44+
});
45+
return traitCollection;
46+
}
47+
3948
NSString *RCTColorSchemePreference(UITraitCollection *traitCollection)
4049
{
4150
static NSDictionary *appearances;
@@ -57,7 +66,7 @@ void RCTOverrideAppearancePreference(NSString *const colorSchemeOverride)
5766
return RCTAppearanceColorSchemeLight;
5867
}
5968

60-
traitCollection = traitCollection ?: [UITraitCollection currentTraitCollection];
69+
traitCollection = traitCollection ?: getKeyWindowTraitCollection();
6170
return appearances[@(traitCollection.userInterfaceStyle)] ?: RCTAppearanceColorSchemeLight;
6271

6372
// Default to light on older OS version - same behavior as Android.

0 commit comments

Comments
 (0)