From c29ec46b0eee99670ce7762898fe3a4810db968b Mon Sep 17 00:00:00 2001 From: Nishan Bende Date: Mon, 16 Aug 2021 11:24:28 -0700 Subject: [PATCH] fix#29319 - ios dismiss modal (#31500) Summary: This PR aims to resolve iOS can't dismiss Modal on swipe gesture. https://github.com/facebook/react-native/issues/29319 When modal presentationStyle is pageSheet, iOS allows to dismiss the modal using swipe gesture. This PR adds support for that feature ## Changelog [iOS] [Added] - Support for onRequestClose for iOS Modal component. Pull Request resolved: https://github.com/facebook/react-native/pull/31500 Test Plan: - If onRequestClose updates the visibility state, modal will be closed. ``` ``` https://user-images.githubusercontent.com/23293248/117590263-36cd7f00-b14c-11eb-940c-86e700c0b8e7.mov ## Notes - In this PR, only support for partial drag is added. i.e. user can't drag the modal up and down completely. I added full user dragging but reverted in this [commit](https://github.com/facebook/react-native/commit/bb65b9a60d54b61652d608661eba876b49be3b17) to support controllable onRequestClose. If someone has any suggestion to have full draggable support + controllable onRequestClose, please let me know. Reviewed By: p-sun Differential Revision: D30041625 Pulled By: sammy-SC fbshipit-source-id: 9675da760bd5c070c4f0e1d30271c8af5c50b998 --- React/Views/RCTModalHostView.h | 2 +- React/Views/RCTModalHostView.m | 16 ++++++++++++++++ React/Views/RCTModalHostViewManager.m | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/React/Views/RCTModalHostView.h b/React/Views/RCTModalHostView.h index 880b24c9a48d46..01d38615400060 100644 --- a/React/Views/RCTModalHostView.h +++ b/React/Views/RCTModalHostView.h @@ -16,7 +16,7 @@ @protocol RCTModalHostViewInteractor; -@interface RCTModalHostView : UIView +@interface RCTModalHostView : UIView @property (nonatomic, copy) NSString *animationType; @property (nonatomic, assign) UIModalPresentationStyle presentationStyle; diff --git a/React/Views/RCTModalHostView.m b/React/Views/RCTModalHostView.m index fa9aae7a2c14f0..bea9e08af961b5 100644 --- a/React/Views/RCTModalHostView.m +++ b/React/Views/RCTModalHostView.m @@ -24,6 +24,7 @@ @implementation RCTModalHostView { RCTTouchHandler *_touchHandler; UIView *_reactSubview; UIInterfaceOrientation _lastKnownOrientation; + RCTDirectEventBlock _onRequestClose; } RCT_NOT_IMPLEMENTED(-(instancetype)initWithFrame : (CGRect)frame) @@ -57,6 +58,18 @@ - (void)notifyForBoundsChange:(CGRect)newBounds } } +- (void)setOnRequestClose:(RCTDirectEventBlock)onRequestClose +{ + _onRequestClose = onRequestClose; +} + +- (void)presentationControllerDidAttemptToDismiss:(UIPresentationController *)controller +{ + if (_onRequestClose != nil) { + _onRequestClose(nil); + } +} + - (void)notifyForOrientationChange { if (!_onOrientationChange) { @@ -169,6 +182,9 @@ - (void)ensurePresentedOnlyIfNeeded if (self.presentationStyle != UIModalPresentationNone) { _modalViewController.modalPresentationStyle = self.presentationStyle; } + if (@available(iOS 13.0, *)) { + _modalViewController.presentationController.delegate = self; + } [_delegate presentModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]]; _isPresented = YES; } diff --git a/React/Views/RCTModalHostViewManager.m b/React/Views/RCTModalHostViewManager.m index 7ca197d5866f7c..80342bac8dd792 100644 --- a/React/Views/RCTModalHostViewManager.m +++ b/React/Views/RCTModalHostViewManager.m @@ -121,6 +121,7 @@ - (void)invalidate RCT_EXPORT_VIEW_PROPERTY(supportedOrientations, NSArray) RCT_EXPORT_VIEW_PROPERTY(onOrientationChange, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(visible, BOOL) +RCT_EXPORT_VIEW_PROPERTY(onRequestClose, RCTDirectEventBlock) // Fabric only RCT_EXPORT_VIEW_PROPERTY(onDismiss, RCTDirectEventBlock)