react-native-menu icon indicating copy to clipboard operation
react-native-menu copied to clipboard

How to close menu when pressed outside?

Open shobhitsinghal624 opened this issue 3 years ago • 5 comments

Need a way to close the menu when user presses outside the area outside the drawer in the background screen. Is it possible in the current implementation?

shobhitsinghal624 avatar Feb 06 '22 15:02 shobhitsinghal624

@shobhitsinghal624 Waiting for a PR to be updated, once merged will check this out 👍

pedreviljoen avatar Feb 16 '22 05:02 pedreviljoen

Any news on this one?

edi avatar Mar 11 '22 17:03 edi

@edi PR merged, are you still facing need to close the drawer when press occurs outside of drawer area?

pedreviljoen avatar Mar 17 '22 08:03 pedreviljoen

@pedreviljoen hei, unless you want to make it a feature, don’t bother with it just for me.

I just copied the your source code and changed it quite a bit as per my needs, since I didn’t need some stuff, but needed other bits like swipe to close, tap outside, background overlay color (not just the opacity of the children), status bar coloring, translucent option (draw underneith on Android) and so on.

Next week when I have some time I can do a PR with a tap to close.

edi avatar Mar 17 '22 10:03 edi

I had this same desire. My somewhat simple but likely fragile solution has been:

In my Navigator.tsx:

return (
<>
      <View style={{ flex: 0, zIndex: 1002 }}>
          <DrawerMenu isOpen={isOpen} toggleMenu={toggleMenu} />
      </View>
      <View
        onResponderGrant={isOpen ? toggleDrawer : undefined}
        onStartShouldSetResponder={isOpen ? toggleDrawer : undefined}
        style={
          isOpen && {
            position: 'absolute',
            top: 0,
            bottom: 0,
            left: 0,
            right: 0,
            backgroundColor: 'rgba(0,0,0,0.5)',    // Wanted more contrast compared to MenuDrawers' opacity 
            zIndex: 1001,
          }
        }
      />

      <View style={{ flex: 1 }}>
           [...] // Content here. I have my navigator screens, but also want a header with a hamburger menu
      </View>
</>
);

Then in DrawerMenu.tsx:

  const drawerContent = () => {
    return (
      <SafeAreaView edges={edges} style={{ flex: 1, backgroundColor: 'white' }}>
          [....] // Menu here
      </SafeAreaView>
    );
 
  return (
      <MenuDrawer
        open={isOpen}
        drawerContent={drawerContent()}
        position={'right'}
        drawerPercentage={50}
        animationTime={200}
        overlay={true}
        opacity={1}
      />
  );

FlanaganSe avatar May 26 '22 07:05 FlanaganSe