From 25d94ff34d86ca3a963021267b7aebe509eff6fb Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Wed, 23 Oct 2019 15:49:43 -0700 Subject: [PATCH] iOS Allow disabling color scheme preference Summary: Some apps may need to disable the automatic dark mode color scheme assignment. This provides such kill switch. Changelog: [Internal] Reviewed By: yungsters Differential Revision: D18088313 fbshipit-source-id: 75abf34e92f8a4a637daa26135adf85965cb9df5 --- React/CoreModules/RCTAppearance.h | 2 ++ React/CoreModules/RCTAppearance.mm | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/React/CoreModules/RCTAppearance.h b/React/CoreModules/RCTAppearance.h index ece36942b7b..2644783e5d6 100644 --- a/React/CoreModules/RCTAppearance.h +++ b/React/CoreModules/RCTAppearance.h @@ -10,6 +10,8 @@ #import #import +RCT_EXTERN void RCTEnableAppearancePreference(BOOL enabled); + NSString *const RCTUserInterfaceStyleDidChangeNotification = @"RCTUserInterfaceStyleDidChangeNotification"; @interface RCTAppearance : RCTEventEmitter diff --git a/React/CoreModules/RCTAppearance.mm b/React/CoreModules/RCTAppearance.mm index 300a102068f..ad4233c6f6b 100644 --- a/React/CoreModules/RCTAppearance.mm +++ b/React/CoreModules/RCTAppearance.mm @@ -17,6 +17,11 @@ using namespace facebook::react; NSString *const RCTAppearanceColorSchemeLight = @"light"; NSString *const RCTAppearanceColorSchemeDark = @"dark"; +static BOOL sAppearancePreferenceEnabled = YES; +void RCTEnableAppearancePreference(BOOL enabled) { + sAppearancePreferenceEnabled = enabled; +} + static NSString *RCTColorSchemePreference(UITraitCollection *traitCollection) { #if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 @@ -31,6 +36,11 @@ static NSString *RCTColorSchemePreference(UITraitCollection *traitCollection) }; }); + if (!sAppearancePreferenceEnabled) { + // Return the default if the app doesn't allow different color schemes. + return RCTAppearanceColorSchemeLight; + } + traitCollection = traitCollection ?: [UITraitCollection currentTraitCollection]; return appearances[@(traitCollection.userInterfaceStyle)] ?: RCTAppearanceColorSchemeLight; }