Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong positioning of React Material UI Popover while creating a component

I am working on creating separate components for every utilities provided by Material UI using Redux framework.

I got stuck in creating Popover component.

The problem is that when I call the Popover Container, It flies from top-left corner of the screen. However when creating that same popover using plain react framework. Everything is working fine.

Snippet of code: Popover.js



class PopoverTip extends Component {

    constructor(props) {
        super(props);
    }

  handleRequestClose = () => {
    this.props.togglePopover();
  };

  render() {
    const { isOpen, anchorEl } = this.props;
    return (
      < div>
        {isOpen && < Popover
          open={isOpen}
          anchorEl={anchorEl}
          anchorOrigin={{horizontal: 'left', vertical: 'bottom'}}
          targetOrigin={{horizontal: 'left', vertical: 'top'}}
          animation={PopoverAnimationVertical}
          onRequestClose={this.handleRequestClose}>
          < Menu>
            < MenuItem primaryText="Refresh" />
            < MenuItem primaryText="Help &amp; feedback" />
            < MenuItem primaryText="Settings" />
            < MenuItem primaryText="Sign out" />
          </ Menu>
        </ Popover>}
      </ div>
    );
  }
}

export default PopoverTip;

ExampleComponent.js - from where I am calling the popover element and showing the popover



showPopover(event) {
    event.preventDefault();
    this.props.togglePopover(event.currentTarget);
};

<IconButton onClick={ this.showPopover.bind(this) }>
  <HelpIco />
</IconButton>
<PopoverTip />

Please note that Popover functionality is working fine and I am also passing that event.currentTarget which is actually the current element.

The only issue I am facing is placement of that popover.

like image 229
jas Avatar asked Feb 06 '23 00:02

jas


1 Answers

This is a bit late, but I just fixed this issue in my own project.

Because of a typo, I was passing undefined to the anchorEl prop of my <Popover> component. Without a valid anchor, the Popover defaults to the top-left of the viewport, I believe.

like image 138
adfontes Avatar answered Feb 09 '23 03:02

adfontes