Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/react-native/Libraries/Utilities/Appearance.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ function getState(): NonNullable<typeof lazyState> {
new NativeEventEmitter<{
appearanceChanged: [NativeAppearancePreferences],
}>(NativeAppearance).addListener('appearanceChanged', newAppearance => {
if (state.appearance?.colorScheme === newAppearance.colorScheme) {
return;
}
state.appearance = {
colorScheme: newAppearance.colorScheme,
};
Expand Down Expand Up @@ -114,13 +117,17 @@ export function setColorScheme(colorScheme: ColorSchemeOverride): void {
const state = getState();
const {NativeAppearance} = state;
if (NativeAppearance != null) {
const prevColorScheme = getColorScheme();
NativeAppearance.setColorScheme(colorScheme);
state.appearance = {
colorScheme:
colorScheme === 'auto' || colorScheme === 'unspecified'
? NativeAppearance.getColorScheme()
: colorScheme,
};
if (state.appearance.colorScheme !== prevColorScheme) {
state.eventEmitter.emit('change', state.appearance);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* 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.
*
* @flow strict-local
* @format
*/

import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';

import View from '../../Components/View/View';
import * as Appearance from '../Appearance';
import useColorScheme from '../useColorScheme';
import * as Fantom from '@react-native/fantom';
import * as React from 'react';
import {DeviceEventEmitter} from 'react-native';

const NativeAppearance = require('../NativeAppearance');

// Fantom has no native Appearance module, so a fake one is installed before
// `Appearance` lazily reads it.
// $FlowFixMe[cannot-write]
NativeAppearance.default = {
getColorScheme: () => 'dark',
setColorScheme: jest.fn(),
addListener: jest.fn(),
removeListeners: jest.fn(),
};

describe('Appearance', () => {
it('notifies change listeners when setColorScheme changes the scheme', () => {
expect(Appearance.getColorScheme()).toBe('dark');

const listener = jest.fn();
const subscription = Appearance.addChangeListener(listener);

Appearance.setColorScheme('light');

expect(listener).toHaveBeenCalledTimes(1);
expect(listener).toHaveBeenCalledWith({colorScheme: 'light'});

DeviceEventEmitter.emit('appearanceChanged', {colorScheme: 'light'});
Appearance.setColorScheme('light');

expect(listener).toHaveBeenCalledTimes(1);

DeviceEventEmitter.emit('appearanceChanged', {colorScheme: 'dark'});

expect(listener).toHaveBeenCalledTimes(2);
expect(listener).toHaveBeenLastCalledWith({colorScheme: 'dark'});
expect(Appearance.getColorScheme()).toBe('dark');

subscription.remove();
});

it('updates useColorScheme in the same task as setColorScheme', () => {
let observed;
function TestComponent() {
observed = useColorScheme();
return <View />;
}

const root = Fantom.createRoot();
Fantom.runTask(() => {
root.render(<TestComponent />);
});

expect(observed).toBe('dark');

Fantom.runTask(() => {
Appearance.setColorScheme('light');
});

expect(observed).toBe('light');
});
});
Loading