Skip to content

Commit 900aac6

Browse files
huntiefacebook-github-bot
authored andcommitted
Align nullable types across Appearance API
Summary: Narrow Flow types to match the hand-written `Appearance.d.ts`, which already uses `ColorSchemeName | null`. Also narrows the `.d.ts` signatures where they were previously too wide (`| undefined`). At the native spec level, nullability is removed entirely — the module always returns a valid color scheme, and the module-level `?Spec` already covers the "module absent" case. Fixes downstream callers (AppearanceExample, AMAAppearance) to match the narrowed listener type. Changelog: [Internal] - This is a net fix/extension of D102527387 Differential Revision: D103865622
1 parent 5405d34 commit 900aac6

6 files changed

Lines changed: 23 additions & 19 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
@@ -15,7 +15,7 @@ type ColorSchemeOverride = ColorSchemeName | 'unspecified';
1515

1616
export namespace Appearance {
1717
type AppearancePreferences = {
18-
colorScheme: ColorSchemeName;
18+
colorScheme: ColorSchemeName | null;
1919
};
2020

2121
type AppearanceListener = (preferences: AppearancePreferences) => void;
@@ -32,7 +32,7 @@ export namespace Appearance {
3232
* - `null` will only be returned if the native Appearance module is
3333
* unavailable (out of tree platforms).
3434
*/
35-
export function getColorScheme(): ColorSchemeName | null | undefined;
35+
export function getColorScheme(): ColorSchemeName | null;
3636

3737
/**
3838
* Force the application to always adopt a light or dark interface style. Pass
@@ -54,5 +54,9 @@ export namespace Appearance {
5454
/**
5555
* Returns the active color scheme (`'light'` or `'dark'`). Automatically
5656
* re-renders the component when the color scheme changes.
57+
*
58+
* Notes:
59+
* - `null` will only be returned if the native Appearance module is unavailable
60+
* (out of tree platforms).
5761
*/
58-
export function useColorScheme(): ColorSchemeName;
62+
export function useColorScheme(): ColorSchemeName | null;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ import EventEmitter from '../vendor/emitter/EventEmitter';
2222
export type {ColorSchemeName, ColorSchemeOverride};
2323

2424
export type AppearancePreferences = {
25-
colorScheme: ?ColorSchemeName,
25+
colorScheme: ColorSchemeName | null,
2626
};
2727

2828
type Appearance = {
29-
colorScheme: ?ColorSchemeName,
29+
colorScheme: ColorSchemeName | null,
3030
};
3131

3232
let lazyState: ?{
@@ -88,7 +88,7 @@ function getState(): NonNullable<typeof lazyState> {
8888
* - `null` will only be returned if the native Appearance module is unavailable
8989
* (out of tree platforms).
9090
*/
91-
export function getColorScheme(): ?ColorSchemeName {
91+
export function getColorScheme(): ColorSchemeName | null {
9292
let colorScheme = null;
9393
const state = getState();
9494
const {NativeAppearance} = state;
@@ -118,7 +118,7 @@ export function setColorScheme(colorScheme: ColorSchemeOverride): void {
118118
state.appearance = {
119119
colorScheme:
120120
colorScheme === 'unspecified'
121-
? (NativeAppearance.getColorScheme() ?? null)
121+
? NativeAppearance.getColorScheme()
122122
: colorScheme,
123123
};
124124
}
@@ -130,7 +130,7 @@ export function setColorScheme(colorScheme: ColorSchemeOverride): void {
130130
* or a call to `setColorScheme()`.
131131
*/
132132
export function addChangeListener(
133-
listener: ({colorScheme: ?ColorSchemeName}) => void,
133+
listener: (preferences: AppearancePreferences) => void,
134134
): EventSubscription {
135135
const {eventEmitter} = getState();
136136
return eventEmitter.addListener('change', listener);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ const subscribe = (onStoreChange: () => void) => {
2828
* - `null` will only be returned if the native Appearance module is unavailable
2929
* (out of tree platforms).
3030
*/
31-
export default function useColorScheme(): ?ColorSchemeName {
31+
export default function useColorScheme(): ColorSchemeName | null {
3232
return useSyncExternalStore(subscribe, getColorScheme);
3333
}

packages/react-native/ReactNativeApi.d.ts

Lines changed: 7 additions & 7 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<<7a366632c3ea4b05d3a7d8bc0f09e961>>
7+
* @generated SignedSource<<04955f1605996352fa3f3a3fc86ba937>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -1218,7 +1218,7 @@ declare type ActivityIndicatorProps = Readonly<
12181218
>
12191219
declare type add = typeof add
12201220
declare function addChangeListener(
1221-
listener: ($$PARAM_0$$: { colorScheme: ColorSchemeName | undefined }) => void,
1221+
listener: (preferences: AppearancePreferences) => void,
12221222
): EventSubscription
12231223
declare class Alert {
12241224
static alert(
@@ -1635,7 +1635,7 @@ declare namespace Appearance {
16351635
}
16361636
}
16371637
declare type AppearancePreferences = {
1638-
colorScheme: ColorSchemeName | undefined
1638+
colorScheme: ColorSchemeName | null
16391639
}
16401640
declare type AppParameters = {
16411641
initialProps: {
@@ -2468,7 +2468,7 @@ declare function get_2<T extends TurboModule>(
24682468
name: string,
24692469
): null | T | undefined
24702470
declare function getAppKeys(): ReadonlyArray<string>
2471-
declare function getColorScheme(): ColorSchemeName | null | undefined
2471+
declare function getColorScheme(): ColorSchemeName | null
24722472
declare function getEnforcing<T extends TurboModule>(name: string): T
24732473
declare function getRegistry(): Registry
24742474
declare function getRunnable(appKey: string): null | Runnable | undefined
@@ -5738,7 +5738,7 @@ declare function useAnimatedValueXY(
57385738
},
57395739
config?: Animated.AnimatedConfig | null | undefined,
57405740
): Animated.ValueXY
5741-
declare function useColorScheme(): ColorSchemeName | null | undefined
5741+
declare function useColorScheme(): ColorSchemeName | null
57425742
declare function usePressability(
57435743
config: null | PressabilityConfig | undefined,
57445744
): null | PressabilityEventHandlers
@@ -6073,7 +6073,7 @@ export {
60736073
AppState, // 12012be5
60746074
AppStateEvent, // 80f034c3
60756075
AppStateStatus, // 447e5ef2
6076-
Appearance, // c1c1ac70
6076+
Appearance, // 0e9bf4fe
60776077
AutoCapitalize, // c0e857a0
60786078
BackHandler, // f139fc69
60796079
BackPressEventName, // 4620fb76
@@ -6339,7 +6339,7 @@ export {
63396339
useAnimatedColor, // e3511f81
63406340
useAnimatedValue, // b18adb63
63416341
useAnimatedValueXY, // c7ee2332
6342-
useColorScheme, // 29a517d5
6342+
useColorScheme, // d585efdb
63436343
usePressability, // b4e21b46
63446344
useWindowDimensions, // bb4b683f
63456345
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export type ColorSchemeName = 'light' | 'dark';
1717
export type ColorSchemeOverride = 'light' | 'dark' | 'unspecified';
1818

1919
export type AppearancePreferences = {
20-
colorScheme?: ?ColorSchemeName,
20+
colorScheme: ColorSchemeName,
2121
};
2222

2323
export interface Spec extends TurboModule {
24-
+getColorScheme: () => ?ColorSchemeName;
24+
+getColorScheme: () => ColorSchemeName;
2525
+setColorScheme: (colorScheme: ColorSchemeOverride) => void;
2626

2727
// RCTEventEmitter

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function ColorSchemeSubscription() {
2424

2525
useEffect(() => {
2626
const subscription = Appearance.addChangeListener(
27-
({colorScheme: newColorScheme}: {colorScheme: ?ColorSchemeName}) => {
27+
({colorScheme: newColorScheme}) => {
2828
setColorScheme(newColorScheme);
2929
},
3030
);

0 commit comments

Comments
 (0)