Skip to content

Commit 47bd78f

Browse files
luoxuhaifacebook-github-bot
authored andcommitted
Added userInterfaceStyle to Alert to override user interface style for iOS 13+ (#33553)
Summary: Support to override Alert interface style to match your app. For example, You want to change the style on the alert. ## Changelog <!-- Help reviewers and the release process by writing your own changelog entry. For an example, see: https://github.com/facebook/react-native/wiki/Changelog --> [iOS] [Added] - Add userInterfaceStyle to Alert to override user interface style for iOS 13+ Pull Request resolved: #33553 Test Plan: **`userInterfaceStyle: 'light'`:** <img width="320" src="https://user-images.githubusercontent.com/37284154/161358408-50dbf0a5-ae46-458e-a075-8595cce1b046.png" /> **`userInterfaceStyle: 'dark'`:** <img width="320" src="https://user-images.githubusercontent.com/37284154/161358326-bc54effb-1635-43df-97e0-522328713259.PNG" /> Reviewed By: philIip Differential Revision: D35371697 Pulled By: ryancat fbshipit-source-id: 597c1a97ca94571abada2b5fb97cb2adcb5337f5
1 parent 472e0e4 commit 47bd78f

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

Libraries/Alert/Alert.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type Buttons = Array<{
2828

2929
type Options = {
3030
cancelable?: ?boolean,
31+
userInterfaceStyle?: 'unspecified' | 'light' | 'dark',
3132
onDismiss?: ?() => void,
3233
...
3334
};
@@ -45,7 +46,15 @@ class Alert {
4546
options?: Options,
4647
): void {
4748
if (Platform.OS === 'ios') {
48-
Alert.prompt(title, message, buttons, 'default');
49+
Alert.prompt(
50+
title,
51+
message,
52+
buttons,
53+
'default',
54+
undefined,
55+
undefined,
56+
options,
57+
);
4958
} else if (Platform.OS === 'android') {
5059
const NativeDialogManagerAndroid =
5160
require('../NativeModules/specs/NativeDialogManagerAndroid').default;
@@ -108,6 +117,7 @@ class Alert {
108117
type?: ?AlertType = 'plain-text',
109118
defaultValue?: string,
110119
keyboardType?: string,
120+
options?: Options,
111121
): void {
112122
if (Platform.OS === 'ios') {
113123
let callbacks = [];
@@ -142,6 +152,7 @@ class Alert {
142152
cancelButtonKey,
143153
destructiveButtonKey,
144154
keyboardType,
155+
userInterfaceStyle: options?.userInterfaceStyle || undefined,
145156
},
146157
(id, value) => {
147158
const cb = callbacks[id];

Libraries/Alert/NativeAlertManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export type Args = {|
2020
cancelButtonKey?: string,
2121
destructiveButtonKey?: string,
2222
keyboardType?: string,
23+
userInterfaceStyle?: string,
2324
|};
2425

2526
export interface Spec extends TurboModule {

React/CoreModules/RCTAlertManager.mm

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ @implementation RCTConvert (UIAlertViewStyle)
3232

3333
@end
3434

35+
@implementation RCTConvert (UIUserInterfaceStyle)
36+
37+
RCT_ENUM_CONVERTER(
38+
UIUserInterfaceStyle,
39+
(@{
40+
@"unspecified" : @(UIUserInterfaceStyleUnspecified),
41+
@"light" : @(UIUserInterfaceStyleLight),
42+
@"dark" : @(UIUserInterfaceStyleDark),
43+
}),
44+
UIUserInterfaceStyleUnspecified,
45+
integerValue)
46+
47+
@end
48+
3549
@interface RCTAlertManager () <NativeAlertManagerSpec>
3650

3751
@end
@@ -103,6 +117,15 @@ - (void)invalidate
103117
RCTAlertController *alertController = [RCTAlertController alertControllerWithTitle:title
104118
message:nil
105119
preferredStyle:UIAlertControllerStyleAlert];
120+
121+
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
122+
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
123+
if (@available(iOS 13.0, *)) {
124+
UIUserInterfaceStyle userInterfaceStyle = [RCTConvert UIUserInterfaceStyle:args.userInterfaceStyle()];
125+
alertController.overrideUserInterfaceStyle = userInterfaceStyle;
126+
}
127+
#endif
128+
106129
switch (type) {
107130
case RCTAlertViewStylePlainTextInput: {
108131
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

0 commit comments

Comments
 (0)