mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Appearance.setColorScheme support (revisited) (#36122)
Summary: Both Android and iOS allow you to set application specific user interface style, which is useful for applications that support both light and dark mode. With the newly added `Appearance.setColorScheme`, you can natively manage the application's user interface style rather than keeping that preference in JavaScript. The benefit is that native dialogs like alert, keyboard, action sheets and more will also be affected by this change. Implemented using Android X [AppCompatDelegate.setDefaultNightMode](https://developer.android.com/reference/androidx/appcompat/app/AppCompatDelegate#setDefaultNightMode(int)) and iOS 13+ [overrideUserInterfaceStyle](https://developer.apple.com/documentation/uikit/uiview/3238086-overrideuserinterfacestyle?language=objc) ```tsx // Lets assume a given device is set to **dark** mode. Appearance.getColorScheme(); // `dark` // Set the app's user interface to `light` Appearance.setColorScheme('light'); Appearance.getColorScheme(); // `light` // Set the app's user interface to `unspecified` Appearance.setColorScheme(null); Appearance.getColorScheme() // `dark` ``` ## Changelog [GENERAL] [ADDED] - Added `setColorScheme` to `Appearance` module Pull Request resolved: https://github.com/facebook/react-native/pull/36122 Test Plan: Added a RNTester for the feature in the Appearance section. Three buttons for toggling all set of modes. Reviewed By: lunaleaps Differential Revision: D43331405 Pulled By: NickGerleman fbshipit-source-id: 3b15f1ed0626d1ad7a8266ec026e903cd3ec46aa
This commit is contained in:
committed by
Facebook GitHub Bot
parent
14ab76ac30
commit
c18566ffdb
+12
@@ -11,6 +11,7 @@ import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatDelegate;
|
||||
import com.facebook.fbreact.specs.NativeAppearanceSpec;
|
||||
import com.facebook.react.bridge.Arguments;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
@@ -65,6 +66,17 @@ public class AppearanceModule extends NativeAppearanceSpec {
|
||||
return "light";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColorScheme(String style) {
|
||||
if (style.equals("dark")) {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
|
||||
} else if (style.equals("light")) {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
|
||||
} else if (style.equals("unspecified")) {
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColorScheme() {
|
||||
// Attempt to use the Activity context first in order to get the most up to date
|
||||
|
||||
@@ -14,6 +14,7 @@ rn_android_library(
|
||||
deps = [
|
||||
react_native_dep("third-party/java/jsr-305:jsr-305"),
|
||||
react_native_dep("third-party/android/androidx:annotation"),
|
||||
react_native_dep("third-party/android/androidx:appcompat"),
|
||||
react_native_target("java/com/facebook/react/bridge:bridge"),
|
||||
react_native_target("java/com/facebook/react/common:common"),
|
||||
react_native_target("java/com/facebook/react/module/annotations:annotations"),
|
||||
|
||||
@@ -15,6 +15,7 @@ rn_android_library(
|
||||
react_native_dep("libraries/fresco/fresco-react-native:imagepipeline"),
|
||||
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
|
||||
react_native_dep("third-party/android/androidx:annotation"),
|
||||
react_native_dep("third-party/android/androidx:appcompat"),
|
||||
react_native_dep("third-party/android/androidx:core"),
|
||||
react_native_dep("third-party/android/androidx:fragment"),
|
||||
react_native_dep("third-party/android/androidx:legacy-support-core-utils"),
|
||||
|
||||
Reference in New Issue
Block a user