[menu] Support cascading / nested menu
Hello,
I wrote some two advanced components, who can handle Submenuitems. I defined an anchorElement, which will be passed as property to the Mui Menu Component. Is there a way to define where the Popup Element should be displayed? Because now it will just overlap. Maybe I want to calculate it more left or more right.
Here is an demo: https://codesandbox.io/s/9ywowy18k4
Benchmark
- https://blueprintjs.com/docs/#core/components/menu
- https://reakit.io/docs/menu/#submenu
- https://www.telerik.com/kendo-react-ui/components/layout/menu/
- https://www.w3.org/TR/wai-aria-practices/examples/menubar/menubar-1/menubar-1.html
- https://ant.design/components/dropdown/#components-dropdown-demo-sub-menu
@janhoeck This is a nice demo! You can use the anchor playground to pick the right values. Here is an example: https://codesandbox.io/s/lx1336xrx7

Do you want to add such example in the documentation? 😄
Thanks for your answer! Its working too when I align my button to the right. I thought the sub menu item will overlap. But it won't. Here is what I mean: https://codesandbox.io/s/x99r38x6rz
Sure, I can add such an example in the documentation. How I do that? 👍 Maybe we can implement a sub menu component in the core code. I think some people would like this!
How I do that?
:) https://github.com/mui-org/material-ui/blob/master/CONTRIBUTING.md#how-do-i-add-a-new-demo-in-the-documentation
Maybe we can implement a sub menu component in the core code. I think some people would like this!
Looking at your demo on codesandbox, the submenu implementation seems already straightforward. What abstraction do we miss?
WOOOOW cool!
@oliviertassinari Uhm just implement a SubMenu Component in the core code so that the user does not have to write it on his own :). Its just an idea :).
I think I have time to add this to the documentation at the evening :)
Uhm @oliviertassinari I don't get material-ui running locally. But I removed mobx from the demo and use the normal setState functions. Soooo maybe you could just copy/paste it in the docs?
Demo: https://codesandbox.io/s/l7wm3rjxzm
menu.md Text:
Sub menus
If you want to have a menu with a sub menu tree (for example you want to add a "others" item as the last item of the parent menu)
then you can use the anchorElement function combining with Menu and MenuItem.
The EnhancedMenu is a wrapper for the Menu. The EnhancedMenu will iterate over the menuItems prop
and create a normal MenuItem or a new Menu with an anchorElement.
{{"demo": "pages/demos/menus/subMenu/Demo.js"}}
I don't get material-ui running locally
@janhoeck Are you using yarn? Otherwise, you need to npm install each package. Thanks for simplifying the codesandbox! Also, if you could open a pull-request with any changes, I could update it to match your previous comment: https://github.com/mui-org/material-ui/issues/11723#issuecomment-395300458.
@oliviertassinari FYI: Until now I did not find time to document it :/ And tomorrow I am 3 days on a business trip.
Guys, Please update this. Thank you!!
@janhoeck Any idea how to enhance/correct the following issues with the SubMenu component you implemented?
-
After expanding a SubMenu, and clicking away from the expanded SubMenu, it will only close the current SubMenu. This seems right, if "clicking away" means clicking its parent. But, the natural thing to do when clicking completely away is to dismiss everything. Consider a deeply nested submenu, where you intentionally need to "click away" N number of times in order to dismiss every menu window.
-
Somewhat related to (1), but "clicking away" on another parent menu item that has a SubMenu should dismiss the current SubMenu and go ahead and open up the new SubMenu. I suspect figuring out focus would be difficult. See Office UI Fabric's ContextualMenu. They can even open things onMouseOver.
@cobbs-totem You can define a boolean in a ContainerComponent. This boolean will be passed into all submenus. When you open a menu, set the boolean value to true. After that check whenever you click away and set this boolean to false. If the boolean is false, than all menus should react to that and closed.
Thanks for the feedback! If I understand your proposal, I think the setback there is that clicking away only generates an event for the deepest child menu. So, the process would have to go in reverse (child onClose() calls props.onClose() to propagate the change upward to parents). This strategy will work when clicking away from all menus, as they will all get notified eventually, and all close out. However, if you click away from a child by clicking on the parent, the child gets the onClose() and tells all parents to close, including the parent in which you clicked inside. I haven't found a workaround for that.
Just check in which container the user clicked. If the container is something else than a menu, then close all menus. And when the user clicked on a menu, than close all child menus of the menu
@0maxxam0 funded this issue with $20. See it on IssueHunt
@0maxxam0 funded this issue with $20. See it on IssueHunt
The task is already completed. Here I have created a new sandbox for you: https://codesandbox.io/s/509wvp6qxk
In the examples above, I am having trouble getting keyboard traversal to work in the nested menu. Any suggestions?
Hi,
does it support close all the sub-menus and menu when clicking outside of it? Right now, we need to click 2 times to close the whole menu.
Also, is possible to close all the sub-menu/menu when a user click on an item?
Thank you!
@Jak3b0 You can do it with the ClickAwayListener. Take a look https://codesandbox.io/s/6voj4o458n
Thanks Jan!
On Fri, Jan 25, 2019 at 7:12 AM Jan Höck [email protected] wrote:
@Jak3b0 https://github.com/Jak3b0 You can do it with the ClickAwayListener. Take a look https://codesandbox.io/s/6voj4o458n
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mui-org/material-ui/issues/11723#issuecomment-457554390, or mute the thread https://github.com/notifications/unsubscribe-auth/ACtpmYf6fW-oSYXokZ59DezpUaKzt3LKks5vGvSzgaJpZM4Ua2JD .
Can I take this up?