How to keep showing the 'popover' on hovering on the anchorEl and 'popover' as well?

问题: Here in this example of material-ui https://material-ui.com/utils/popover/#mouse-over-interaction Follow these steps for the example material-ui https://material-ui.com/u...

问题:

Here in this example of material-ui https://material-ui.com/utils/popover/#mouse-over-interaction

Follow these steps for the example material-ui https://material-ui.com/utils/popover/#mouse-over-interaction

  1. In the above example keep the mouse on the text Hover with a Popover. --- you see the popover

  2. Try to move your mouse near the popover --- popover disappears right? But I wanna show the popover even if I hover on popover

And make popover disappear only if the user is not hovering on either popover or the Hover with a Popover. (basically anchorEl)

I am copying code from their demo

  import React from 'react';
    import PropTypes from 'prop-types';
    import Popover from '@material-ui/core/Popover';
    import Typography from '@material-ui/core/Typography';
    import { withStyles } from '@material-ui/core/styles';

    const styles = theme => ({
      popover: {
        pointerEvents: 'none',
      },
      paper: {
        padding: theme.spacing.unit,
      },
    });

    class MouseOverPopover extends React.Component {
      state = {
        anchorEl: null,
      };

      handlePopoverOpen = event => {
        this.setState({ anchorEl: event.currentTarget });
      };

      handlePopoverClose = () => {
        this.setState({ anchorEl: null });
      };

      render() {
        const { classes } = this.props;
        const { anchorEl } = this.state;
        const open = Boolean(anchorEl);

        return (
          <div>
            <Typography
              aria-owns={open ? 'mouse-over-popover' : undefined}
              aria-haspopup="true"
              onMouseEnter={this.handlePopoverOpen}
              onMouseLeave={this.handlePopoverClose}
            >
              Hover with a Popover.
            </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={this.handlePopoverClose}
              disableRestoreFocus
            >
              <Typography>I use Popover.</Typography>
            </Popover>
          </div>
        );
      }
    }

    MouseOverPopover.propTypes = {
      classes: PropTypes.object.isRequired,
    };

    export default withStyles(styles)(MouseOverPopover);

What code change I need to make here? You may experiment https://codesandbox.io/s/6l3wk6kv3


回答1:

codesandbox DEMO

I hacked my way through it by adding a setTimeout() function for the onMouseLeave event...I am sure that there is other ways of doing it, but it depends on your specific needs

  handlePopoverClose = () => {
    setTimeout(() => { 
      this.setState({ anchorEl: null });
    }, 3000);
  };
  • 发表于 2019-02-16 16:03
  • 阅读 ( 743 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除