Skip to content

Commit

Permalink
Appearance: Migrate to ESM Named Exports
Browse files Browse the repository at this point in the history
Summary:
Straightforward migration of the `Appearance` module to use ESM named exports.

Changelog:
[Internal]

Reviewed By: TheSavior

Differential Revision: D61567882
  • Loading branch information
yungsters authored and facebook-github-bot committed Aug 20, 2024
1 parent 2bbe8f4 commit 6953a34
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 61 deletions.
94 changes: 46 additions & 48 deletions packages/react-native/Libraries/Utilities/Appearance.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,57 +51,55 @@ if (NativeAppearance) {
);
}

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';
}
/**
* 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.
*/
export function 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;
},
// 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;
export function 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?",
);
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);
}
},
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);
},
};
/**
* Add an event handler that is fired when appearance preferences change.
*/
export function addChangeListener(
listener: AppearanceListener,
): EventSubscription {
return eventEmitter.addListener('change', listener);
}
6 changes: 2 additions & 4 deletions packages/react-native/Libraries/Utilities/DevLoadingView.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

import processColor from '../StyleSheet/processColor';
import Appearance from './Appearance';
import {getColorScheme} from './Appearance';
import NativeDevLoadingView from './NativeDevLoadingView';

const COLOR_SCHEME = {
Expand Down Expand Up @@ -39,9 +39,7 @@ module.exports = {
showMessage(message: string, type: 'load' | 'refresh') {
if (NativeDevLoadingView) {
const colorScheme =
Appearance.getColorScheme() === 'dark'
? COLOR_SCHEME.dark
: COLOR_SCHEME.default;
getColorScheme() === 'dark' ? COLOR_SCHEME.dark : COLOR_SCHEME.default;

const colorSet = colorScheme[type];

Expand Down
6 changes: 3 additions & 3 deletions packages/react-native/Libraries/Utilities/useColorScheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@

import type {ColorSchemeName} from './NativeAppearance';

import Appearance from './Appearance';
import {addChangeListener, getColorScheme} from './Appearance';
import {useSyncExternalStore} from 'react';

const subscribe = (onStoreChange: () => void) => {
const appearanceSubscription = Appearance.addChangeListener(onStoreChange);
const appearanceSubscription = addChangeListener(onStoreChange);
return () => appearanceSubscription.remove();
};

export default function useColorScheme(): ?ColorSchemeName {
return useSyncExternalStore(subscribe, Appearance.getColorScheme);
return useSyncExternalStore(subscribe, getColorScheme);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9162,11 +9162,11 @@ declare export default typeof UTFSequence;

exports[`public API should not change unintentionally Libraries/Utilities/Appearance.js 1`] = `
"type AppearanceListener = (preferences: AppearancePreferences) => void;
declare module.exports: {
getColorScheme(): ?ColorSchemeName,
setColorScheme(colorScheme: ?ColorSchemeName): void,
addChangeListener(listener: AppearanceListener): EventSubscription,
};
declare export function getColorScheme(): ?ColorSchemeName;
declare export function setColorScheme(colorScheme: ?ColorSchemeName): void;
declare export function addChangeListener(
listener: AppearanceListener
): EventSubscription;
"
`;

Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ import typeof StyleSheet from './Libraries/StyleSheet/StyleSheet';
import typeof Text from './Libraries/Text/Text';
import typeof * as TurboModuleRegistry from './Libraries/TurboModule/TurboModuleRegistry';
import typeof UTFSequence from './Libraries/UTFSequence';
import typeof Appearance from './Libraries/Utilities/Appearance';
import typeof * as Appearance from './Libraries/Utilities/Appearance';
import typeof BackHandler from './Libraries/Utilities/BackHandler';
import typeof DeviceInfo from './Libraries/Utilities/DeviceInfo';
import typeof DevSettings from './Libraries/Utilities/DevSettings';
Expand Down

0 comments on commit 6953a34

Please sign in to comment.