Skip to content

Commit 91702e5

Browse files
huntiemeta-codesync[bot]
authored andcommitted
Add 'auto' to ColorSchemeOverride, deprecate 'unspecified'
Summary: NOTE: 👋🏻 **This is an RFC**, additional and separate to the previous 2 diffs. Looking for feedback :) Proposes an API tweak to `Appearance.setColorScheme()` to make it more idiomatic/understandable. - Aligns with the CSS `color-scheme` property vocabulary, where `auto` means "defer to the system preference". - Replaces the ambiguous `unspecified` (now deprecated), which gave no indication of the resulting behaviour. See also: - History of this API + return type narrowing in D102527387. - Extended docs + diagram in react/react-native-website#5060. **Alternative names considered** - `'reset'` - Implies reversing a change, not deferring to system - `'inherit'` - Weak — CSS inherit is element→parent, not app→OS Changelog: [General][Deprecated] - `Appearance.setColorScheme('unspecified')` is deprecated, use `'auto'` instead. Reviewed By: cipolleschi Differential Revision: D103841988 fbshipit-source-id: 474d0925ce9ee0ab79f87d0ff9ae86ae56ceb0aa
1 parent ef6463c commit 91702e5

7 files changed

Lines changed: 19 additions & 16 deletions

File tree

packages/react-native/Libraries/Utilities/Appearance.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import {NativeEventSubscription} from '../EventEmitter/RCTNativeAppEventEmitter'
1111

1212
type ColorSchemeName = 'light' | 'dark';
1313

14-
type ColorSchemeOverride = ColorSchemeName | 'unspecified';
14+
type ColorSchemeOverride =
15+
| ColorSchemeName
16+
| 'auto'
17+
/** @deprecated Use 'auto' instead */
18+
| 'unspecified';
1519

1620
export namespace Appearance {
1721
type AppearancePreferences = {
@@ -36,8 +40,8 @@ export namespace Appearance {
3640

3741
/**
3842
* Force the application to always adopt a light or dark interface style. Pass
39-
* `'unspecified'` to reset and follow the system default (removes any
40-
* override). This does not affect the system UI, only the application.
43+
* `'auto'` to reset and follow the system default (removes any override).
44+
* This does not affect the system UI, only the application.
4145
*/
4246
export function setColorScheme(scheme: ColorSchemeOverride): void;
4347

packages/react-native/Libraries/Utilities/Appearance.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ export function getColorScheme(): ColorSchemeName | null {
107107

108108
/**
109109
* Force the application to always adopt a light or dark interface style. Pass
110-
* `'unspecified'` to reset and follow the system default (removes any
111-
* override). This does not affect the system UI, only the application.
110+
* `'auto'` to reset and follow the system default (removes any override).
111+
* This does not affect the system UI, only the application.
112112
*/
113113
export function setColorScheme(colorScheme: ColorSchemeOverride): void {
114114
const state = getState();
@@ -117,7 +117,7 @@ export function setColorScheme(colorScheme: ColorSchemeOverride): void {
117117
NativeAppearance.setColorScheme(colorScheme);
118118
state.appearance = {
119119
colorScheme:
120-
colorScheme === 'unspecified'
120+
colorScheme === 'auto' || colorScheme === 'unspecified'
121121
? NativeAppearance.getColorScheme()
122122
: colorScheme,
123123
};

packages/react-native/React/Base/RCTConvert.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ + (UIKeyboardType)UIKeyboardType:(id)json RCT_DYNAMIC
494494
RCT_ENUM_CONVERTER(
495495
UIUserInterfaceStyle,
496496
(@{
497+
@"auto" : @(UIUserInterfaceStyleUnspecified),
497498
@"unspecified" : @(UIUserInterfaceStyleUnspecified),
498499
@"light" : @(UIUserInterfaceStyleLight),
499500
@"dark" : @(UIUserInterfaceStyleDark),

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/appearance/AppearanceModule.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ constructor(
8787
when (style) {
8888
"dark" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
8989
"light" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
90+
"auto",
9091
"unspecified" ->
9192
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
9293
}

packages/react-native/ReactNativeApi.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<04955f1605996352fa3f3a3fc86ba937>>
7+
* @generated SignedSource<<eeffa8d1c270f98debd70024251b4a1a>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -1859,7 +1859,7 @@ declare namespace CodegenTypes {
18591859
}
18601860
declare type ColorListenerCallback = (value: ColorValue) => unknown
18611861
declare type ColorSchemeName = "dark" | "light"
1862-
declare type ColorSchemeOverride = "dark" | "light" | "unspecified"
1862+
declare type ColorSchemeOverride = "auto" | "dark" | "light" | "unspecified"
18631863
declare type ColorValue = ____ColorValue_Internal
18641864
declare type ComponentProvider = () => React.ComponentType<any>
18651865
declare type ComponentProviderInstrumentationHook = (
@@ -6073,7 +6073,7 @@ export {
60736073
AppState, // 12012be5
60746074
AppStateEvent, // 80f034c3
60756075
AppStateStatus, // 447e5ef2
6076-
Appearance, // 0e9bf4fe
6076+
Appearance, // 83e9641a
60776077
AutoCapitalize, // c0e857a0
60786078
BackHandler, // f139fc69
60796079
BackPressEventName, // 4620fb76

packages/react-native/src/private/specs_DEPRECATED/modules/NativeAppearance.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as TurboModuleRegistry from '../../../../Libraries/TurboModule/TurboMod
1414

1515
export type ColorSchemeName = 'light' | 'dark';
1616

17-
export type ColorSchemeOverride = 'light' | 'dark' | 'unspecified';
17+
export type ColorSchemeOverride = 'light' | 'dark' | 'auto' | 'unspecified';
1818

1919
export type AppearancePreferences = {
2020
colorScheme: ColorSchemeName,

packages/rn-tester/js/examples/Appearance/AppearanceExample.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ const ColorShowcase = (props: {themeName: string}) => (
136136

137137
const ToggleNativeAppearance = () => {
138138
const [nativeColorScheme, setNativeColorScheme] = useState<
139-
ColorSchemeName | 'unspecified',
140-
>('unspecified');
139+
ColorSchemeName | 'auto',
140+
>('auto');
141141
const colorScheme = useColorScheme();
142142

143143
useEffect(() => {
@@ -156,10 +156,7 @@ const ToggleNativeAppearance = () => {
156156
title="Set to dark"
157157
onPress={() => setNativeColorScheme('dark')}
158158
/>
159-
<Button
160-
title="Unset"
161-
onPress={() => setNativeColorScheme('unspecified')}
162-
/>
159+
<Button title="Auto" onPress={() => setNativeColorScheme('auto')} />
163160
</View>
164161
);
165162
};

0 commit comments

Comments
 (0)