-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Implements the Appearance native module as discussed in react-native-community/discussions-and-proposals#126. The purpose of the Appearance native module is to expose the user's appearance preferences. It provides a basic get() API that returns the user's preferred color scheme on iOS 13 devices, also known as Dark Mode. It also provides the ability to subscribe to events whenever an appearance preference changes. The name, "Appearance", was chosen purposefully to allow for future expansion to cover other appearance preferences such as reduced motion, reduced transparency, or high contrast modes. Changelog: [iOS] [Added] - The Appearance native module can be used to prepare your app for Dark Mode on iOS 13. Reviewed By: yungsters Differential Revision: D16699954 fbshipit-source-id: 03b4cc5d2a1a69f31f3a6d9bece23f6867b774ea
- Loading branch information
1 parent
26a8d2e
commit 63fa3f2
Showing
12 changed files
with
381 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its 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 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import EventEmitter from '../vendor/emitter/EventEmitter'; | ||
import NativeEventEmitter from '../EventEmitter/NativeEventEmitter'; | ||
import NativeAppearance, { | ||
type AppearancePreferences, | ||
type ColorSchemeName, | ||
} from './NativeAppearance'; | ||
import invariant from 'invariant'; | ||
|
||
type AppearanceListener = (preferences: AppearancePreferences) => void; | ||
const eventEmitter = new EventEmitter(); | ||
|
||
const nativeColorScheme: ?string = | ||
NativeAppearance == null ? null : NativeAppearance.getColorScheme(); | ||
invariant( | ||
nativeColorScheme === 'dark' || | ||
nativeColorScheme === 'light' || | ||
nativeColorScheme == null, | ||
"Unrecognized color scheme. Did you mean 'dark' or 'light'?", | ||
); | ||
|
||
let currentColorScheme: ?ColorSchemeName = nativeColorScheme; | ||
|
||
if (NativeAppearance) { | ||
const nativeEventEmitter = new NativeEventEmitter(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'?", | ||
); | ||
currentColorScheme = colorScheme; | ||
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 { | ||
return currentColorScheme; | ||
}, | ||
/** | ||
* Add an event handler that is fired when appearance preferences change. | ||
*/ | ||
addChangeListener(listener: AppearanceListener): void { | ||
eventEmitter.addListener('change', listener); | ||
}, | ||
/** | ||
* Remove an event handler. | ||
*/ | ||
removeChangeListener(listener: AppearanceListener): void { | ||
eventEmitter.removeListener('change', listener); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its 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 | ||
*/ | ||
|
||
'use strict'; | ||
|
||
import type {TurboModule} from '../TurboModule/RCTExport'; | ||
import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry'; | ||
|
||
export type ColorSchemeName = 'light' | 'dark'; | ||
|
||
export type AppearancePreferences = {| | ||
// TODO: (hramos) T52919652 Use ?ColorSchemeName once codegen supports union | ||
// types. | ||
/* 'light' | 'dark' */ | ||
colorScheme?: ?string, | ||
|}; | ||
|
||
export interface Spec extends TurboModule { | ||
// TODO: (hramos) T52919652 Use ?ColorSchemeName once codegen supports union | ||
// types. | ||
/* 'light' | 'dark' */ | ||
+getColorScheme: () => ?string; | ||
|
||
// RCTEventEmitter | ||
+addListener: (eventName: string) => void; | ||
+removeListeners: (count: number) => void; | ||
} | ||
|
||
export default (TurboModuleRegistry.get<Spec>('Appearance'): ?Spec); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#import <UIKit/UIKit.h> | ||
|
||
#import <React/RCTBridgeModule.h> | ||
#import <React/RCTEventEmitter.h> | ||
|
||
NSString *const RCTUserInterfaceStyleDidChangeNotification = @"RCTUserInterfaceStyleDidChangeNotification"; | ||
|
||
@interface RCTAppearance : RCTEventEmitter <RCTBridgeModule> | ||
@end |
Oops, something went wrong.