Skip to content

Commit

Permalink
Added userInterfaceStyle to Alert to override user interface style fo…
Browse files Browse the repository at this point in the history
…r iOS 13+
  • Loading branch information
luoxuhai committed Apr 2, 2022
1 parent 6e0fa5f commit bcf26e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Libraries/Alert/Alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type Buttons = Array<{

type Options = {
cancelable?: ?boolean,
userInterfaceStyle?: string,
onDismiss?: ?() => void,
...
};
Expand All @@ -44,7 +45,15 @@ class Alert {
options?: Options,
): void {
if (Platform.OS === 'ios') {
Alert.prompt(title, message, buttons, 'default');
Alert.prompt(
title,
message,
buttons,
'default',
undefined,
undefined,
options,
);
} else if (Platform.OS === 'android') {
const NativeDialogManagerAndroid =
require('../NativeModules/specs/NativeDialogManagerAndroid').default;
Expand Down Expand Up @@ -107,6 +116,7 @@ class Alert {
type?: ?AlertType = 'plain-text',
defaultValue?: string,
keyboardType?: string,
options?: Options,
): void {
if (Platform.OS === 'ios') {
let callbacks = [];
Expand Down Expand Up @@ -141,6 +151,7 @@ class Alert {
cancelButtonKey,
destructiveButtonKey,
keyboardType,
userInterfaceStyle: options?.userInterfaceStyle || undefined,
},
(id, value) => {
const cb = callbacks[id];
Expand Down
1 change: 1 addition & 0 deletions Libraries/Alert/NativeAlertManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type Args = {|
cancelButtonKey?: string,
destructiveButtonKey?: string,
keyboardType?: string,
userInterfaceStyle?: string,
|};

export interface Spec extends TurboModule {
Expand Down
16 changes: 16 additions & 0 deletions React/CoreModules/RCTAlertManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ - (void)invalidate
RCTAlertController *alertController = [RCTAlertController alertControllerWithTitle:title
message:nil
preferredStyle:UIAlertControllerStyleAlert];

#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
NSString *userInterfaceStyle = [RCTConvert NSString:args.userInterfaceStyle()];

if (userInterfaceStyle == nil || [userInterfaceStyle isEqualToString:@""]) {
alertController.overrideUserInterfaceStyle = UIUserInterfaceStyleUnspecified;
} else if ([userInterfaceStyle isEqualToString:@"dark"]) {
alertController.overrideUserInterfaceStyle = UIUserInterfaceStyleDark;
} else if ([userInterfaceStyle isEqualToString:@"light"]) {
alertController.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
}
}
#endif

switch (type) {
case RCTAlertViewStylePlainTextInput: {
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
Expand Down

0 comments on commit bcf26e4

Please sign in to comment.