Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you disable "Swipe down to close" on the Modal component in React Native?

I'm using the core React Native Modal component. Within the modal content I have a Done button.

Pressing Doneis the only way we want users to close the modal. But the Modal component allows swiping down from the top of the screen to close.

How do you turn off "swipe to close"?

like image 866
GollyJer Avatar asked Mar 14 '18 17:03

GollyJer


People also ask

How do you turn off swipe gestures in react native?

Disable the swipe gesture for the screen ( gestureEnabled: false ). Override the native back button in the header with a custom back button ( headerLeft: (props) => <CustomBackButton {... props} /> ).

How do you swipe in react native?

In order to display something when we swipe either to the right or to the left, we have to render a component inside the props: renderLeftActions and/or renderRightActions . And to trigger a function on swipe we have to pass a function definition to onSwipeableLeftOpen and/or onSwipeableRightOpen .


4 Answers

To answer @Nikolai in the comments, I am using React Navigation.

I didn't realize the gesture settings from the navigator also controls the gestures of the react native modal.

Turning off gestures solved my problem.

const HomeScreenContainer = StackNavigator(
  {
    HomeScreen: { screen: Screens.HomeScreen },
    PostScreen: { screen: Screens.PostScreen },
    CameraScreen: { screen: Screens.CameraScreen },
    CameraRollScreen: { screen: Screens.CameraRollScreen },
  },
  {
    navigationOptions: {
      gesturesEnabled: false,
    },
  },
);
like image 98
GollyJer Avatar answered Oct 21 '22 23:10

GollyJer


Struggled with it a bit too. Here is what worked for me:

If you have root navigator as modal and inside it another stacked navigator for which you want to disable gestures, then put this inside root navigator for the stacked navigator, worked for me in v2.12 iOS
navigationOptions: { gesturesEnabled: false, },

here's full code:

const RootStack = createStackNavigator(
  {
    LoginNavigator: {
      screen: LoginNavigator,
      navigationOptions: {
        gesturesEnabled: false,
      },
    },
    ModerationNavigator: {
      screen: ModerationNavigator,
    },
    WalletNavigator: {
      screen: WalletNavigator,
    },
    FloatingNavigator: {
      screen: FloatingNavigator,
    },
    UIKitNavigator: {
      screen: UIKitNavigator,
    },
    MainMapViewScreen: {
      screen: MainMapViewScreen,
    },
    FullscreenPhotoScreen: {
      screen: FullscreenPhotoScreen,
    },
  },
  {
    mode: 'modal',
    initialRouteName: 'MainMapViewScreen',
    headerMode: 'none',
    header: null,
  },
);
like image 25
Oleg Dater Avatar answered Oct 21 '22 23:10

Oleg Dater


Since React Navigation Version 5.x, they have changed it to gestureEnabled instead of gesturesEnabled (without the 's) for both StackNavigator and DrawerNavigator

Sample usage:

<Stack.Navigator mode="modal" screenOptions={{ gestureEnabled: false }}>
    <Stack.Screen name="HomeNav" component={HomeNavigator} />
</Stack.Navigator>
like image 43
Mazin Luriahk Avatar answered Oct 22 '22 00:10

Mazin Luriahk


In addition to @GollyJer's answer, if you want to disable the swipe gesture for a single Modal you can also do this:

const AppNavigator = StackNavigator({
    ModalScreen: {
      screen: ModalScreen,
      navigationOptions: {
        gesturesEnabled: false
      },
    }
}
like image 5
Sam Bellerose Avatar answered Oct 22 '22 01:10

Sam Bellerose