Instant menu closure #3515
Answered
by
diegohaz
alekseykurylev
asked this question in
Questions
-
How can I instantly close the drop-down menu if I hover over the adjacent menu, provided that I use the <Menubar>
<MenuProvider hideTimeout={250}>
<MenuButton showOnHover>Item menu</MenuButton>
<Menu hideOnHoverOutside portal unmountOnHide>
<MenuItem focusOnHover={false}>Undo</MenuItem>
<MenuItem focusOnHover={false}>Redo</MenuItem>
</Menu>
</MenuProvider>
<MenuProvider hideTimeout={250}>
<MenuButton showOnHover>Item menu</MenuButton>
<Menu hideOnHoverOutside portal unmountOnHide>
<MenuItem focusOnHover={false}>Undo</MenuItem>
<MenuItem focusOnHover={false}>Redo</MenuItem>
</Menu>
</MenuProvider>
</Menubar> hideTimeout.mov |
Beta Was this translation helpful? Give feedback.
Answered by
diegohaz
Feb 27, 2024
Replies: 1 comment 1 reply
-
The ideal solution depends on your concrete use case, but generally you can control the open menu with a single state: const [menu, setMenu] = useState("");
const isOpen = (label) => menu === label;
const setOpen = (label) => (open) => {
if (open) {
setMenu(label);
} else if (label === menu) {
setMenu("");
}
};
<Menubar>
<MenuProvider
open={isOpen("first")}
setOpen={setOpen("first")}
hideTimeout={250}
>
<MenuButton showOnHover>Item menu</MenuButton>
<Menu hideOnHoverOutside portal unmountOnHide>
<MenuItem focusOnHover={false}>Undo</MenuItem>
<MenuItem focusOnHover={false}>Redo</MenuItem>
</Menu>
</MenuProvider>
<MenuProvider
open={isOpen("second")}
setOpen={setOpen("second")}
hideTimeout={250}
>
<MenuButton showOnHover>Item menu</MenuButton>
<Menu hideOnHoverOutside portal unmountOnHide>
<MenuItem focusOnHover={false}>Undo</MenuItem>
<MenuItem focusOnHover={false}>Redo</MenuItem>
</Menu>
</MenuProvider>
</Menubar> |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
alekseykurylev
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The ideal solution depends on your concrete use case, but generally you can control the open menu with a single state: