feat(menu): click trigger again to close menu content
Hi everyone!
I use md-menu. But there is one thing that may be improved: When click on menu button for open content --> content is opened --> as a user I want to click on this button one more time and expect to close menu content.
I looking at this:
// Use the $mdMenu interim element service to close the menu contents
this.close = function closeMenu(skipFocus) {
ctrl.isOpen = false;
triggerElement.setAttribute('aria-expanded', 'false');
$mdMenu.hide();
if (!skipFocus) {
$element.children()[0].focus();
}
};
How can I use it? And maybe it's possible to add closing content (when it opened and user click on menu button one more time)?
@lillylykho - Currently the default menu display overlays the trigger button; so you cannot click on it. And clicking anywhere off the menu will close it. Is this not sufficient?
I will second this request. In our desktop app, I want to be able to close the menu when mdMenu has a mouseleave. User moves away, menu goes away. No click needed. This is standard dropdown menu behavior.
As for the default behavior, in our app, I moved the menu so the top item is NOT hidden; I prefer my users be able to see what item led to the drop down menu. It would be nice if I could have it with a toggle behavior such that "if open, close...if close, open" but since there is only the ability to open, I can't do it via material.
Exposing the close to $scope would make any situation possible and it is just one line:
$scope.mdCloseMenu = this.close;
@ThomasBurleson - By default it is really sufficient and expected. But Material has position mode properties for md-menu. I used md-offset and moved menu-container so as not to display overlays, because my users need to be able to see item led to the drop down menu.
If we have ability to move menu container, so why we can not click on this item one more time and close the menu?
@rkotenko - I agree with you. It would be great to have extra toggle behavior "open-close" in md-menu
Thanks!
lillylykho -
In the meantime, you can expose the functionality yourself:
angular.module('material.components.menu')
// the trick here is to override the existing directive and add to it via link.
.directive('mdMenu', function() {
return {
require: '^mdMenu',
link: function(scope, elm, attr, ctrl) {
// now I can expose what I need to the scope
scope.$mdCloseMenu = ctrl.close;
}
}
});
@rkotenko I'll try this, thanks!
This is outside the scope of the Material Design spec and an additional detail we can't address in 1.0. We'd be happy to consider a PR post-1.0 if you'd be interested in contributing to the project.
Really strange to have an open function but no close function...
I just discovered you can close a menu by injecting $mdMenu in your controller and calling $mdMenu.hide();
~~However this is not documented (honestly, the whole mdMenuService is not). And, by calling this method in the trigger element's mouseleave (as @rkotenko I'm trying to hide the menu on mouse leave) is not feasible, since the hide should be called on the menu's mouseleave.~~
EDIT: forget about the above, of course ng-mouseleave should be added to the <md-menu-content>, not the <md-button> that opens the menu. Adding it to the <md-menu-content> does the trick.
1.0.0-rc6
I have an md-menu with a menu item that opens a dialog with $mdDialog. The menu is still open and sits ontop of the dialog. It would be handy to be able to call a close/hide function on the menu before calling $mdDialog.show. It'd be even better if the menu just closed on its own before the dialog is shown.
The previous comment suggesting $mdMenu.hide() isn't working for me.
12/9/2015 UPDATE: I no longer have this particular problem in 1.0.0-rc7
@opadillaintegrify am facing the same problem. have you any workaround please?
I have no workaround. Sorry. It just worked for me after 1.0.0-rc7. Still working for me in 1.0.6
As @Lorthirk mentioned, you can use $mdMenu.hide() in your controller (don't forget to inject $mdMenu.
This works for me:
ng-click="$mdOpenMenu(); AppCtrl.closeMdMenu();"
AppCtrl
vm.closeMdMenu = function() { $mdMenu.hide(); };
The close menu function does not activate until the 2nd click as the menu is not yet in it's open state.
@rkotenko Thank you! That worked perfectly for me. with the exposed I could use ng-mouseleave="$mdCloseMenu()" in the md-menu tag.
EDIT: I has having a problem, the
Did anyone manage to get this working with mouseenter and mouseleave, on hover instead of on click?
I am using this on a md-sidenav and I have a submenus which I wanna pop open on hover not on click (for better UX)
Has anyone done that?
@ForkInSpace Please try Stack Overflow, Gitter, or the AngularJS Material forums for asking for help.
Please limit GitHub issues to bug reports and enhancement requests.