diff --git a/packages/react-native/React/CoreModules/RCTActionSheetManager.mm b/packages/react-native/React/CoreModules/RCTActionSheetManager.mm index d615eca6e8a3..15da8514b1e5 100644 --- a/packages/react-native/React/CoreModules/RCTActionSheetManager.mm +++ b/packages/react-native/React/CoreModules/RCTActionSheetManager.mm @@ -25,6 +25,8 @@ @interface RCTActionSheetManager () @property (nonatomic, strong) NSMutableArray *alertControllers; +@property (nonatomic, strong) UIControl *outsideTouchShield; +@property (nonatomic, copy) dispatch_block_t outsideTouchHandler; @end @@ -47,30 +49,77 @@ + (BOOL)requiresMainQueueSetup @synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED; +// The anchor view, if it is still on screen. The press that opened the sheet may have removed it. +- (UIView *)anchorViewForTag:(NSNumber *)anchorViewTag +{ + UIView *anchorView = anchorViewTag != nullptr ? [self.viewRegistry_DEPRECATED viewForReactTag:anchorViewTag] : nil; + return anchorView.window != nil ? anchorView : nil; +} + +// On iOS 26 a popover lets the touch that dismisses it reach the views below, so a tap outside +// the sheet would also press whatever React Native view is under the finger. +// A transparent control under the popover takes that touch and dismisses the sheet instead. +- (void)showOutsideTouchShieldInWindow:(UIWindow *)window handler:(dispatch_block_t)handler +{ + [self hideOutsideTouchShield]; + UIControl *shield = [[UIControl alloc] initWithFrame:window.bounds]; + shield.isAccessibilityElement = NO; + shield.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; + [shield addTarget:self action:@selector(outsideTouchShieldTouched) forControlEvents:UIControlEventTouchDown]; + [window addSubview:shield]; + self.outsideTouchShield = shield; + self.outsideTouchHandler = handler; +} + +- (void)outsideTouchShieldTouched +{ + dispatch_block_t handler = self.outsideTouchHandler; + if (handler != nil) { + handler(); + } +} + +- (void)hideOutsideTouchShield +{ + [self.outsideTouchShield removeFromSuperview]; + self.outsideTouchShield = nil; + self.outsideTouchHandler = nil; +} + - (void)presentViewController:(UIViewController *)alertController onParentViewController:(UIViewController *)parentViewController anchorViewTag:(NSNumber *)anchorViewTag + outsideTouchHandler:(dispatch_block_t)outsideTouchHandler { + [self hideOutsideTouchShield]; alertController.modalPresentationStyle = UIModalPresentationPopover; UIView *sourceView = parentViewController.view; + UIView *anchorView = [self anchorViewForTag:anchorViewTag]; - if (anchorViewTag != nullptr) { - sourceView = [self.viewRegistry_DEPRECATED viewForReactTag:anchorViewTag]; + if (anchorView != nil) { + sourceView = anchorView; } else { alertController.popoverPresentationController.permittedArrowDirections = 0; } - if ([UIDevice.currentDevice userInterfaceIdiom] == UIUserInterfaceIdiomPad) { - // These information only make sense for iPad, where the action sheet needs - // to be presented from a specific anchor point. - // Before iOS 26, if these information were passed on an iOS app, the action sheet - // was ignoring them. after iOS 26, they are took into consideration and the - // sheet behavior changes, for example the user can interact with the background - // By applying these informations only to the iPad use case, we revert to the previous - // behavior. + // On iOS 26 an iPhone action sheet without a source is shown centred, like an alert. + // When the caller passes an anchor, present the sheet from it as UIKit does in native apps. + BOOL presentsFromAnchor = NO; + if (@available(iOS 26.0, *)) { + presentsFromAnchor = anchorView != nil; + } + + if (presentsFromAnchor || [UIDevice.currentDevice userInterfaceIdiom] == UIUserInterfaceIdiomPad) { + // Before iOS 26, iPhones ignored the source view and rect. On iOS 26 they are used and the popover + // lets the background receive touches, so they are set on iPhone only when an anchor is passed, + // and the outside touch shield blocks the background while the sheet is shown. alertController.popoverPresentationController.sourceView = sourceView; alertController.popoverPresentationController.sourceRect = sourceView.bounds; } + // iPad keeps its popover behaviour, so the shield is only added on iPhone. + if (presentsFromAnchor && [UIDevice.currentDevice userInterfaceIdiom] != UIUserInterfaceIdiomPad) { + [self showOutsideTouchShieldInWindow:parentViewController.view.window handler:outsideTouchHandler]; + } [parentViewController presentViewController:alertController animated:YES completion:nil]; } @@ -165,6 +214,7 @@ - (void)showActionSheetWithOptions:(JS::NativeActionSheetManager::SpecShowAction if (!callbackInvoked) { callbackInvoked = true; [self->_alertControllers removeObject:alertController]; + [self hideOutsideTouchShield]; callback(@[ @(localIndex) ]); } }]; @@ -212,7 +262,25 @@ - (void)showActionSheetWithOptions:(JS::NativeActionSheetManager::SpecShowAction } [self->_alertControllers addObject:alertController]; - [self presentViewController:alertController onParentViewController:controller anchorViewTag:anchorViewTag]; + + __weak RCTActionSheetManager *weakSelf = self; + [self presentViewController:alertController + onParentViewController:controller + anchorViewTag:anchorViewTag + outsideTouchHandler:^{ + RCTActionSheetManager *strongSelf = weakSelf; + if (strongSelf == nil) { + return; + } + [alertController dismissViewControllerAnimated:YES completion:nil]; + [strongSelf->_alertControllers removeObject:alertController]; + [strongSelf hideOutsideTouchShield]; + // As in UIKit, a tap outside the popover selects the cancel button, if any. + if (!callbackInvoked && cancelButtonIndex >= 0) { + callbackInvoked = true; + callback(@[ @(cancelButtonIndex) ]); + } + }]; }); } @@ -226,6 +294,7 @@ - (void)dismissActionSheet dispatch_async(dispatch_get_main_queue(), ^{ [alertController dismissViewControllerAnimated:YES completion:nil]; [self->_alertControllers removeLastObject]; + [self hideOutsideTouchShield]; }); } @@ -283,8 +352,30 @@ - (void)showShareActionSheetWithOptions: } UIViewController *controller = RCTPresentedViewController(); + __weak RCTActionSheetManager *weakSelf = self; + __weak UIActivityViewController *weakShareController = shareController; + __block bool callbackInvoked = false; + dispatch_block_t outsideTouchHandler = [^{ + [weakSelf hideOutsideTouchShield]; + [weakShareController dismissViewControllerAnimated:YES completion:nil]; + // Same result as closing the share sheet: not completed, no activity. + if (!callbackInvoked) { + callbackInvoked = true; + successCallback(@[ @NO, [NSNull null] ]); + } + } copy]; shareController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, __unused NSArray *returnedItems, NSError *activityError) { + // Backing out of an activity calls this with the share sheet still open, so the shield stays. + // A later sheet may have its own shield by now, so only this sheet's shield is removed. + RCTActionSheetManager *strongSelf = weakSelf; + if ((activityError != nullptr || completed || activityType == nil) && + strongSelf.outsideTouchHandler == outsideTouchHandler) { + [strongSelf hideOutsideTouchShield]; + } + if (callbackInvoked) { + return; + } if (activityError != nullptr) { failureCallback(@[ RCTJSErrorFromNSError(activityError) ]); } else if (completed || activityType == nil) { @@ -302,7 +393,10 @@ - (void)showShareActionSheetWithOptions: shareController.overrideUserInterfaceStyle = UIUserInterfaceStyleLight; } - [self presentViewController:shareController onParentViewController:controller anchorViewTag:anchorViewTag]; + [self presentViewController:shareController + onParentViewController:controller + anchorViewTag:anchorViewTag + outsideTouchHandler:outsideTouchHandler]; }); }