Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/menu/js/menuController.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function MenuController($mdMenu, $attrs, $element, $scope, $mdUtil, $timeout, $r
};

this.focusMenuContainer = function focusMenuContainer() {
var focusTarget = menuContainer[0].querySelector('[md-menu-focus-target]');
var focusTarget = menuContainer[0].querySelector('[md-menu-focus-target], [md-autofocus]');
if (!focusTarget) focusTarget = menuContainer[0].querySelector('.md-button');
focusTarget.focus();
};
Expand Down
17 changes: 16 additions & 1 deletion src/components/menu/js/menuDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,22 @@
* <!-- menu-content -->
* </md-menu>
* </hljs>

*
* ### Auto Focus
* By default, when a menu opens, `md-menu` focuses the first button in the menu content.
*
* But sometimes you would like to focus another specific menu item instead of the first.<br/>
* This can be done by applying the `md-autofocus` directive on the given element.
*
* <hljs lang="html">
* <md-menu-item>
* <md-button md-autofocus ng-click="doSomething()">
* Auto Focus
* </md-button>
* </md-menu-item>
* </hljs>
*
*
* ### Preventing close
*
* Sometimes you would like to be able to click on a menu item without having the menu
Expand Down
2 changes: 1 addition & 1 deletion src/components/menu/js/menuServiceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function MenuProvider($$interimElementProvider) {
opts.menuContentEl[0].addEventListener('click', captureClickListener, true);

// kick off initial focus in the menu on the first element
var focusTarget = opts.menuContentEl[0].querySelector('[md-menu-focus-target]');
var focusTarget = opts.menuContentEl[0].querySelector('[md-menu-focus-target], [md-autofocus]');
if ( !focusTarget ) {
var firstChild = opts.menuContentEl[0].firstElementChild;

Expand Down
42 changes: 42 additions & 0 deletions src/components/menu/menu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,48 @@ describe('material.components.menu', function() {
expect(getOpenMenuContainer(menu).length).toBe(0);
}));

describe('autofocus', function() {

it('should focus a button with md-menu-focus-target', inject(function($compile, $rootScope, $document) {
var menu = $compile(
'<md-menu>' +
'<button ng-click="$mdOpenMenu($event)">Hello World</button>' +
'<md-menu-content>' +
'<md-menu-item>' +
'<button id="menuFocus" md-menu-focus-target ng-click="doSomething($event)"></button>' +
'</md-menu-item>' +
'</md-menu-content>' +
'</md-menu>'
)($rootScope);

openMenu(menu);

var menuTarget = $document[0].querySelector('#menuFocus');

expect(document.activeElement).toBe(menuTarget);
}));

it('should focus a button with md-autofocus', inject(function($compile, $rootScope, $document) {
var menu = $compile(
'<md-menu>' +
'<button ng-click="$mdOpenMenu($event)">Hello World</button>' +
'<md-menu-content>' +
'<md-menu-item>' +
'<button id="menuFocus" md-autofocus ng-click="doSomething($event)"></button>' +
'</md-menu-item>' +
'</md-menu-content>' +
'</md-menu>'
)($rootScope);

openMenu(menu);

var menuTarget = $document[0].querySelector('#menuFocus');

expect(document.activeElement).toBe(menuTarget);
}));

});

describe('closes with -', function() {
it('closes on normal option click', function() {

Expand Down
2 changes: 1 addition & 1 deletion src/core/util/autofocus.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ angular.module('material.core')
* @description
*
* `[md-autofocus]` provides an optional way to identify the focused element when a `$mdDialog`,
* `$mdBottomSheet`, or `$mdSidenav` opens or upon page load for input-like elements.
* `$mdBottomSheet`, `$mdMenu` or `$mdSidenav` opens or upon page load for input-like elements.
*
* When one of these opens, it will find the first nested element with the `[md-autofocus]`
* attribute directive and optional expression. An expression may be specified as the directive
Expand Down