Material UI Menu not closing after clicking a menu item - reactjs

This is code straight from MUI menu - customized menu.. I didn't want to put my code because there are some built in functions that make it more confusing.
In my code (not this) I open a MUI Dialog when a menu item is clicked. The issue is that the menu does not go away after the Dialog is submitted.
I would like to know how to make the menu close as soon as anything on the menu is clicked(menu items).
Thanks
import React from 'react';
import { withStyles } from '#material-ui/core/styles';
import Button from '#material-ui/core/Button';
import Menu, { MenuProps } from '#material-ui/core/Menu';
import MenuItem from '#material-ui/core/MenuItem';
import ListItemIcon from '#material-ui/core/ListItemIcon';
import ListItemText from '#material-ui/core/ListItemText';
import InboxIcon from '#material-ui/icons/MoveToInbox';
import DraftsIcon from '#material-ui/icons/Drafts';
import SendIcon from '#material-ui/icons/Send';
const StyledMenu = withStyles({
paper: {
border: '1px solid #d3d4d5',
},
})((props: MenuProps) => (
<Menu
elevation={0}
getContentAnchorEl={null}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'center',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'center',
}}
{...props}
/>
));
const StyledMenuItem = withStyles((theme) => ({
root: {
'&:focus': {
backgroundColor: theme.palette.primary.main,
'& .MuiListItemIcon-root, & .MuiListItemText-primary': {
color: theme.palette.common.white,
},
},
},
}))(MenuItem);
export default function CustomizedMenus() {
const [anchorEl, setAnchorEl] = React.useState<null | HTMLElement>(null);
const handleClick = (event: React.MouseEvent<HTMLElement>) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
return (
<div>
<Button
aria-controls="customized-menu"
aria-haspopup="true"
variant="contained"
color="primary"
onClick={handleClick}
>
Open Menu
</Button>
<StyledMenu
id="customized-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
>
<StyledMenuItem>
<ListItemIcon>
<SendIcon fontSize="small" />
</ListItemIcon>
<ListItemText primary="Sent mail" />
</StyledMenuItem>
<StyledMenuItem>
<ListItemIcon>
<DraftsIcon fontSize="small" />
</ListItemIcon>
<ListItemText primary="Drafts" />
</StyledMenuItem>
<StyledMenuItem>
<ListItemIcon>
<InboxIcon fontSize="small" />
</ListItemIcon>
<ListItemText primary="Inbox" />
</StyledMenuItem>
</StyledMenu>
</div>
);
}

You can just assign handleClose handler to the onClick property of the <Menu> itself like this:
<StyledMenu
onClick={handleClose}
onClose={handleClose}
{...yourProps}
>
...
</StyledMenu>

You can put an onClick prop to the MenuItem:
<StyledMenuItem onClick={handleClose}>Text</StyledMenuItem>

I had a similar issue, couldn't apply hangindev.com solution to my problem. Only difference was that my menu items were children passed outside of the menu component.
You could use the onBlur event on StyledMenu.
<StyledMenu
id="customized-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
onBlur={handleClose}
>{children}<StyledMenu>
Sorry for posting after 2 years, I hope this contributes. I used #mui/material 5.4.0 version.

onBlur={handleClose} works correctly if pass comp as children for other

we may use onClick instead of onClose in
<StyledMenu
id="customized-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClick={onClose}
>

Related

Material UI - Menu Component locks body scrollbar

I have made a dropdown menu using Material-ui Menu component.
The problem is once that dropdown menu is open, the body scrollbar disappears and can not scroll over the page.
I tried to find answers but there are only a few answers for Popper, Popover or Select component but seems like no answer for Menu component.
DropDownMenu component is like this.
import React from 'react'
import Menu from '#material-ui/core/Menu'
import MuiMenuItem from '#material-ui/core/MenuItem'
import styled from 'styled-components'
import MoreVertIcon from '#material-ui/icons/MoreVert'
import IconButton from '#material-ui/core/IconButton'
import SendIcon from '#material-ui/icons/Send'
import ListItemIcon from '#material-ui/core/ListItemIcon'
import ListItemText from '#material-ui/core/ListItemText'
const MenuItem = styled(MuiMenuItem)`
justify-content: flex-end;
`
export default function DropDownMenu() {
const [anchorEl, setAnchorEl] = React.useState(null)
const handleClick = (event) => {
setAnchorEl(event.currentTarget)
}
const handleClose = () => {
setAnchorEl(null)
}
return (
<div>
<IconButton
style={{ padding: 0 }}
aria-label="more"
aria-controls="long-menu"
aria-haspopup="true"
onClick={handleClick}
>
<MoreVertIcon style={{ fontSize: 15 }} />
</IconButton>
<Menu
id="simple-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
getContentAnchorEl={null}
anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
transformOrigin={{ vertical: 'top', horizontal: 'right' }}
>
<MenuItem onClick={handleClose}>
<ListItemIcon>
<SendIcon fontSize="small" />
</ListItemIcon>
<ListItemText primary="Sent mail" />
</MenuItem>
<MenuItem onClick={handleClose}>
<ListItemIcon>
<SendIcon fontSize="small" />
</ListItemIcon>
<ListItemText primary="Sent mail" />
</MenuItem>
<MenuItem onClick={handleClose}>
<ListItemIcon>
<SendIcon fontSize="small" />
</ListItemIcon>
<ListItemText primary="Sent mail" />
</MenuItem>
</Menu>
</div>
)
}
Sharpening code to Menu props is as following.
<Menu
id="simple-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
getContentAnchorEl={null}
anchorOrigin={{ vertical: 'bottom', horizontal: 'right' }}
transformOrigin={{ vertical: 'top', horizontal: 'right' }}
>
<MenuItem onClick={handleClose}>
<ListItemIcon>
<SendIcon fontSize="small" />
</ListItemIcon>
<ListItemText primary="Sent mail" />
</MenuItem>
The working example can be seen https://codesandbox.io/s/billowing-cache-042j1?file=/src/App.js
Thanks in advance.
Set the disableScrollLock prop to true. This prop is from the Material UI Modal component but it is also made available for the Menu component.
<Menu
...others
disableScrollLock={true}
>
</Menu>
You should use Popper instead of Menu. You should also create ref and use it for IconButton or Button.
import React from 'react'
import ClickAwayListener from '#material-ui/core/ClickAwayListener'
import Grow from '#material-ui/core/Grow'
import Paper from '#material-ui/core/Paper'
import Popper from '#material-ui/core/Popper'
import MenuItem from '#material-ui/core/MenuItem'
import MenuList from '#material-ui/core/MenuList'
import IconButton from '#material-ui/core/IconButton'
import MoreVertIcon from '#material-ui/icons/MoreVert'
import SendIcon from '#material-ui/icons/Send'
import ListItemIcon from '#material-ui/core/ListItemIcon'
import ListItemText from '#material-ui/core/ListItemText'
export default function DropDownMenu(props) {
const [open, setOpen] = React.useState(false)
const anchorRef = React.useRef(null)
const handleToggle = () => {
setOpen((prevOpen) => !prevOpen)
}
const handleClose = (event) => {
if (anchorRef.current && anchorRef.current.contains(event.target)) {
return
}
setOpen(false)
}
function handleListKeyDown(event) {
if (event.key === 'Tab') {
event.preventDefault()
setOpen(false)
}
}
const handleClick = () => {
// handle menu click here
setOpen(false)
}
return (
<div>
<IconButton
ref={anchorRef}
aria-controls={open ? 'menu-list-grow' : undefined}
aria-haspopup="true"
onClick={handleToggle}
size="small"
>
<MoreVertIcon fontSize="small" />
</IconButton>
<Popper open={open} anchorEl={anchorRef.current} transition disablePortal>
{({ TransitionProps, placement }) => (
<Grow
{...TransitionProps}
style={{ transformOrigin: placement === 'bottom' ? 'center top' : 'center bottom' }}
>
<Paper>
<ClickAwayListener onClickAway={handleClose}>
<MenuList autoFocusItem={open} id="menu-list-grow" onKeyDown={handleListKeyDown}>
<MenuItem onClick={handleClick}>
<ListItemIcon>
<SendIcon fontSize="small"/>
</ListItemIcon>
<ListItemText primary="Sent mail" />
</MenuItem>
<MenuItem onClick={handleClick}>
<ListItemIcon>
<SendIcon fontSize="small"/>
</ListItemIcon>
<ListItemText primary="Sent mail" />
</MenuItem>
</MenuList>
</ClickAwayListener>
</Paper>
</Grow>
)}
</Popper>
</div>
)
}
There is also an example code of it at Material UI Menus Documentation.

Material-ui.Why Popover set Modal's backdrop invisible

Can't understand why Popover set backdrop invisible by default, and get no way to change it.
Did I miss something important in Material Design?Or can I just create an issue for it?
<Modal
container={container}
open={open}
ref={ref}
BackdropProps={{ invisible: true }}
className={clsx(classes.root, className)}
{...other}
>
https://github.com/mui-org/material-ui/blob/next/packages/material-ui/src/Popover/Popover.js#L386
You can change it with BackdropProps={{ invisible: false }}. In the code snippet you included from Popover, if BackdropProps has been specified on the Popover it will be part of {...other} and will win over the earlier BackdropProps={{ invisible: true }}.
Here's a working example based on one of the demos:
import React from "react";
import { makeStyles } from "#material-ui/core/styles";
import Popover from "#material-ui/core/Popover";
import Typography from "#material-ui/core/Typography";
import Button from "#material-ui/core/Button";
const useStyles = makeStyles((theme) => ({
typography: {
padding: theme.spacing(2)
}
}));
export default function SimplePopover() {
const classes = useStyles();
const [anchorEl, setAnchorEl] = React.useState(null);
const handleClick = (event) => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
const open = Boolean(anchorEl);
const id = open ? "simple-popover" : undefined;
return (
<div>
<Button
aria-describedby={id}
variant="contained"
color="primary"
onClick={handleClick}
>
Open Popover
</Button>
<Popover
id={id}
open={open}
anchorEl={anchorEl}
onClose={handleClose}
anchorOrigin={{
vertical: "bottom",
horizontal: "center"
}}
transformOrigin={{
vertical: "top",
horizontal: "center"
}}
BackdropProps={{ invisible: false }}
>
<Typography className={classes.typography}>
The content of the Popover.
</Typography>
</Popover>
</div>
);
}

material-ui-popup-state change button color

I am using material-ui-popup-state to create a material ui drop down menu
In my Navbar.js i call <HoverMenu /> component
HoverMen.js is as follows
import * as React from 'react';
import withStyles from '#material-ui/core/styles/withStyles';
import Menu from 'material-ui-popup-state/HoverMenu';
import MenuItem from '#material-ui/core/MenuItem';
import ChevronRight from '#material-ui/icons/ChevronRight';
import ClickAwayListener from '#material-ui/core/ClickAwayListener';
import { Link } from 'react-router-dom';
import PopupState, {
bindHover,
bindMenu,
bindToggle
} from 'material-ui-popup-state';
import IconButton from '#material-ui/core/IconButton';
import MenuIcon from '#material-ui/icons/Menu';
const ParentPopupState = React.createContext(null);
const menuStyles = theme => ({
button: {
color: 'white'
}
});
const HoverMenu = () => (
<PopupState variant="popover" popupId="demoMenu">
{popupState => (
<ClickAwayListener onClickAway={popupState.close}>
<div>
<IconButton edge="start" {...bindToggle(popupState)}>
<MenuIcon />
</IconButton>
<ParentPopupState.Provider value={popupState}>
<Menu
{...bindMenu(popupState)}
anchorOrigin={{ vertical: 'bottom', horizontal: 'left' }}
transformOrigin={{ vertical: 'top', horizontal: 'left' }}
getContentAnchorEl={null}
>
<MenuItem onClick={popupState.close} to="/" component={Link}>
Home
</MenuItem>
<MenuItem onClick={popupState.close} to="/about" component={Link}>
About
</MenuItem>
<Submenu popupId="contribute" title="Contribute">
<MenuItem
onClick={popupState.close}
to="/donate"
component={Link}
>
To Samidoun
</MenuItem>
<MenuItem onClick={popupState.close}>To Benificiary</MenuItem>
<MenuItem
onClick={popupState.close}
to="/musicians"
component={Link}
>
To Musicians
</MenuItem>
</Submenu>
<MenuItem onClick={popupState.close} to="/" component={Link}>
Volunteer
</MenuItem>
<MenuItem
onClick={popupState.close}
to="/contact"
component={Link}
>
Contact
</MenuItem>
</Menu>
</ParentPopupState.Provider>
</div>
</ClickAwayListener>
)}
</PopupState>
);
export default HoverMenu;
const submenuStyles = theme => ({
menu: {
marginTop: theme.spacing(-1)
},
title: {
flexGrow: 1
},
moreArrow: {
marginRight: theme.spacing(-1)
}
});
const Submenu = withStyles(submenuStyles)(
// Unfortunately, MUI <Menu> injects refs into its children, which causes a
// warning in some cases unless we use forwardRef here.
React.forwardRef(({ classes, title, popupId, children, ...props }, ref) => (
<ParentPopupState.Consumer>
{parentPopupState => (
<PopupState
variant="popover"
popupId={popupId}
parentPopupState={parentPopupState}
>
{popupState => (
<ParentPopupState.Provider value={popupState}>
<MenuItem
{...bindHover(popupState)}
selected={popupState.isOpen}
ref={ref}
>
<span className={classes.title}>{title}</span>
<ChevronRight className={classes.moreArrow} />
</MenuItem>
<Menu
{...bindMenu(popupState)}
classes={{ paper: classes.menu }}
anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
transformOrigin={{ vertical: 'top', horizontal: 'left' }}
getContentAnchorEl={null}
{...props}
>
{children}
</Menu>
</ParentPopupState.Provider>
)}
</PopupState>
)}
</ParentPopupState.Consumer>
))
);
I need to change the color of the IconButton in the HoverMenu to white (something other than the primary and secondary colors)
<IconButton edge="start" {...bindToggle(popupState)}>
<MenuIcon />
</IconButton>
But i dont know how to use className in this situation.
Thank you
I found the solution as follows.
<IconButton
edge="start"
{...bindToggle(popupState)}
style={{ color: 'white' }}
>
<MenuIcon />
</IconButton>

How do I control the positon of the menu icon on a page using a material-ui menu?

By default the menu icon is appearing on the left of the page, but I need it on the right as shown in the attachment.
I have tried styling the div and the icon element, but no luck. I exhaustively searched the internet and I cannot find and answer. Thanks for helping.
Here's my code.
import React, { useState } from "react";
import { withStyles } from "#material-ui/core/styles";
import Menu from "#material-ui/core/Menu";
import MenuItem from "#material-ui/core/MenuItem";
import ListItemIcon from "#material-ui/core/ListItemIcon";
import ListItemText from "#material-ui/core/ListItemText";
import KeyboardArrowUpIcon from "#material-ui/icons/KeyboardArrowUp";
import Printer from "../SVG/Printer";
import Download from "../SVG/Download";
import Email from "../SVG/Email";
const StyledMenu = withStyles({
paper: {
border: "1px solid #d3d4d5"
}
})(props => (
<Menu
elevation={0}
getContentAnchorEl={null}
anchorOrigin={{
vertical: "bottom",
horizontal: "center"
}}
transformOrigin={{
vertical: "top",
horizontal: "center"
}}
{...props}
/>
));
const StyledMenuItem = withStyles(theme => ({
root: {
"&:focus": {
backgroundColor: theme.palette.primary.main,
"& .MuiListItemIcon-root, & .MuiListItemText-primary": {
color: theme.palette.common.white
}
}
}
}))(MenuItem);
const MyClaimedClassmatesOptionMenu = () => {
const [anchorEl, setAnchorEl] = useState(null);
const handleClick = event => {
setAnchorEl(event.currentTarget);
};
const handleClose = () => {
setAnchorEl(null);
};
const handleDownload = () => {
alert("You clicked handleDownload");
};
return (
<div>
<KeyboardArrowUpIcon
aria-controls="customized-menu"
aria-haspopup="true"
variant="contained"
color="primary"
onClick={handleClick}
></KeyboardArrowUpIcon>
<StyledMenu
id="customized-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
>
<StyledMenuItem onClick={handleDownload}>
<ListItemIcon>
<Download />
</ListItemIcon>
<ListItemText primary="Download" />
</StyledMenuItem>
<StyledMenuItem>
<ListItemIcon>
<Printer />
</ListItemIcon>
<ListItemText primary="Print" />
</StyledMenuItem>
<StyledMenuItem>
<ListItemIcon>
<Email />
</ListItemIcon>
<ListItemText primary="Email" />
</StyledMenuItem>
</StyledMenu>
</div>
);
};
export default MyClaimedClassmatesOptionMenu;
simple float-right should do the trick... this would be my response working from the code you provided...
relevant css:
.MuiSvgIcon-colorPrimary { float: right; }
complete working stackblitz here

How to Make Material-UI Menu based on Hover, not Click

I am using Material-UI Menu.
It should work as it was, but just using mouse hover, not click.
Here is my code link: https://codesandbox.io/embed/vn3p5j40m0
Below is the code of what I tried. It opens correctly, but doesn't close when the mouse moves away.
import React from "react";
import Button from "#material-ui/core/Button";
import Menu from "#material-ui/core/Menu";
import MenuItem from "#material-ui/core/MenuItem";
function SimpleMenu() {
const [anchorEl, setAnchorEl] = React.useState(null);
function handleClick(event) {
setAnchorEl(event.currentTarget);
}
function handleClose() {
setAnchorEl(null);
}
return (
<div>
<Button
aria-owns={anchorEl ? "simple-menu" : undefined}
aria-haspopup="true"
onClick={handleClick}
onMouseEnter={handleClick}
>
Open Menu
</Button>
<Menu
id="simple-menu"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={handleClose}
onMouseLeave={handleClose}
>
<MenuItem onClick={handleClose}>Profile</MenuItem>
<MenuItem onClick={handleClose}>My account</MenuItem>
<MenuItem onClick={handleClose}>Logout</MenuItem>
</Menu>
</div>
);
}
export default SimpleMenu;
The code below seems to work reasonably. The main changes compared to your sandbox are to use onMouseOver={handleClick} instead of onMouseEnter on the button. Without this change, it doesn't open reliably if the mouse isn't over where part of the menu will be. The other change is to use MenuListProps={{ onMouseLeave: handleClose }}. Using onMouseLeave directly on Menu doesn't work because the Menu includes an overlay as part of the Menu leveraging Modal and the mouse never "leaves" the overlay. MenuList is the portion of Menu that displays the menu items.
import React from "react";
import Button from "#material-ui/core/Button";
import Menu from "#material-ui/core/Menu";
import MenuItem from "#material-ui/core/MenuItem";
function SimpleMenu() {
const [anchorEl, setAnchorEl] = React.useState(null);
function handleClick(event) {
if (anchorEl !== event.currentTarget) {
setAnchorEl(event.currentTarget);
}
}
function handleClose() {
setAnchorEl(null);
}
return (
<div>
<Button
aria-owns={anchorEl ? "simple-menu" : undefined}
aria-haspopup="true"
onClick={handleClick}
onMouseOver={handleClick}
>
Open Menu
</Button>
<Menu
id="simple-menu"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={handleClose}
MenuListProps={{ onMouseLeave: handleClose }}
>
<MenuItem onClick={handleClose}>Profile</MenuItem>
<MenuItem onClick={handleClose}>My account</MenuItem>
<MenuItem onClick={handleClose}>Logout</MenuItem>
</Menu>
</div>
);
}
export default SimpleMenu;
I've updated Ryan's original answer to fix the issue where it doesn't close when you move the mouse off the element to the side.
How it works is to disable the pointerEvents on the MUI backdrop so you can continue to detect the hover behind it (and re-enables it again inside the menu container). This means we can add a leave event listener to the button as well.
It then keeps track of if you've hovered over either the button or menu using currentlyHovering.
When you hover over the button it shows the menu, then when you leave it starts a 50ms timeout to close it, but if we hover over the button or menu again in that time it will reset currentlyHovering and keep it open.
I've also added these lines so the menu opens below the button:
getContentAnchorEl={null}
anchorOrigin={{ horizontal: "left", vertical: "bottom" }}
import React from "react";
import Button from "#material-ui/core/Button";
import Menu from "#material-ui/core/Menu";
import MenuItem from "#material-ui/core/MenuItem";
import makeStyles from "#material-ui/styles/makeStyles";
const useStyles = makeStyles({
popOverRoot: {
pointerEvents: "none"
}
});
function SimpleMenu() {
let currentlyHovering = false;
const styles = useStyles();
const [anchorEl, setAnchorEl] = React.useState(null);
function handleClick(event) {
if (anchorEl !== event.currentTarget) {
setAnchorEl(event.currentTarget);
}
}
function handleHover() {
currentlyHovering = true;
}
function handleClose() {
setAnchorEl(null);
}
function handleCloseHover() {
currentlyHovering = false;
setTimeout(() => {
if (!currentlyHovering) {
handleClose();
}
}, 50);
}
return (
<div>
<Button
aria-owns={anchorEl ? "simple-menu" : undefined}
aria-haspopup="true"
onClick={handleClick}
onMouseOver={handleClick}
onMouseLeave={handleCloseHover}
>
Open Menu
</Button>
<Menu
id="simple-menu"
anchorEl={anchorEl}
open={Boolean(anchorEl)}
onClose={handleClose}
MenuListProps={{
onMouseEnter: handleHover,
onMouseLeave: handleCloseHover,
style: { pointerEvents: "auto" }
}}
getContentAnchorEl={null}
anchorOrigin={{ horizontal: "left", vertical: "bottom" }}
PopoverClasses={{
root: styles.popOverRoot
}}
>
<MenuItem onClick={handleClose}>Profile</MenuItem>
<MenuItem onClick={handleClose}>My account</MenuItem>
<MenuItem onClick={handleClose}>Logout</MenuItem>
</Menu>
</div>
);
}
export default SimpleMenu;
Using an interactive HTML tooltip with menu items works perfectly, without requiring you to necessarily click to view menu items.
Here is an example for material UI v.4.
import React from 'react';
import { withStyles, Theme, makeStyles } from '#material-ui/core/styles';
import Tooltip from '#material-ui/core/Tooltip';
import { MenuItem, IconButton } from '#material-ui/core';
import MoreVertIcon from '#material-ui/icons/MoreVert';
import styles from 'assets/jss/material-dashboard-pro-react/components/tasksStyle.js';
// #ts-ignore
const useStyles = makeStyles(styles);
const LightTooltip = withStyles((theme: Theme) => ({
tooltip: {
backgroundColor: theme.palette.common.white,
color: 'rgba(0, 0, 0, 0.87)',
boxShadow: theme.shadows[1],
fontSize: 11,
padding: 0,
margin: 4,
},
}))(Tooltip);
interface IProps {
menus: {
action: () => void;
name: string;
}[];
}
const HoverDropdown: React.FC<IProps> = ({ menus }) => {
const classes = useStyles();
const [showTooltip, setShowTooltip] = useState(false);
return (
<div>
<LightTooltip
interactive
open={showTooltip}
onOpen={() => setShowTooltip(true)}
onClose={() => setShowTooltip(false)}
title={
<React.Fragment>
{menus.map((item) => {
return <MenuItem onClick={item.action}>{item.name}</MenuItem>;
})}
</React.Fragment>
}
>
<IconButton
aria-label='more'
aria-controls='long-menu'
aria-haspopup='true'
className={classes.tableActionButton}
>
<MoreVertIcon />
</IconButton>
</LightTooltip>
</div>
);
};
export default HoverDropdown;
Usage:
<HoverDropdown
menus={[
{
name: 'Item 1',
action: () => {
history.push(
codeGeneratorRoutes.getEditLink(row.values['node._id'])
);
},
},{
name: 'Item 2',
action: () => {
history.push(
codeGeneratorRoutes.getEditLink(row.values['node._id'])
);
},
},{
name: 'Item 3',
action: () => {
history.push(
codeGeneratorRoutes.getEditLink(row.values['node._id'])
);
},
},{
name: 'Item 4',
action: () => {
history.push(
codeGeneratorRoutes.getEditLink(row.values['node._id'])
);
},
},
]}
/>
I gave up using Menu component because it implemented Popover. To solve the overlay problem I had to write too much code. So I tried to use the old CSS way:
CSS: relative parent element + absolute menu element
Component: Paper + MenuList
<ListItem>
<Link href="#" >
{user.name}
</Link>
<AccountPopover elevation={4}>
<MenuList>
<MenuItem>Profile</MenuItem>
<MenuItem>Logout</MenuItem>
</MenuList>
</AccountPopover>
</ListItem>
styled components:
export const ListItem = styled(Stack)(() => ({
position: 'relative',
"&:hover .MuiPaper-root": {
display: 'block'
}
}))
export const AccountPopover = styled(Paper)(() => ({
position: 'absolute',
zIndex:2,
right: 0,
top: 30,
width: 170,
display: 'none'
}))
use **MenuListProps** in the Menu component and use your menu **closeFunction** -
MenuListProps={{ onMouseLeave: handleClose }}
example-
<Menu
dense
id="demo-positioned-menu"
anchorEl={anchorEl}
open={open}
onClose={handleCloseMain}
title={item?.title}
anchorOrigin={{
vertical: "bottom",
horizontal: "right",
}}
transformOrigin={{
vertical: "top",
horizontal: "center",
}}
MenuListProps={{ onMouseLeave: handleClose }}
/>
I hope it will work perfectly.

Resources