material icon indicating copy to clipboard operation
material copied to clipboard

feat(menu): click trigger again to close menu content

Open lillylykho opened this issue 11 years ago • 15 comments

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 avatar Jul 13 '15 08:07 lillylykho

@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?

ThomasBurleson avatar Jul 15 '15 16:07 ThomasBurleson

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;

rkotenko avatar Jul 15 '15 20:07 rkotenko

@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 avatar Jul 16 '15 06:07 lillylykho

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 avatar Jul 16 '15 12:07 rkotenko

@rkotenko I'll try this, thanks!

lillylykho avatar Jul 20 '15 06:07 lillylykho

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.

naomiblack avatar Sep 04 '15 18:09 naomiblack

Really strange to have an open function but no close function...

jswhisperer avatar Sep 23 '15 14:09 jswhisperer

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.

lorthirk avatar Sep 25 '15 13:09 lorthirk

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

ghost avatar Dec 08 '15 19:12 ghost

@opadillaintegrify am facing the same problem. have you any workaround please?

raza2022 avatar Mar 08 '16 14:03 raza2022

I have no workaround. Sorry. It just worked for me after 1.0.0-rc7. Still working for me in 1.0.6

ghost avatar Mar 08 '16 20:03 ghost

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.

craigivemy avatar Sep 07 '16 10:09 craigivemy

@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 events didn't include the so when entering menu-content the md-menu activates the mouseleave. I also tried managing the events with local variables for knowing when the and the where on hover o leaved. but other issues appear... I couldn't find a solution for opening the menus without having to click that didn't have bugs or issues. I couldn't make it work perfectly. so I decided to just leave it like it works by default. it was taking too long for such simple feature. they should consider this.

SantiMe1 avatar Oct 06 '16 19:10 SantiMe1

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 avatar Apr 07 '19 08:04 ForkInSpace

@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.

Splaktar avatar May 10 '19 20:05 Splaktar