OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Open and Close Dropdown menu on by mouse hover in react js

  • Thread starter Thread starter Durai DD
  • Start date Start date
D

Durai DD

Guest
I am new to React JS. My problem is that I want the menu to disappear if the mouse leaves that area, so I tried onMouseOut and onMouseLeave to close the menu but it seems that when this option is there the menu never opens! But when I remove the onMouseOver or Leave then it works fine but just stays there if you don't click something. MenuListProps={{ onMouseLeave: handleClose }}

That happens because when I open the menu, a modal layout is placed over the whole page, which means the onMouseLeave event will be fired immediately after onMouseEnter. but for it to work I must change the menu anchor so that it covers the button in its entirety

Code:
<div>
            {menuItem.map((text) => (
              <Button               
                onClick={(e) => handleChangeMenu(text, e)}
                onMouseOver={(e) => handleHover(text, e)}>
                {text}            
              </Button>
            ))}
            <Menu
              transitionDuration={700}
              anchorEl={project}
              open={Boolean(project)}
              onClose={() => {
                setproject(null);
              }}
              MenuListProps={{
                onMouseLeave: () => {
                  setproject(null);
                },
              }}
            >
              {mySubMenu.map((text) => (
                <MenuItem sx={menuItemStyle}>{text}</MenuItem>
              ))}
            </Menu>
          </div>

enter image description here

<p>I am new to React JS.
My problem is that I want the menu to disappear if the mouse leaves that area, so I tried <code>onMouseOut</code> and <code>onMouseLeave</code> to close the menu but it seems that when this option is there the menu never opens! But when I remove the <code>onMouseOver</code> or Leave then it works fine but just stays there if you don't click something.
<strong>MenuListProps={{ onMouseLeave: handleClose }}</strong></p>
<p>That happens because when I open the menu, a modal layout is placed over the whole page, which means the <code>onMouseLeave</code> event will be fired immediately after <code>onMouseEnter</code>. but for it to work I must change the menu anchor so that it covers the button in its entirety</p>
<pre><code><div>
{menuItem.map((text) => (
<Button
onClick={(e) => handleChangeMenu(text, e)}
onMouseOver={(e) => handleHover(text, e)}>
{text}
</Button>
))}
<Menu
transitionDuration={700}
anchorEl={project}
open={Boolean(project)}
onClose={() => {
setproject(null);
}}
MenuListProps={{
onMouseLeave: () => {
setproject(null);
},
}}
>
{mySubMenu.map((text) => (
<MenuItem sx={menuItemStyle}>{text}</MenuItem>
))}
</Menu>
</div>
</code></pre>
<p><a href="https://i.sstatic.net/rT1xV.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/rT1xV.gif" alt="enter image description here" /></a></p>
 

Latest posts

Top