Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material-UI Popover component does trigger onClose event

I am using Material-ui Popover component, the open of Popover component is handled by its state, I defined a function named handlePopoverClose to handle When I click outside the 'Hover with a Popover', it was supposed to set the state of anchorEl to null, and then state of open should turn to false. But this function action didn't trigger, does anyone know what happen?

import React from 'react';
import Popover from '@material-ui/core/Popover';
import Typography from '@material-ui/core/Typography';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles(theme => ({
  popover: {
    pointerEvents: 'none',
  },
  paper: {
    padding: theme.spacing(1),
  },
}));

export default function MouseOverPopover() {
  const classes = useStyles();
  const [anchorEl, setAnchorEl] = React.useState(null);

  const handlePopoverOpen = event => {
    setAnchorEl(event.currentTarget);
  };

  const handlePopoverClose = () => {
    setAnchorEl(null);
  };

  const open = Boolean(anchorEl);

  return (
    <div>
      <Typography
        aria-owns={open ? 'mouse-over-popover' : undefined}
        aria-haspopup="true"
        onMouseEnter={handlePopoverOpen}
      >
        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={handlePopoverClose}
        disableRestoreFocus
      >
        <Typography>I use Popover.</Typography>
      </Popover>
    </div>
  );
}
like image 428
HungryBird Avatar asked Jul 06 '26 06:07

HungryBird


1 Answers

you just have to remove below code from your styles:

popover: {
    pointerEvents: 'none',
  }

It will start working fine. Live demo: https://codesandbox.io/s/boring-snow-k6x1d

like image 146
sidverma Avatar answered Jul 08 '26 21:07

sidverma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!