mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
c18566ffdb
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
108 lines
3.5 KiB
JavaScript
108 lines
3.5 KiB
JavaScript
/**
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @flow strict-local
|
|
*/
|
|
|
|
import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
|
|
import Platform from '../Utilities/Platform';
|
|
import EventEmitter, {
|
|
type EventSubscription,
|
|
} from '../vendor/emitter/EventEmitter';
|
|
import {isAsyncDebugging} from './DebugEnvironment';
|
|
import NativeAppearance, {
|
|
type AppearancePreferences,
|
|
type ColorSchemeName,
|
|
} from './NativeAppearance';
|
|
import invariant from 'invariant';
|
|
|
|
type AppearanceListener = (preferences: AppearancePreferences) => void;
|
|
const eventEmitter = new EventEmitter<{
|
|
change: [AppearancePreferences],
|
|
}>();
|
|
|
|
type NativeAppearanceEventDefinitions = {
|
|
appearanceChanged: [AppearancePreferences],
|
|
};
|
|
|
|
if (NativeAppearance) {
|
|
const nativeEventEmitter =
|
|
new NativeEventEmitter<NativeAppearanceEventDefinitions>(
|
|
// T88715063: NativeEventEmitter only used this parameter on iOS. Now it uses it on all platforms, so this code was modified automatically to preserve its behavior
|
|
// If you want to use the native module on other platforms, please remove this condition and test its behavior
|
|
Platform.OS !== 'ios' ? null : NativeAppearance,
|
|
);
|
|
nativeEventEmitter.addListener(
|
|
'appearanceChanged',
|
|
(newAppearance: AppearancePreferences) => {
|
|
const {colorScheme} = newAppearance;
|
|
invariant(
|
|
colorScheme === 'dark' ||
|
|
colorScheme === 'light' ||
|
|
colorScheme == null,
|
|
"Unrecognized color scheme. Did you mean 'dark' or 'light'?",
|
|
);
|
|
eventEmitter.emit('change', {colorScheme});
|
|
},
|
|
);
|
|
}
|
|
|
|
module.exports = {
|
|
/**
|
|
* Note: Although color scheme is available immediately, it may change at any
|
|
* time. Any rendering logic or styles that depend on this should try to call
|
|
* this function on every render, rather than caching the value (for example,
|
|
* using inline styles rather than setting a value in a `StyleSheet`).
|
|
*
|
|
* Example: `const colorScheme = Appearance.getColorScheme();`
|
|
*
|
|
* @returns {?ColorSchemeName} Value for the color scheme preference.
|
|
*/
|
|
getColorScheme(): ?ColorSchemeName {
|
|
if (__DEV__) {
|
|
if (isAsyncDebugging) {
|
|
// Hard code light theme when using the async debugger as
|
|
// sync calls aren't supported
|
|
return 'light';
|
|
}
|
|
}
|
|
|
|
// TODO: (hramos) T52919652 Use ?ColorSchemeName once codegen supports union
|
|
const nativeColorScheme: ?string =
|
|
NativeAppearance == null
|
|
? null
|
|
: NativeAppearance.getColorScheme() || null;
|
|
invariant(
|
|
nativeColorScheme === 'dark' ||
|
|
nativeColorScheme === 'light' ||
|
|
nativeColorScheme == null,
|
|
"Unrecognized color scheme. Did you mean 'dark' or 'light'?",
|
|
);
|
|
return nativeColorScheme;
|
|
},
|
|
|
|
setColorScheme(colorScheme: ?ColorSchemeName): void {
|
|
const nativeColorScheme = colorScheme == null ? 'unspecified' : colorScheme;
|
|
|
|
invariant(
|
|
colorScheme === 'dark' || colorScheme === 'light' || colorScheme == null,
|
|
"Unrecognized color scheme. Did you mean 'dark', 'light' or null?",
|
|
);
|
|
|
|
if (NativeAppearance != null && NativeAppearance.setColorScheme != null) {
|
|
NativeAppearance.setColorScheme(nativeColorScheme);
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Add an event handler that is fired when appearance preferences change.
|
|
*/
|
|
addChangeListener(listener: AppearanceListener): EventSubscription {
|
|
return eventEmitter.addListener('change', listener);
|
|
},
|
|
};
|