Skip to content

Menu automatically dismisses on web when outside the bounds of the window #3854

Open

Description

@CodingItWrong

Current behaviour

On web on desktop browsers (at least on macOS), when a Menu attempts to display and it partially falls outside the bounds of the browser window, it auto-dismisses.

Expected behaviour

Ideally, the Menu would position itself so that it would not attempt to display outside the bounds of the browser window, so that no scrolling is needed. But if it does attempt to display outside the bounds of the browser window, it would not auto-dismiss, so that at least the user can access it by scrolling.

How to reproduce?

https://github.com/CodingItWrong/paper-sandbox/tree/menu-auto-dismiss-web

  • Open the app in any desktop web browser (tested in Chrome, Safari, and Firefox for Mac)
  • Click each menu button
  • Expected: the menu displays for all buttons
  • Actual: on any web browser for Mac, the left and right menus display, but the right button flashes and is automatically dismissed

Note: the bug does not occur on iOS or Android native, on iOS Safari, or on Android Chrome.

Preview

menu-web-auto-dismiss.mov

What have you tried so far?

Tried adjusting anchorPosition so the Menu displays within the window. But since it is displaying off to the right, anchorPosition's top and bottom don't help. It might be a workaround if we could anchor to left and right, similar to MUI: https://mui.com/material-ui/react-popover/#anchor-playground

Tried adding margin to the right of the button so that the menu fully fits within the browser window. This prevents triggering the issue, but will be challenging for my use case on small screens.

Your Environment

software version
ios 16.2
android n/a
react-native 0.71.7
react-native-paper 5.7.0
node 18.15.0
npm or yarn yarn 1.22.19
expo sdk 48.0.15

Activity

  1. jboyan commented on Apr 30, 2023

    @jboyan

    see also #3716

  2. CodingItWrong commented on May 1, 2023

    @CodingItWrong
    ContributorAuthor

    Thanks @jboyan, #3678 (linked from #3716) seems relates too

  3. nappa0326 commented on May 19, 2023

    @nappa0326

    I have the same problem. Any solusions? If the menu over the right or bottom edges of the screen, a screen redraw will occur and the menu will be automatically dismissed.

    https://snack.expo.dev/@nappa0326/popupmenusample?platform=web

    In this sample, the menu is not automatically dismissed, But when the menu is displayed by clicking on the right or bottom edge of the screen, a scrollbar appears for a moment, and the scrollbar also appears to change with the easing of the menu.

  4. ErionTp commented on May 27, 2023

    @ErionTp

    Another problem is when I have a list of let's say for example 100 items inside the menu. In this case, the menu won't open. Cause the height of the list becomes bigger than the screen. I tried to put a flatlist inside and everything works ok, but I don't think it is the best solution. Any solutions, please?

  5. nappa0326 commented on Jun 20, 2023

    @nappa0326

    anybody there? I tried with 5.8.0 and it was not fixed..

  6. dheeraj539 commented on Jul 25, 2023

    @dheeraj539

    Facing the same issue after upgrading my expo SDK version from 45 to 46 (Working without any issues in expo SDK 45)

  7. yang-sprinter commented on Aug 14, 2023

    @yang-sprinter

    I am also running into this issue.

  8. augini commented on Sep 12, 2023

    @augini

    I think this is not a solution but more like a workaround that wrapping the menu inside PaperProvider seems to avoid the menu from automatically dismissing.

  9. jarredkenny commented on Sep 14, 2023

    @jarredkenny

    Having the same problem. The wrapping in PaperProvider just pushes things around in my layout.

  10. yang-sprinter commented on Sep 14, 2023

    @yang-sprinter

    @lukewalczak would you be able to look into or suggest a solution for this? Seems like a number of folks are still running into this issue.

  11. VladTabacariu commented on Oct 12, 2023

    @VladTabacariu

    can confirm. same here. the menu autodismises if its left most positioned.

  12. lukewalczak commented on Oct 12, 2023

    @lukewalczak
    Member

    We're really open to getting help and contributions for the library! If you'd like to explore the issue and share your solution, we'd truly appreciate it.

  13. dzinreach commented on Dec 17, 2023

    @dzinreach

    Same here. The menu takes a sec to show and then dismisses within a sec of showing. It actually shows up properly when I wrap the component with BUT it messes up the rest of the layout.

    Using the latest React Native Paper and Expo SDK 50.

    If anyone finds a workaround please share.

  14. lukewis commented on Jan 12, 2024

    @lukewis

    In my experimentation, the workaround appears to be inserting the "overflow: 'hidden'" style into your view hierarchy at an appropriate location. How do you find the appropriate location?

    1. Open the dev tools and go to the "elements" (DOM) view
    2. Open the menu

    As the menu appears briefly (and then immediately disappears), you'll see a new div element appear in the DOM. You'll then need to apply the "overflow: 'hidden'" style ABOVE that level. In my case, the root of my app was a PaperProvider. I added a view above this level with the overflow style set to hidden, and now the menu sticks around correctly. YMMV, but I hope this helps!

    <View style={styles.root}>
          <PaperProvider>
             ...
          </PaperProvider>
    </View>
    
    const styles = StyleSheet.create({
      root: {
        flex: 1,
        overflow: "hidden"
      }
    }
    
  15. MorganTrudeau commented on Aug 1, 2025

    @MorganTrudeau

    I have done some investigation into what is causing the the menu to disappear after opening. It seems like sometimes on windows the menu opening will cause scroll bars to appear for a split second. The scroll bars appearing will cause the dimensions of the window to change triggering the dimensions listener inside the Menu component which triggers the onDismiss prop. As @lukewis mentioned adding overflow: "hidden" as a wrapper around the PaperProvider will work, but in my case it was breaking ScrollViews on some screens because the PaperProvider was wrapping my App Component.

    What I have done is add a view that wraps all content under the Portal inside the Menu component. This view contains the overflow: "hidden" style. This is working nicely.

    // Menu.jsx
    
       <Portal>
              <View style={styles.portalChild}>
                  ...
              </View>
       </Portal>
    
       ...
    
    
    const styles = StyleSheet.create({
      portalChild: {
        ...StyleSheet.absoluteFillObject, 
        overflow: 'hidden'
      },
    

    This is my patch:

    diff --git a/node_modules/react-native-paper/lib/commonjs/components/Menu/Menu.js b/node_modules/react-native-paper/lib/commonjs/components/Menu/Menu.js
    index 4a2de89..56433ee 100644
    --- a/node_modules/react-native-paper/lib/commonjs/components/Menu/Menu.js
    +++ b/node_modules/react-native-paper/lib/commonjs/components/Menu/Menu.js
    @@ -452,7 +452,9 @@ const Menu = _ref => {
       return /*#__PURE__*/React.createElement(_reactNative.View, {
         ref: ref => anchorRef.current = ref,
         collapsable: false
    -  }, isCoordinate(anchor) ? null : anchor, rendered ? /*#__PURE__*/React.createElement(_Portal.default, null, /*#__PURE__*/React.createElement(_reactNative.Pressable, {
    +  }, isCoordinate(anchor) ? null : anchor, rendered ? /*#__PURE__*/React.createElement(_Portal.default, null, /*#__PURE__*/React.createElement(_reactNative.View, {
    +    style: styles.portalChild
    +  }, /*#__PURE__*/React.createElement(_reactNative.Pressable, {
         accessibilityLabel: overlayAccessibilityLabel,
         accessibilityRole: "button",
         onPress: onDismiss,
    @@ -483,10 +485,14 @@ const Menu = _ref => {
         theme: theme
       }), scrollableMenuHeight && /*#__PURE__*/React.createElement(_reactNative.ScrollView, {
         keyboardShouldPersistTaps: keyboardShouldPersistTaps
    -  }, children) || /*#__PURE__*/React.createElement(React.Fragment, null, children))))) : null);
    +  }, children) || /*#__PURE__*/React.createElement(React.Fragment, null, children)))))) : null);
     };
     Menu.Item = _MenuItem.default;
     const styles = _reactNative.StyleSheet.create({
    +  portalChild: {
    +    ..._reactNative.StyleSheet.absoluteFillObject,
    +    overflow: "hidden"
    +  },
       wrapper: {
         position: 'absolute'
       },
    @@ -497,7 +503,8 @@ const styles = _reactNative.StyleSheet.create({
       pressableOverlay: {
         ..._reactNative.Platform.select({
           web: {
    -        cursor: 'auto'
    +        cursor: 'auto',
    +        position: 'fixed'
           }
         }),
         ..._reactNative.StyleSheet.absoluteFillObject,
    diff --git a/node_modules/react-native-paper/lib/module/components/Menu/Menu.js b/node_modules/react-native-paper/lib/module/components/Menu/Menu.js
    index 3c6623d..4a502a6 100644
    --- a/node_modules/react-native-paper/lib/module/components/Menu/Menu.js
    +++ b/node_modules/react-native-paper/lib/module/components/Menu/Menu.js
    @@ -442,7 +442,9 @@ const Menu = _ref => {
       return /*#__PURE__*/React.createElement(View, {
         ref: ref => anchorRef.current = ref,
         collapsable: false
    -  }, isCoordinate(anchor) ? null : anchor, rendered ? /*#__PURE__*/React.createElement(Portal, null, /*#__PURE__*/React.createElement(Pressable, {
    +  }, isCoordinate(anchor) ? null : anchor, rendered ? /*#__PURE__*/React.createElement(Portal, null, /*#__PURE__*/React.createElement(View, {
    +    style: styles.portalChild
    +  }, /*#__PURE__*/React.createElement(Pressable, {
         accessibilityLabel: overlayAccessibilityLabel,
         accessibilityRole: "button",
         onPress: onDismiss,
    @@ -473,10 +475,14 @@ const Menu = _ref => {
         theme: theme
       }), scrollableMenuHeight && /*#__PURE__*/React.createElement(ScrollView, {
         keyboardShouldPersistTaps: keyboardShouldPersistTaps
    -  }, children) || /*#__PURE__*/React.createElement(React.Fragment, null, children))))) : null);
    +  }, children) || /*#__PURE__*/React.createElement(React.Fragment, null, children)))))) : null);
     };
     Menu.Item = MenuItem;
     const styles = StyleSheet.create({
    +  portalChild: {
    +    ...StyleSheet.absoluteFillObject,
    +    overflow: "hidden"
    +  },
       wrapper: {
         position: 'absolute'
       },
    @@ -487,7 +493,8 @@ const styles = StyleSheet.create({
       pressableOverlay: {
         ...Platform.select({
           web: {
    -        cursor: 'auto'
    +        cursor: 'auto',
    +        position: 'fixed'
           }
         }),
         ...StyleSheet.absoluteFillObject,
    diff --git a/node_modules/react-native-paper/src/components/Menu/Menu.tsx b/node_modules/react-native-paper/src/components/Menu/Menu.tsx
    index db5e9a1..07327b7 100644
    --- a/node_modules/react-native-paper/src/components/Menu/Menu.tsx
    +++ b/node_modules/react-native-paper/src/components/Menu/Menu.tsx
    @@ -631,52 +631,54 @@ const Menu = ({
           {isCoordinate(anchor) ? null : anchor}
           {rendered ? (
             <Portal>
    -          <Pressable
    -            accessibilityLabel={overlayAccessibilityLabel}
    -            accessibilityRole="button"
    -            onPress={onDismiss}
    -            style={styles.pressableOverlay}
    -          />
    -          <View
    -            ref={(ref) => (menuRef.current = ref)}
    -            collapsable={false}
    -            accessibilityViewIsModal={visible}
    -            style={[styles.wrapper, positionStyle, style]}
    -            pointerEvents={pointerEvents}
    -            onAccessibilityEscape={onDismiss}
    -            testID={`${testID}-view`}
    -          >
    -            <Animated.View
    +          <View style={styles.portalChild}>
    +            <Pressable
    +              accessibilityLabel={overlayAccessibilityLabel}
    +              accessibilityRole="button"
    +              onPress={onDismiss}
    +              style={styles.pressableOverlay}
    +              />
    +            <View
    +              ref={(ref) => (menuRef.current = ref)}
    +              collapsable={false}
    +              accessibilityViewIsModal={visible}
    +              style={[styles.wrapper, positionStyle, style]}
                   pointerEvents={pointerEvents}
    -              style={{
    -                transform: positionTransforms,
    -              }}
    -            >
    -              <Surface
    -                mode={mode}
    +              onAccessibilityEscape={onDismiss}
    +              testID={`${testID}-view`}
    +              >
    +              <Animated.View
                     pointerEvents={pointerEvents}
    -                style={[
    -                  styles.shadowMenuContainer,
    -                  shadowMenuContainerStyle,
    -                  theme.isV3 && {
    -                    backgroundColor:
    +                style={{
    +                  transform: positionTransforms,
    +                }}
    +                >
    +                <Surface
    +                  mode={mode}
    +                  pointerEvents={pointerEvents}
    +                  style={[
    +                    styles.shadowMenuContainer,
    +                    shadowMenuContainerStyle,
    +                    theme.isV3 && {
    +                      backgroundColor:
                           theme.colors.elevation[ELEVATION_LEVELS_MAP[elevation]],
    -                  },
    -                  contentStyle,
    -                ]}
    -                {...(theme.isV3 && { elevation })}
    -                testID={`${testID}-surface`}
    -                theme={theme}
    -              >
    -                {(scrollableMenuHeight && (
    -                  <ScrollView
    -                    keyboardShouldPersistTaps={keyboardShouldPersistTaps}
    +                    },
    +                    contentStyle,
    +                  ]}
    +                  {...(theme.isV3 && { elevation })}
    +                  testID={`${testID}-surface`}
    +                  theme={theme}
                       >
    -                    {children}
    -                  </ScrollView>
    -                )) || <React.Fragment>{children}</React.Fragment>}
    -              </Surface>
    -            </Animated.View>
    +                  {(scrollableMenuHeight && (
    +                    <ScrollView
    +                    keyboardShouldPersistTaps={keyboardShouldPersistTaps}
    +                    >
    +                      {children}
    +                    </ScrollView>
    +                  )) || <React.Fragment>{children}</React.Fragment>}
    +                </Surface>
    +              </Animated.View>
    +            </View>
               </View>
             </Portal>
           ) : null}
    @@ -687,6 +689,10 @@ const Menu = ({
     Menu.Item = MenuItem;
     
     const styles = StyleSheet.create({
    +  portalChild: {
    +    ...StyleSheet.absoluteFillObject, 
    +    overflow: 'hidden'
    +  },
       wrapper: {
         position: 'absolute',
       },
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions