简体   繁体   English

滚动页面时,hover 上的弹出窗口应该消失 - React 的 Material UI (MUI) v5

[英]Popover on hover should disappear when page is scrolled - Material UI (MUI) v5 for React

When I follow the MUI v5 standard pattern to display a popover when another element is hovered over, it works fine, except when you当我按照 MUI v5 标准模式在另一个元素悬停时显示弹出窗口时,它工作正常,除非你

  1. Hover over the element Hover 过元
  2. Without moving the mouse, use the scroll wheel to scroll the document不移动鼠标,使用滚轮滚动文档
  3. The popover remains visible even after the triggering element has scrolled away即使在触发元素滚动离开后,弹出窗口仍然可见

This is the usual code snippet:这是通常的代码片段:

    <div style={{ height: "100vh", overflow: "auto" }}>
      <div style={{ height: 1000 }}>
        <Typography
          aria-owns={open ? "mouse-over-popover" : undefined}
          aria-haspopup="true"
          onMouseEnter={handlePopoverOpen}
          onMouseLeave={handlePopoverClose}
        >
          Hover and then scroll
        </Typography>
        <Popover
          id="mouse-over-popover"
          className={classes.popover}
          classes={{
            paper: classes.paper
          }}
          open={open}
          anchorEl={anchorEl}
          anchorOrigin={{
            vertical: "bottom",
            horizontal: "left"
          }}
          transformOrigin={{
            vertical: "top",
            horizontal: "left"
          }}
          onClose={handlePopoverClose}
          disableRestoreFocus
        >
          <Typography>I use Popover.</Typography>
        </Popover>
      </div>
    </div>

And here's a full working demo demonstrating the problem.这是一个完整的演示问题演示。

How can I make the popover disappear when the user scrolls the document?当用户滚动文档时,如何使弹出窗口消失? I do not want to disable scrolling.我不想禁用滚动。

Why not just add an event listener that listens to scroll event to certain element (or to any element in the page) and invoke the handlePopoverClose event when the scroll event is triggered?为什么不直接添加一个事件监听器来监听特定元素(或页面中的任何元素)的滚动事件,并在触发滚动事件时调用handlePopoverClose事件? Basically just add these lines to the component:基本上只需将这些行添加到组件中:

  ...
  // on mount, listen to scroll event on any element in the page (document, and its child)
  // if scroll is triggered, close the popover
  React.useEffect(() => {
    document.addEventListener("scroll", handlePopoverClose, true);
  }, []);

  // on unmount, remove the scroll event listener
  React.useEffect(
    () => () => {
      document.removeEventListener("scroll", handlePopoverClose, true);
    },
    []
  );
  ...

Here's the working fork demo .这是工作叉演示 Note that the true was set so that it adds the scroll event listener to any child element of the page.请注意,设置为true以便将滚动事件侦听器添加到页面的任何子元素。 You could also only add the scroll event listener to specific element, but it wouldn't solve the problem when the scroll is caused by other element (or window).您也可以只将滚动事件监听器添加到特定元素,但是当滚动是由其他元素(或窗口)引起时,它不会解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM