question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[MenuItem] Menu closes when clicking on a MenuItemOption

See original GitHub issue

When you have a MenuOptionGroup of type=“radio” inside a Menu, clicking on any of this options will make the menu to close. I’d like to give my users some kind of “change” feedback, when selecting this options. Same as closeOnSelect property of Menu component, I’d like to have a closeOnSelect for MenuItem and MenuItemOption components, so I can choose which Items should close the menu on selection, and which items should keep the menu open.

Here’s a small sandbox showing this behavior with a “language selection” example: I’d like to keep the menu open when clicking on ‘Spanish’ or ‘English’, so the users have instant feedback on this change.

https://codesandbox.io/s/small-paper-exgwi?file=/src/App.tsx

I am aware of some workarounds to make this work, but it will be great if this was supported by Chakra with a property on the MenuItem components.

Thanks in advance! 😄

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
franky47commented, Mar 29, 2021

@with-heart if the solution proposed in my previous comment is acceptable, I can open a PR.

Edit: that being said, there could be a more declarative way of doing this (working with props rather than events), by allowing MenuItem and MenuItemOption to pass a closeOnSelect that overrides the parent Menu’s value.

Eg:

<Menu 
  closeOnSelect={true} // default value explicitly added for clarity
>
  {({ onClose }) => (
    <>
      <MenuButton>Menu</MenuButton>
      <MenuList>
        <MenuItem>This item closes the menu when clicked</MenuItem>
        <MenuItem
          closeOnSelect={false}
          onClick={() => {
            setTimeout(onClose, 1000)
          }}
        >
          This one waits 1 second, then closes it
        </MenuItem>
      </MenuList>
    </>
  )}
</Menu>

Implementation in useMenuItem (works by proxy for MenuItemOption):

  const onClick = React.useCallback(
    (event: React.MouseEvent) => {
      onClickProp?.(event)
      /**
       * Close menu and parent menu's if `closeOnSelect` is set to `true`
+     * or if the menu item explicitly asked not to close on select:
       */
-      if (closeOnSelect) {
+      if (closeOnSelect && props.closeOnSelect !== false) {
        onClose()
      }
    },
-    [onClose, onClickProp, closeOnSelect],
+    [onClose, onClickProp, closeOnSelect, props.closeOnSelect],
  )
2reactions
segunadebayocommented, Apr 7, 2021

That’s a great idea @franky47, thanks for sharing your thoughts.

I think it makes sense to be able to control closeOnSelect per MenuItem. Feel free to create a PR for this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent Menu From Closing When User Clicks the Input
Currently, if I click inside the input, the menu just closes. I have the same setup as the original poster. I've tried playing...
Read more >
Menu - Chakra UI
Chakra UI exports 8 components for rendering menus: ... MenuItemOption : The checkable menu item, to be used with MenuOptionGroup . ... Open...
Read more >
Menu - Chakra UI
MenuOptionGroup : A wrapper for checkable menu items (radio and checkbox). MenuItemOption : The checkable menu item, to be used with MenuOptionGroup ....
Read more >
Prevent Menu from closing when clicking a MenuItem in Quick ...
I'm trying to create a nested menu, which has multiple MenuItems as ... issue: Whenever a MenuItem is clicked, the whole menu is...
Read more >
MenuItem - NW.js Documentation
MenuItem represents an item in a menu. A MenuItem can be a separator or a normal item which has label and icon or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found