Nav Popover not closing on leaving the button #130138
Replies: 1 comment 1 reply
-
To ensure that the popover closes when leaving the button, consider the following steps:
By using a single state and passing it down to the |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Select Topic Area
Bug
Body
I have a nav bar developed in Reactjs. In that submenus are there which is opening on popover, it is opening on button but not closing when leaving the button i have to drag the mouse to my popover compnent then if i drag outside it will close.
Part of Code:
`
export default function Nav({
openNav,
onCloseNav,
}) {
const pathname = usePathname();
const upLg = useResponsive('up', 'lg');
const { user, logout } = useAuth();
const [openDropdown, setOpenDropdown] = useState(null);
const [anchorEl, setAnchorEl] = useState(null);
const navigate = useNavigate();
const handlePopoverOpen = (event, title) => {
setAnchorEl(event.currentTarget);
setOpenDropdown(title);
};
const handlePopoverClose = () => {
setAnchorEl(null);
setOpenDropdown(null);
};
useEffect(() => {
if (openNav) {
onCloseNav();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pathname]);
const renderMenu = (<Stack component="nav" sx={{ paddingLeft: '4px', paddingRight: '4px' }}>
<Box component="ul" sx={{ padding: 0, display: 'flex', flexDirection: 'column', gap: '8px' }}>
{navConfig
.filter((item) => item.role === 0 || (item.role === 1 && user.role === 1))
.map((item) => item.children ? (<Box
key={item.title}
component="li"
sx={{ listStyle: 'none' }}
onMouseOver={(event) => handlePopoverOpen(event, item.title)}
onMouseOut={handlePopoverClose}
>
<Button
sx={{
borderRadius: '8px',
typography: 'body2',
color: 'grey.500',
textTransform: 'capitalize',
flexDirection: 'column',
alignItems: 'center',
width: '100%',
}}
>
<Box component="span" sx={{ display: 'flex', flexDirection: 'row', alignItems: 'center', mb: 1 }}>
<Box component="span" sx={{ width: 22, height: 22, ml: 2, color: 'grey.500' }}>
{item.icon}
<KeyboardArrowRightIcon sx={{ ml: 0.5, alignSelf: 'center', width: 18, height: 18 }} />
<Box component="span" sx={{ fontSize: '13px', textAlign: 'center' }}>{item.title}
<Popover
anchorEl={anchorEl}
open={openDropdown === item.title}
anchorOrigin={{
vertical: 'center', horizontal: 'right',
}}
transformOrigin={{
vertical: 'center', horizontal: 'left',
}}
disableRestoreFocus
PaperProps={{
onMouseOver: () => handlePopoverOpen(anchorEl, item.title),
onMouseOut: handlePopoverClose,
}}
>
{renderSubMenu(item.children)}
) : ())}
);
`
Can someone let me know what i am missing?
Beta Was this translation helpful? Give feedback.
All reactions