From 0a9cc34dd82a3a7dba576997ebd424b12876dbaa Mon Sep 17 00:00:00 2001 From: Arjan Zuidema Date: Fri, 28 Feb 2020 00:05:53 -0800 Subject: [PATCH] Added userInterfaceStyle prop to ActionSheetmanager to override user interface style for iOS 13 (#26401) Summary: Support to override actionsheet and share interface style to match your app. For example, when your app has it's own theming you want to match the stying on actionsheet and the share menu. ## Changelog [iOS] [Added] - Added userInterfaceStyle for ActionSheetIOS and Share to override user interface style on IOS 13 Pull Request resolved: https://github.com/facebook/react-native/pull/26401 Test Plan: Set dark style ![dark](https://user-images.githubusercontent.com/30040390/64685321-12a53080-d487-11e9-8846-f2ef89e114a2.jpg) Set light style ![light](https://user-images.githubusercontent.com/30040390/64685322-12a53080-d487-11e9-9dfd-1e07b9fe0ce2.jpg) Differential Revision: D17314080 Pulled By: hramos fbshipit-source-id: f84278ca99ba20347d17e27295f661d6690fa68c --- Libraries/ActionSheetIOS/ActionSheetIOS.js | 1 + .../NativeActionSheetManager.js | 2 ++ .../FBReactNativeSpec/FBReactNativeSpec.h | 12 ++++++++ React/CoreModules/RCTActionSheetManager.mm | 30 +++++++++++++++++++ React/CoreModules/RCTDevLoadingView.mm | 7 +++-- 5 files changed, 50 insertions(+), 2 deletions(-) diff --git a/Libraries/ActionSheetIOS/ActionSheetIOS.js b/Libraries/ActionSheetIOS/ActionSheetIOS.js index bda2844d4f..d3b904fbbb 100644 --- a/Libraries/ActionSheetIOS/ActionSheetIOS.js +++ b/Libraries/ActionSheetIOS/ActionSheetIOS.js @@ -46,6 +46,7 @@ const ActionSheetIOS = { +cancelButtonIndex?: ?number, +anchor?: ?number, +tintColor?: number | string, + +userInterfaceStyle?: string, |}, callback: (buttonIndex: number) => void, ) { diff --git a/Libraries/ActionSheetIOS/NativeActionSheetManager.js b/Libraries/ActionSheetIOS/NativeActionSheetManager.js index afc2b82f97..063d9147e1 100644 --- a/Libraries/ActionSheetIOS/NativeActionSheetManager.js +++ b/Libraries/ActionSheetIOS/NativeActionSheetManager.js @@ -24,6 +24,7 @@ export interface Spec extends TurboModule { +cancelButtonIndex?: ?number, +anchor?: ?number, +tintColor?: ?number, + +userInterfaceStyle?: ?string, |}, callback: (buttonIndex: number) => void, ) => void; @@ -35,6 +36,7 @@ export interface Spec extends TurboModule { +anchor?: ?number, +tintColor?: ?number, +excludedActivityTypes?: ?Array, + +userInterfaceStyle?: ?string, |}, failureCallback: (error: {| +domain: string, diff --git a/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h b/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h index 265fcdf377..a6cdf54b5d 100644 --- a/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h +++ b/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h @@ -117,6 +117,7 @@ namespace JS { folly::Optional cancelButtonIndex() const; folly::Optional anchor() const; folly::Optional tintColor() const; + NSString *userInterfaceStyle() const; SpecShowActionSheetWithOptionsOptions(NSDictionary *const v) : _v(v) {} private: @@ -138,6 +139,7 @@ namespace JS { folly::Optional anchor() const; folly::Optional tintColor() const; folly::Optional> excludedActivityTypes() const; + NSString *userInterfaceStyle() const; SpecShowShareActionSheetWithOptionsOptions(NSDictionary *const v) : _v(v) {} private: @@ -2935,6 +2937,11 @@ inline folly::Optional JS::NativeActionSheetManager::SpecShowActionSheet id const p = _v[@"tintColor"]; return RCTBridgingToOptionalDouble(p); } +inline NSString *JS::NativeActionSheetManager::SpecShowActionSheetWithOptionsOptions::userInterfaceStyle() const +{ + id const p = _v[@"userInterfaceStyle"]; + return RCTBridgingToString(p); +} inline NSString *JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsOptions::message() const { id const p = _v[@"message"]; @@ -2965,6 +2972,11 @@ inline folly::Optional> JS::NativeAction id const p = _v[@"excludedActivityTypes"]; return RCTBridgingToOptionalVec(p, ^NSString *(id itemValue_0) { return RCTBridgingToString(itemValue_0); }); } +inline NSString *JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsOptions::userInterfaceStyle() const +{ + id const p = _v[@"userInterfaceStyle"]; + return RCTBridgingToString(p); +} inline NSString *JS::NativeActionSheetManager::SpecShowShareActionSheetWithOptionsFailureCallbackError::domain() const { id const p = _v[@"domain"]; diff --git a/React/CoreModules/RCTActionSheetManager.mm b/React/CoreModules/RCTActionSheetManager.mm index ca87136929..9ca746a40b 100644 --- a/React/CoreModules/RCTActionSheetManager.mm +++ b/React/CoreModules/RCTActionSheetManager.mm @@ -129,6 +129,21 @@ RCT_EXPORT_METHOD(showActionSheetWithOptions:(JS::NativeActionSheetManager::Spec } alertController.view.tintColor = tintColor; +#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:options.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 + [self presentViewController:alertController onParentViewController:controller anchorViewTag:anchorViewTag]; } @@ -191,6 +206,21 @@ RCT_EXPORT_METHOD(showShareActionSheetWithOptions:(JS::NativeActionSheetManager: NSNumber *anchorViewTag = [RCTConvert NSNumber:options.anchor() ? @(*options.anchor()) : nil]; shareController.view.tintColor = [RCTConvert UIColor:options.tintColor() ? @(*options.tintColor()) : nil]; +#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:options.userInterfaceStyle()]; + + if (userInterfaceStyle == nil || [userInterfaceStyle isEqualToString:@""]) { + shareController.overrideUserInterfaceStyle = UIUserInterfaceStyleUnspecified; + } else if ([userInterfaceStyle isEqualToString:@"dark"]) { + shareController.overrideUserInterfaceStyle = UIUserInterfaceStyleDark; + } else if ([userInterfaceStyle isEqualToString:@"light"]) { + shareController.overrideUserInterfaceStyle = UIUserInterfaceStyleLight; + } + } +#endif + [self presentViewController:shareController onParentViewController:controller anchorViewTag:anchorViewTag]; } diff --git a/React/CoreModules/RCTDevLoadingView.mm b/React/CoreModules/RCTDevLoadingView.mm index a76f4a3e77..fdfee9176e 100644 --- a/React/CoreModules/RCTDevLoadingView.mm +++ b/React/CoreModules/RCTDevLoadingView.mm @@ -103,10 +103,13 @@ RCT_EXPORT_MODULE() self->_window.backgroundColor = backgroundColor; self->_window.hidden = NO; +#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \ +__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0 if (@available(iOS 13.0, *)) { - id scene = [[RCTSharedApplication() valueForKey:@"connectedScenes"] anyObject]; - [self->_window setValue:scene forKey:@"windowScene"]; + UIWindowScene *scene = (UIWindowScene *)RCTSharedApplication().connectedScenes.anyObject; + self->_window.windowScene = scene; } +#endif }); }