Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 10e4712

Browse files
devversionThomasBurleson
authored andcommitted
feat(menu): add support for md-autofocus attribute
* Currently, the most common components are supporting `md-autofocus`, except the `$mdMenu` component. `md-menu` did only support `md-menu-focus-target`, which was a unclear and undocumented attribute. * This commit adds support for the `md-autofocus` directive, to be consistent with the whole API. It also adds a documentation for the focus behavior in `md-menu`. Closes #7868. Closes #8469
1 parent 63e1c8e commit 10e4712

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

src/components/menu/js/menuController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function MenuController($mdMenu, $attrs, $element, $scope, $mdUtil, $timeout, $r
153153
};
154154

155155
this.focusMenuContainer = function focusMenuContainer() {
156-
var focusTarget = menuContainer[0].querySelector('[md-menu-focus-target]');
156+
var focusTarget = menuContainer[0].querySelector('[md-menu-focus-target], [md-autofocus]');
157157
if (!focusTarget) focusTarget = menuContainer[0].querySelector('.md-button');
158158
focusTarget.focus();
159159
};

src/components/menu/js/menuDirective.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,22 @@
102102
* <!-- menu-content -->
103103
* </md-menu>
104104
* </hljs>
105-
105+
*
106+
* ### Auto Focus
107+
* By default, when a menu opens, `md-menu` focuses the first button in the menu content.
108+
*
109+
* But sometimes you would like to focus another specific menu item instead of the first.<br/>
110+
* This can be done by applying the `md-autofocus` directive on the given element.
111+
*
112+
* <hljs lang="html">
113+
* <md-menu-item>
114+
* <md-button md-autofocus ng-click="doSomething()">
115+
* Auto Focus
116+
* </md-button>
117+
* </md-menu-item>
118+
* </hljs>
119+
*
120+
*
106121
* ### Preventing close
107122
*
108123
* Sometimes you would like to be able to click on a menu item without having the menu

src/components/menu/js/menuServiceProvider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ function MenuProvider($$interimElementProvider) {
210210
opts.menuContentEl[0].addEventListener('click', captureClickListener, true);
211211

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

src/components/menu/menu.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,48 @@ describe('material.components.menu', function() {
141141
expect(getOpenMenuContainer(menu).length).toBe(0);
142142
}));
143143

144+
describe('autofocus', function() {
145+
146+
it('should focus a button with md-menu-focus-target', inject(function($compile, $rootScope, $document) {
147+
var menu = $compile(
148+
'<md-menu>' +
149+
'<button ng-click="$mdOpenMenu($event)">Hello World</button>' +
150+
'<md-menu-content>' +
151+
'<md-menu-item>' +
152+
'<button id="menuFocus" md-menu-focus-target ng-click="doSomething($event)"></button>' +
153+
'</md-menu-item>' +
154+
'</md-menu-content>' +
155+
'</md-menu>'
156+
)($rootScope);
157+
158+
openMenu(menu);
159+
160+
var menuTarget = $document[0].querySelector('#menuFocus');
161+
162+
expect(document.activeElement).toBe(menuTarget);
163+
}));
164+
165+
it('should focus a button with md-autofocus', inject(function($compile, $rootScope, $document) {
166+
var menu = $compile(
167+
'<md-menu>' +
168+
'<button ng-click="$mdOpenMenu($event)">Hello World</button>' +
169+
'<md-menu-content>' +
170+
'<md-menu-item>' +
171+
'<button id="menuFocus" md-autofocus ng-click="doSomething($event)"></button>' +
172+
'</md-menu-item>' +
173+
'</md-menu-content>' +
174+
'</md-menu>'
175+
)($rootScope);
176+
177+
openMenu(menu);
178+
179+
var menuTarget = $document[0].querySelector('#menuFocus');
180+
181+
expect(document.activeElement).toBe(menuTarget);
182+
}));
183+
184+
});
185+
144186
describe('closes with -', function() {
145187
it('closes on normal option click', function() {
146188

src/core/util/autofocus.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ angular.module('material.core')
1313
* @description
1414
*
1515
* `[md-autofocus]` provides an optional way to identify the focused element when a `$mdDialog`,
16-
* `$mdBottomSheet`, or `$mdSidenav` opens or upon page load for input-like elements.
16+
* `$mdBottomSheet`, `$mdMenu` or `$mdSidenav` opens or upon page load for input-like elements.
1717
*
1818
* When one of these opens, it will find the first nested element with the `[md-autofocus]`
1919
* attribute directive and optional expression. An expression may be specified as the directive

0 commit comments

Comments
 (0)