diff --git a/packages/react-native/Libraries/Modal/Modal.d.ts b/packages/react-native/Libraries/Modal/Modal.d.ts index 21fca65479c..9c9e1ea5722 100644 --- a/packages/react-native/Libraries/Modal/Modal.d.ts +++ b/packages/react-native/Libraries/Modal/Modal.d.ts @@ -35,9 +35,9 @@ export interface ModalBaseProps { */ visible?: boolean | undefined; /** - * The `onRequestClose` callback is called when the user taps the hardware back button on Android or the menu button on Apple TV. + * The `onRequestClose` callback is called when the user taps the hardware back button on Android, dismisses the sheet using a gesture on iOS (when `allowSwipeDismissal` is set to true) or the menu button on Apple TV. * - * This is required on Apple TV and Android. + * This is required on iOS and Android. */ onRequestClose?: ((event: NativeSyntheticEvent) => void) | undefined; /** @@ -89,6 +89,12 @@ export interface ModalPropsIOS { onOrientationChange?: | ((event: NativeSyntheticEvent) => void) | undefined; + + /** + * Controls whether the modal can be dismissed by swiping down on iOS. + * This requires you to implement the `onRequestClose` prop to handle the dismissal. + */ + allowSwipeDismissal?: boolean | undefined; } export interface ModalPropsAndroid { diff --git a/packages/react-native/Libraries/Modal/Modal.js b/packages/react-native/Libraries/Modal/Modal.js index b3690c6f1d0..2bdcbecc4dd 100644 --- a/packages/react-native/Libraries/Modal/Modal.js +++ b/packages/react-native/Libraries/Modal/Modal.js @@ -86,9 +86,9 @@ export type ModalBaseProps = { */ visible?: ?boolean, /** - * The `onRequestClose` callback is called when the user taps the hardware back button on Android or the menu button on Apple TV. + * The `onRequestClose` callback is called when the user taps the hardware back button on Android, dismisses the sheet using a gesture on iOS (when `allowSwipeDismissal` is set to true) or the menu button on Apple TV. * - * This is required on Apple TV and Android. + * This is required on iOS and Android. */ // onRequestClose?: (event: NativeSyntheticEvent) => void; onRequestClose?: ?DirectEventHandler, @@ -147,6 +147,12 @@ export type ModalPropsIOS = { // | ((event: NativeSyntheticEvent) => void) // | undefined; onOrientationChange?: ?DirectEventHandler, + + /** + * Controls whether the modal can be dismissed by swiping down on iOS. + * This requires you to implement the `onRequestClose` prop to handle the dismissal. + */ + allowSwipeDismissal?: ?boolean, }; export type ModalPropsAndroid = { @@ -192,6 +198,16 @@ function confirmProps(props: ModalProps) { 'Modal with translucent navigation bar and without translucent status bar is not supported.', ); } + + if ( + Platform.OS === 'ios' && + props.allowSwipeDismissal === true && + !props.onRequestClose + ) { + console.warn( + 'Modal requires the onRequestClose prop when used with `allowSwipeDismissal`. This is necessary to prevent state corruption.', + ); + } } } @@ -327,6 +343,7 @@ class Modal extends React.Component { onStartShouldSetResponder={this._shouldSetResponder} supportedOrientations={this.props.supportedOrientations} onOrientationChange={this.props.onOrientationChange} + allowSwipeDismissal={this.props.allowSwipeDismissal} testID={this.props.testID}> diff --git a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap index 3ea96f69b20..c7e8aee0244 100644 --- a/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap +++ b/packages/react-native/Libraries/__tests__/__snapshots__/public-api-test.js.snap @@ -5689,6 +5689,7 @@ export type ModalPropsIOS = { >, onDismiss?: ?() => void, onOrientationChange?: ?DirectEventHandler, + allowSwipeDismissal?: ?boolean, }; export type ModalPropsAndroid = { hardwareAccelerated?: ?boolean, diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTFabricModalHostViewController.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTFabricModalHostViewController.mm index a1fd1eeffd4..eb100a437d5 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTFabricModalHostViewController.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTFabricModalHostViewController.mm @@ -22,8 +22,6 @@ } _touchHandler = [RCTSurfaceTouchHandler new]; - self.modalInPresentation = YES; - return self; } diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm index a1f484dbad2..af486993a3f 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/Modal/RCTModalHostViewComponentView.mm @@ -124,6 +124,7 @@ static ModalHostViewEventEmitter::OnOrientationChange onOrientationChangeStruct( _viewController = [RCTFabricModalHostViewController new]; _viewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; _viewController.delegate = self; + _viewController.modalInPresentation = YES; } return _viewController; } @@ -239,6 +240,7 @@ static ModalHostViewEventEmitter::OnOrientationChange onOrientationChangeStruct( - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &)oldProps { + const auto &oldViewProps = static_cast(*_props); const auto &newProps = static_cast(*props); #if !TARGET_OS_TV @@ -251,6 +253,10 @@ static ModalHostViewEventEmitter::OnOrientationChange onOrientationChangeStruct( self.viewController.modalPresentationStyle = presentationConfiguration(newProps); + if (oldViewProps.allowSwipeDismissal != newProps.allowSwipeDismissal) { + self.viewController.modalInPresentation = !newProps.allowSwipeDismissal; + } + _shouldPresent = newProps.visible; [self ensurePresentedOnlyIfNeeded]; @@ -283,6 +289,16 @@ static ModalHostViewEventEmitter::OnOrientationChange onOrientationChangeStruct( } } +- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController +{ + auto eventEmitter = [self modalEventEmitter]; + const auto &props = static_cast(*_props); + + if (eventEmitter && props.allowSwipeDismissal) { + eventEmitter->onRequestClose({}); + } +} + @end #ifdef __cplusplus diff --git a/packages/react-native/React/Views/RCTModalHostView.h b/packages/react-native/React/Views/RCTModalHostView.h index 2fcdcaea83f..c7811587c60 100644 --- a/packages/react-native/React/Views/RCTModalHostView.h +++ b/packages/react-native/React/Views/RCTModalHostView.h @@ -24,6 +24,7 @@ @property (nonatomic, copy) RCTDirectEventBlock onShow; @property (nonatomic, assign) BOOL visible; +@property (nonatomic, assign) BOOL allowSwipeDismissal; // Android only @property (nonatomic, assign) BOOL statusBarTranslucent; diff --git a/packages/react-native/React/Views/RCTModalHostView.m b/packages/react-native/React/Views/RCTModalHostView.m index 5e1c2a6d8f1..475802f3351 100644 --- a/packages/react-native/React/Views/RCTModalHostView.m +++ b/packages/react-native/React/Views/RCTModalHostView.m @@ -35,6 +35,7 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : coder) if ((self = [super initWithFrame:CGRectZero])) { _bridge = bridge; _modalViewController = [RCTModalHostViewController new]; + _modalViewController.modalInPresentation = YES; UIView *containerView = [UIView new]; containerView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; _modalViewController.view = containerView; @@ -50,6 +51,14 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : coder) return self; } +- (void)setAllowSwipeDismissal:(BOOL)allowSwipeDismissal +{ + if (_allowSwipeDismissal != allowSwipeDismissal) { + _allowSwipeDismissal = allowSwipeDismissal; + _modalViewController.modalInPresentation = !allowSwipeDismissal; + } +} + - (void)notifyForBoundsChange:(CGRect)newBounds { if (_reactSubview && _isPresented) { @@ -70,6 +79,13 @@ RCT_NOT_IMPLEMENTED(-(instancetype)initWithCoder : coder) } } +- (void)presentationControllerDidDismiss:(UIPresentationController *)presentationController +{ + if (_onRequestClose != nil && _allowSwipeDismissal) { + _onRequestClose(nil); + } +} + - (void)notifyForOrientationChange { if (!_onOrientationChange) { diff --git a/packages/react-native/React/Views/RCTModalHostViewController.m b/packages/react-native/React/Views/RCTModalHostViewController.m index 41db3b01431..f0277f8c35e 100644 --- a/packages/react-native/React/Views/RCTModalHostViewController.m +++ b/packages/react-native/React/Views/RCTModalHostViewController.m @@ -22,8 +22,6 @@ return nil; } - self.modalInPresentation = YES; - _preferredStatusBarStyle = [RCTUIStatusBarManager() statusBarStyle]; _preferredStatusBarHidden = [RCTUIStatusBarManager() isStatusBarHidden]; diff --git a/packages/react-native/React/Views/RCTModalHostViewManager.m b/packages/react-native/React/Views/RCTModalHostViewManager.m index eccf05f9cb6..203d0b44134 100644 --- a/packages/react-native/React/Views/RCTModalHostViewManager.m +++ b/packages/react-native/React/Views/RCTModalHostViewManager.m @@ -119,6 +119,7 @@ RCT_EXPORT_VIEW_PROPERTY(supportedOrientations, NSArray) RCT_EXPORT_VIEW_PROPERTY(onOrientationChange, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(visible, BOOL) RCT_EXPORT_VIEW_PROPERTY(onRequestClose, RCTDirectEventBlock) +RCT_EXPORT_VIEW_PROPERTY(allowSwipeDismissal, BOOL) // Fabric only RCT_EXPORT_VIEW_PROPERTY(onDismiss, RCTDirectEventBlock) diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 19eef9468a7..97434b8b351 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -5306,6 +5306,7 @@ public class com/facebook/react/viewmanagers/ModalHostViewManagerDelegate : com/ } public abstract interface class com/facebook/react/viewmanagers/ModalHostViewManagerInterface : com/facebook/react/uimanager/ViewManagerWithGeneratedInterface { + public abstract fun setAllowSwipeDismissal (Landroid/view/View;Z)V public abstract fun setAnimated (Landroid/view/View;Z)V public abstract fun setAnimationType (Landroid/view/View;Ljava/lang/String;)V public abstract fun setHardwareAccelerated (Landroid/view/View;Z)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.kt index b020b29d07b..8bfe5328a8e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/modal/ReactModalHostManager.kt @@ -85,6 +85,9 @@ internal class ReactModalHostManager : @ReactProp(name = "identifier") override fun setIdentifier(view: ReactModalHostView, value: Int): Unit = Unit + @ReactProp(name = "allowSwipeDismissal") + override fun setAllowSwipeDismissal(view: ReactModalHostView, value: Boolean): Unit = Unit + override fun setTestId(view: ReactModalHostView, value: String?) { super.setTestId(view, value) view.setDialogRootViewGroupTestId(value) diff --git a/packages/react-native/src/private/specs_DEPRECATED/components/RCTModalHostViewNativeComponent.js b/packages/react-native/src/private/specs_DEPRECATED/components/RCTModalHostViewNativeComponent.js index 98338f87a0f..b5aa18adf20 100644 --- a/packages/react-native/src/private/specs_DEPRECATED/components/RCTModalHostViewNativeComponent.js +++ b/packages/react-native/src/private/specs_DEPRECATED/components/RCTModalHostViewNativeComponent.js @@ -112,6 +112,12 @@ type RCTModalHostViewNativeProps = $ReadOnly<{ */ animated?: WithDefault, + /** + * Controls whether the modal can be dismissed by swiping down on iOS. + * This requires you to implement the `onRequestClose` prop to handle the dismissal. + */ + allowSwipeDismissal?: WithDefault, + /** * The `supportedOrientations` prop allows the modal to be rotated to any of the specified orientations. * diff --git a/packages/rn-tester/js/examples/Modal/ModalPresentation.js b/packages/rn-tester/js/examples/Modal/ModalPresentation.js index fe310ba7b22..5ac8e416fed 100644 --- a/packages/rn-tester/js/examples/Modal/ModalPresentation.js +++ b/packages/rn-tester/js/examples/Modal/ModalPresentation.js @@ -49,6 +49,7 @@ function ModalPresentation() { const onRequestClose = useCallback(() => { console.log('onRequestClose'); + setProps(prev => ({...prev, visible: false})); }, []); const [props, setProps] = useState({ @@ -61,6 +62,7 @@ function ModalPresentation() { ios: 'fullScreen', default: undefined, }), + allowSwipeDismissal: false, supportedOrientations: Platform.select({ ios: ['portrait'], default: undefined, @@ -74,6 +76,7 @@ function ModalPresentation() { const hardwareAccelerated = props.hardwareAccelerated; const statusBarTranslucent = props.statusBarTranslucent; const navigationBarTranslucent = props.navigationBarTranslucent; + const allowSwipeDismissal = props.allowSwipeDismissal; const backdropColor = props.backdropColor; const backgroundColor = useContext(RNTesterThemeContext).BackgroundColor; @@ -131,6 +134,21 @@ function ModalPresentation() { } /> + + + + Allow Swipe Dismissal ⚫️ + + + setProps(prev => ({ + ...prev, + allowSwipeDismissal: enabled, + })) + } + /> + Presentation Style ⚫️