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

Commit e64875d

Browse files
Splaktarmmalerba
authored andcommitted
fix(nav-bar): improve focus behavior for click events (#11600)
<!-- Filling out this template is required! Do not delete it when submitting a Pull Request! Without this information, your Pull Request may be auto-closed. --> ## PR Checklist Please check that your PR fulfills the following requirements: - [x] The commit message follows [our guidelines](https://github.com/angular/material/blob/master/.github/CONTRIBUTING.md#-commit-message-format) - [x] Tests for the changes have been added or this is not a bug fix / enhancement - [x] Docs have been added, updated, or were not required ## PR Type What kind of change does this PR introduce? <!-- Please check the one that applies to this PR using "x". --> ``` [x] Bugfix [ ] Enhancement [ ] Documentation content changes [ ] Code style update (formatting, local variables) [ ] Refactoring (no functional changes, no api changes) [ ] Build related changes [ ] CI related changes [ ] Infrastructure changes [ ] Other... Please describe: ``` ## What is the current behavior? Clicking on nav items with a mouse causes the focus to move to the previously clicked nav item instead of the newly clicked nav item. <!-- Please describe the current behavior that you are modifying and link to one or more relevant issues. --> Issue Number: Fixes #11591. Relates to #11494. Closes #11598. ## What is the new behavior? - Focus is moved to the new nav item after it is clicked. - remove hover style that is inconsistent with spec - add test from @codymikol ## Does this PR introduce a breaking change? ``` [ ] Yes [x] No ``` <!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. --> <!-- Note that breaking changes are highly unlikely to get merged to master unless the validation is clear and the use case is critical. --> ## Other information Thank you to @codymikol for doing the initial investigation into this regression!
1 parent a191a8e commit e64875d

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/components/navBar/navBar.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,9 @@ function MdNavBar($mdAria, $mdTheming) {
114114

115115
/**
116116
* Controller for the nav-bar component.
117-
*
118-
* TODO update this with a link to tablist when that implementation gets merged.
119-
* Accessibility functionality is implemented as a site navigator with a listbox, according to
120-
* https://www.w3.org/TR/wai-aria-practices/#Site_Navigator_Tabbed_Style.
117+
* Accessibility functionality is implemented as a tablist
118+
* (https://www.w3.org/TR/wai-aria-1.0/complete#tablist) and
119+
* tabs (https://www.w3.org/TR/wai-aria-1.0/complete#tab).
121120
*
122121
* @param {!angular.JQLite} $element
123122
* @param {!angular.Scope} $scope
@@ -196,6 +195,7 @@ MdNavBarController.prototype._initTabs = function() {
196195
MdNavBarController.prototype._updateTabs = function(newValue, oldValue) {
197196
var self = this;
198197
var tabs = this._getTabs();
198+
var sameTab = newValue === oldValue;
199199

200200
// this._getTabs can return null if nav-bar has not yet been initialized
201201
if (!tabs) return;
@@ -217,6 +217,11 @@ MdNavBarController.prototype._updateTabs = function(newValue, oldValue) {
217217

218218
this._$timeout(function() {
219219
self._updateInkBarStyles(newTab, newIndex, oldIndex);
220+
// Don't change focus when there is no newTab, the new and old tabs are the same, or when
221+
// called from MdNavBarController._initTabs() which would have no oldTab defined.
222+
if (newTab && oldTab && !sameTab) {
223+
self._moveFocus(oldTab, newTab);
224+
}
220225
});
221226
};
222227

@@ -596,6 +601,9 @@ function MdNavItem($mdAria, $$rAF, $mdUtil, $window) {
596601
});
597602

598603
navButton.on('click', function() {
604+
// This triggers a watcher on mdNavBar.mdSelectedNavItem which calls
605+
// MdNavBarController._updateTabs() after a $timeout. That function calls
606+
// MdNavItemController.setSelected() for the old tab with false and the new tab with true.
599607
mdNavBar.mdSelectedNavItem = mdNavItem.name;
600608
scope.$apply();
601609
});

src/components/navBar/navBar.scss

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ $md-nav-bar-height: 48px;
3434
&:focus {
3535
outline: none;
3636
}
37-
38-
&:hover {
39-
background-color: inherit;
40-
}
41-
4237
}
4338

4439
md-nav-ink-bar {

src/components/navBar/navBar.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,17 @@ describe('mdNavBar', function() {
183183
expect($scope.selectedTabRoute).toBe('tab2');
184184
});
185185

186+
it('should add the md-focused class when focused', function () {
187+
$scope.selectedTabRoute = 'tab1';
188+
createTabs();
189+
var tab2Ctrl = getTabCtrl('tab2');
190+
angular.element(tab2Ctrl.getButtonEl()).triggerHandler('focus');
191+
angular.element(tab2Ctrl.getButtonEl()).triggerHandler('click');
192+
$scope.$apply();
193+
$timeout.flush();
194+
expect(tab2Ctrl.getButtonEl().classList.contains('md-focused')).toBe(true);
195+
});
196+
186197
it('adds ui-sref-opts attribute to nav item when sref-opts attribute is ' +
187198
'defined', function() {
188199
create(

0 commit comments

Comments
 (0)