Handling onMouseLeave event for Popover component in material ui library with hiderBackdrop parameter specified

   <Popover
       key={element.id}
       className={classes.popoverItem}
       classes={{
         paper: classes.paperElement
       }}
       open={isOpen}
       anchorEl={this.myRef.current}
       anchorOrigin={{
         vertical: 'bottom',
         horizontal: 'right'
       }}
       transformOrigin={{
         vertical: 'top',
         horizontal: 'left'
       }}
       disableRestoreFocus
       hideBackdrop
    >

The Popover above is causing an issue when trying to trigger the onMouseLeave event. It seems that due to the lack of other HTML elements surrounding it, the action is not being emitted. Is there a way to resolve this problem and make the event work as intended?

If you need more information on Popovers, check out the documentation here - https://material-ui.com/utils/popover/

Answer №1

One suggestion is to use the onMouseLeave event with PaperProps, like this: {{onMouseLeave: onClose}}

Answer №2

Dealing with a similar issue, their provided example proved to be unhelpful.

The solution is to turn off the pointer events:

<Popover
    style={{ pointerEvents: 'none'}}
    disableRestoreFocus
    ...
>
...

For an improved approach, consider using the Popper component instead.

Answer №3

It seems similar to my experience. Feel free to reach out if you find it useful.

<ButtonColorCircle
      aria-owns={open ? 'color-popover' : undefined}
      aria-haspopup="true"
      onMouseEnter={handleClick}
      // onMouseLeave={handleClose}
     /> 

<Popover
       id="color-popover"
       key={element.name}
       className={classes.popover}
       PaperProps={{onMouseLeave: handleClose}}
       onExit={handleClose}
       classes={{
         paper: classes.paper
       }}
       open={open}
       anchorEl={this.myRef.current}
       anchorOrigin={{
         vertical: 'bottom',
         horizontal: 'left'
       }}
       transformOrigin={{
         vertical: 'top',
         horizontal: 'left'
       }}
       disableRestoreFocus
       hideBackdrop
    >

Answer №4

I'm a bit uncertain about your objective, but Material-UI Popover includes an onExit feature that is similar to onMouseLeave.

Perhaps you can try something like this:

     <Popover
       key={element.name}
       className={classes.popover}
       classes={{
         paper: classes.paper
       }}
       open={open}
       anchorEl={this.myRef.current}
       anchorOrigin={{
         vertical: 'bottom',
         horizontal: 'left'
       }}
       transformOrigin={{
         vertical: 'top',
         horizontal: 'left'
       }}
       disableRestoreFocus
       hideBackdrop
       onExit={() => {
         INSERT YOUR CODE HERE TO PERFORM ACTIONS!
       }}
     >

Answer №5

Utilize these properties

slotProps={{ paper: { onMouseLeave: handleMenuMouseLeave } }}

Below is the entire code snippet

import * as React from "react";
import Button from "@mui/material/Button";
import Popover from "@mui/material/Popover";
import MenuItem from "@mui/material/MenuItem";

export default function PositionedMenu() {
  const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
  const open = Boolean(anchorEl);

  const handleClick = (event: React.MouseEvent<HTMLElement>) => {
    setAnchorEl(event.currentTarget);
    console.log("open");
  };

  const handleClose = () => {
    console.log("closed");
    setAnchorEl(null);
  };

  const handleMenuMouseEnter = (event: React.MouseEvent<HTMLElement>) => {
    console.log("menu mouse enter");
  };

  const handleMenuMouseLeave = () => {
    console.log("menu mouse leave");
    setAnchorEl(null);
  };

  return (
    <div>
      <Button
        id="demo-positioned-button"
        aria-haspopup="true"
        variant="text"
        sx={{ color: "black" }}
        aria-expanded={open ? "true" : undefined}
        onMouseEnter={(e) => handleClick(e)}
      >
        Parent
      </Button>
      <Popover
        id="demo-positioned-popover"
        open={open}
        anchorEl={anchorEl}
        onClose={handleClose}
        onMouseEnter={handleMenuMouseEnter}
        anchorOrigin={{
          vertical: "bottom",
          horizontal: "left",
        }}
        transformOrigin={{
          vertical: "top",
          horizontal: "left",
        }}
        sx={{ marginTop: "20px" }}
        slotProps={{ paper: { onMouseLeave: handleMenuMouseLeave } }}
      >
    
        <MenuItem onClick={handleClose}>Child 1</MenuItem>
        <MenuItem onClick={handleClose}>
         Child 2
        </MenuItem>
        <MenuItem onClick={handleClose}>Child 3</MenuItem>
      </Popover>
    </div>
  );
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to merge makeStyle classes from both the parent and child components together?

Is it possible to pass makeStyle classes from a parent component to a child component and merge them with the makeStyle classes within the child component? For example, incorporating breakpoint hiding into the child component's style. Here is an exam ...

Having trouble loading CSS and JavaScript files in CodeIgniter?

In my project, I am utilizing Bootstrap as a template. However, when attempting to access Bootstrap in Codeigniter, the page fails to load the CSS and JavaScript files. I have included the URL in autoload.php $autoload['helper'] = array('url ...

Style large and small text side by side with CSS styling

I've been trying to figure out how to position a smaller biography-style text next to this large title, but I'm having trouble finding a solution. I've experimented with float: left, float: right, and display: flex in my CSS code. Below is t ...

Unable to start React Native CLI Project Initialization

I recently started using React Native CLI for my CRUD project. After initializing the project and running 'npx react-native run-android' inside the project folder, I encountered the following errors: npx react-native run-android info Starting JS ...

The Material UI button feature neglects to account for custom CSS styles when attempting to override the default settings

Utilizing a custom bootstrap css styles in my react app, I am seeking to enhance the default material ui components with the bootstrap styles. import React, {useState} from 'react'; import 'cg-bootstrap/core/build/cg-bootstrap-standard.css&a ...

Displaying icons from an array using Material-UI

Issue at Hand: Currently, I am utilizing react and material-ui to develop my project. To maintain a clean code structure, I am populating menu items from a constant array as shown below: const menuItems = [ { label: "Home", path: "/home& ...

Modify the contents of the 'data-content' area in the popover

Looking for a solution where I can update only a portion of the data-content within a popover. Here is an example code snippet: <div class="popover-wrapper"> <i class="glyphicon glyphicon-question-sign hover_content" ...

Transferring an object between pages in NextJS

Is there a way to transfer nested object data from one page to another? I need to pass the entire object without relying on localStorage or sessionStorage, as the values have to be consumed from getServerSideProps. The router query method is not an optio ...

When the add button is clicked, I would like to implement a feature where a checkbox is added

When the user clicks on the link "출력하기", I want the web page to add a checkbox to all images. I wrote this code, but it's not working. Can anyone help me? This is my JS: $(document).ready(function(){ $("#print").on('click', fu ...

Alignment issue with Bootstrap dropdowns noticed after dynamically loading dropdown content in Blazor

When working with Blazor, you have the ability to dynamically load content by enclosing it within an @if block and then setting the condition to true, such as when a button is clicked. Recently, I encountered an issue with a Bootstrap dropdown in my proje ...

Create a Bootstrap multi-bar component for visually displaying the daytime information

I am looking to create a horizontal bar with multiple intervals. Here is what I have tried so far: <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;"& ...

Creating a dynamic div with various paragraphs using only Javascript

My goal is to dynamically generate paragraphs with their respective icons inside individual div elements. For instance, if the service API returns 30 items, I will create 30 div elements with the class "tile". However, if only one item is returned, then I ...

The object entries map fails to display

I have data stored in DynamoDB that I need to present in a list format for displaying the nutritional information (refer to AwsFunctions.js). AwsFunctions.js export function Nutrients(props) { // Load the AWS SDK for Node.js var AWS = require(&ap ...

What is the best way to neatly import multiple images in Next.js?

I have a dilemma involving 10 images located in my public directory that I need to use in a component. Instead of individually importing each image like this: import imgurl1 from "../../public/celsius.gif"; import imgurl2 from "../../public/ ...

Responsive tables in Bootstrap 4 that are nested

I am currently utilizing Bootstrap 4. I have a complex table structure where I want only the second column and onward to be scrollable. The challenge is making this specific part of the table responsive while keeping the rest static. I attempted to nest a ...

Issue with unit tests failing to properly update states after calling setState within a callback function

Exploring a basic modal component has led me to a scenario where upon clicking a button to open it, a request is triggered with two callback functions (success and error). The success callback aims to modify three states: function successGetData(data) { ...

`More than one path from a single location in NextJs`

Currently, I am in the process of developing a marketing/consumer website for my company. We are making a shift away from WordPress to a combination of Next and Prismic, which is a move we are excited about. Our consumer site consists of approximately 600 ...

The WebSocket has already transitioned to the CLOSING or CLOSED state in Socket io

Encountering an error with Socket.io: WebSocket is already in CLOSING or CLOSED state in Socket.io. When using Node.js: socket.to(socketId).emit('details', { userdetails: username }); In React JS: socket.on('details', data => { ...

Analyze React.js source code for errors, instead of focusing solely on the DOM manipulation aspect

Can breakpoints be set in React.js code and debugged in the actual script rather than just in compiled javascript? For example: var HelloMessage = React.createClass({ render: function() { return <div>Hello {this.props.name}</div>; // ...

Executing VueJS keyup handler after the previous onclick handler has been executed

Link to example code demonstrating the issue https://codepen.io/user123/pen/example-demo I am currently facing an issue with a text field named search_val that has a watcher attached to it. The text field includes a v-on keyup attribute to detect when th ...