Skip to content

Instant menu closure #3515

Feb 27, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

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
    o…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@alekseykurylev
Comment options

Answer selected by alekseykurylev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants