Skip to content

Commit

Permalink
Fix Application always in light mode on initial load. (#44335)
Browse files Browse the repository at this point in the history
Summary:
Hi, I'm Filip from software mansion.  This PR solves a problem I stumbled upon.

On iOS, applications are always in light mode on initial load. Even if the device is turned to dark mode.

### Cause of the problem:

The initial appearance is taken from `RCTKeyWindow()`, but at the time of initialization of `RCTAppearance` it does not exist yet.

### Solution:

This PR moves repeats initialization of the appearance the first time `getColorScheme()` is called if it was not initialized properly before.

## Changelog:

[IOS] [FIXED] - Fix dark mode on initial load.

Pull Request resolved: #44335

Test Plan:
- Create new React native app with `npx react-native@latest init AwesomeProjec`
- Run the application on iphone using simulator
- turn on dark mode using `cmd+shift+A`
- close application and run it again

### without changes:
  The application will turn on in light mode despite the simulator being set to dark mode.
  When you reload the application it works as expected (is in dark mode)

### with changes:
  Works as expected

#### note:
any change to device ui settings will trigger a listener that will set appearance to correct state, so testing of this problem should happen in as isolated conditions as possible.

Reviewed By: cortinico

Differential Revision: D58189058

Pulled By: cipolleschi

fbshipit-source-id: 9a864f3d045e966bc88601f661d221c4796c5c95
  • Loading branch information
filip131311 authored and facebook-github-bot committed Jun 7, 2024
1 parent b5fd041 commit b957513
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/react-native/React/CoreModules/RCTAppearance.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
NSString *const RCTAppearanceColorSchemeLight = @"light";
NSString *const RCTAppearanceColorSchemeDark = @"dark";

static BOOL sIsAppearancePreferenceSet = NO;

static BOOL sAppearancePreferenceEnabled = YES;
void RCTEnableAppearancePreference(BOOL enabled)
{
Expand Down Expand Up @@ -62,6 +64,12 @@ void RCTUseKeyWindowForSystemStyle(BOOL useMainScreen)
// Return the default if the app doesn't allow different color schemes.
return RCTAppearanceColorSchemeLight;
}

if (appearances[@(traitCollection.userInterfaceStyle)]) {
sIsAppearancePreferenceSet = YES;
return appearances[@(traitCollection.userInterfaceStyle)];
}

UIUserInterfaceStyle systemStyle = sUseKeyWindowForSystemStyle ? RCTKeyWindow().traitCollection.userInterfaceStyle
: traitCollection.userInterfaceStyle;
return appearances[@(systemStyle)] ?: RCTAppearanceColorSchemeLight;
Expand Down Expand Up @@ -116,6 +124,10 @@ - (dispatch_queue_t)methodQueue

RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSString *, getColorScheme)
{
if (!sIsAppearancePreferenceSet) {
UITraitCollection *traitCollection = RCTKeyWindow().traitCollection;
_currentColorScheme = RCTColorSchemePreference(traitCollection);
}
return _currentColorScheme;
}

Expand Down

0 comments on commit b957513

Please sign in to comment.