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

Commit bcfe00a

Browse files
devversionjelbourn
authored andcommitted
fix(dialog): focus dialog element when no actions are set (#9272)
By default the dialog searches for possible focus targets. Using the `md-autofocus` directive and querying for buttons inside of the `md-dialog-actions` element. Some developers don't have any actions inside of the dialog, so there will be no focus target and Screenreaders are not able to detect the new change. The dialog should focus the actual dialog element when no focus target is available. Users are still able to disable the auto focus (as same as before) by using the `focusOnOpen` option. > `mockElementFocus` now mocks the HTMLElement prototype instead of the jqLite prototype. > This allows us to test focus on native HTML elements as well. Fixes #9271.
1 parent f170133 commit bcfe00a

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/components/dialog/dialog.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ function MdDialogProvider($$interimElementProvider) {
761761
*/
762762
function focusOnOpen() {
763763
if (options.focusOnOpen) {
764-
var target = $mdUtil.findFocusTarget(element) || findCloseButton();
764+
var target = $mdUtil.findFocusTarget(element) || findCloseButton() || dialogElement;
765765
target.focus();
766766
}
767767

@@ -773,11 +773,13 @@ function MdDialogProvider($$interimElementProvider) {
773773
*/
774774
function findCloseButton() {
775775
var closeButton = element[0].querySelector('.dialog-close');
776+
776777
if (!closeButton) {
777778
var actionButtons = element[0].querySelectorAll('.md-actions button, md-dialog-actions button');
778779
closeButton = actionButtons[actionButtons.length - 1];
779780
}
780-
return angular.element(closeButton);
781+
782+
return closeButton;
781783
}
782784
}
783785
}

src/components/dialog/dialog.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,24 @@ describe('$mdDialog', function() {
11341134
expect($document.activeElement).toBe(parent[0].querySelector('#focus-target'));
11351135
}));
11361136

1137+
it('should focus the dialog element if no actions are set', inject(function($mdDialog, $rootScope, $document) {
1138+
jasmine.mockElementFocus(this);
1139+
1140+
var parent = angular.element('<div>');
1141+
1142+
$mdDialog.show({
1143+
parent: parent,
1144+
template:
1145+
'<md-dialog></md-dialog>'
1146+
});
1147+
1148+
$rootScope.$apply();
1149+
runAnimation();
1150+
1151+
expect($document.activeElement).toBe(parent[0].querySelector('md-dialog'));
1152+
1153+
}));
1154+
11371155
it('should focusOnOpen == false', inject(function($mdDialog, $rootScope, $document, $timeout, $mdConstant) {
11381156
jasmine.mockElementFocus(this);
11391157

test/angular-material-spec.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@
9191
});
9292

9393
/**
94-
* Mocks angular.element#focus ONLY for the duration of a particular test.
94+
* Mocks the focus method from the HTMLElement prototype for the duration
95+
* of the running test.
96+
*
97+
* The mock will be automatically removed after the test finished.
9598
*
9699
* @example
97100
*
@@ -105,17 +108,20 @@
105108
* }));
106109
*
107110
*/
108-
jasmine.mockElementFocus = function(test) {
109-
var focus = angular.element.prototype.focus;
111+
jasmine.mockElementFocus = function() {
112+
var _focusFn = HTMLElement.prototype.focus;
113+
110114
inject(function($document) {
111-
angular.element.prototype.focus = function() {
112-
$document.activeElement = this[0];
115+
HTMLElement.prototype.focus = function() {
116+
$document.activeElement = this;
113117
};
114118
});
119+
115120
// Un-mock focus after the test is done
116121
afterEach(function() {
117-
angular.element.prototype.focus = focus;
122+
HTMLElement.prototype.focus = _focusFn;
118123
});
124+
119125
};
120126

121127
/**

0 commit comments

Comments
 (0)