Skip to content

Commit 476cbfa

Browse files
committed
fix(dialog): move focus into container if no focusable elements are found
Moves focus onto the `md-dialog-container`, if it doesn't contain any focusable elements. The reasoning behind this change is that not moving focus at all will leave the behind on the dialog trigger, allowing them to be able to tab behind the dialog.
1 parent 72360e2 commit 476cbfa

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/lib/dialog/dialog-container.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export function throwMdDialogContentAlreadyAttachedError() {
6666
],
6767
host: {
6868
'class': 'mat-dialog-container',
69+
'tabindex': '-1',
6970
'[attr.role]': '_config?.role',
7071
'[attr.aria-labelledby]': '_ariaLabelledBy',
7172
'[attr.aria-describedby]': '_config?.ariaDescribedBy || null',
@@ -155,7 +156,13 @@ export class MdDialogContainer extends BasePortalHost {
155156
// If were to attempt to focus immediately, then the content of the dialog would not yet be
156157
// ready in instances where change detection has to run first. To deal with this, we simply
157158
// wait for the microtask queue to be empty.
158-
this._focusTrap.focusInitialElementWhenReady();
159+
this._focusTrap.focusInitialElementWhenReady().then(hasMovedFocus => {
160+
// If we didn't find any focusable elements inside the dialog, focus the
161+
// container so the user can't tab into other elements behind it.
162+
if (!hasMovedFocus) {
163+
this._elementRef.nativeElement.focus();
164+
}
165+
});
159166
}
160167

161168
/** Restores focus to the element that was focused before the dialog opened. */

src/lib/dialog/dialog.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ $mat-dialog-button-margin: 8px !default;
1717
box-sizing: border-box;
1818
overflow: auto;
1919
max-width: $mat-dialog-max-width;
20+
outline: 0;
2021

2122
// The dialog container should completely fill its parent overlay element.
2223
width: 100%;

src/lib/dialog/dialog.spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,17 @@ describe('MdDialog', () => {
679679
document.body.removeChild(input);
680680
}));
681681

682+
it('should move focus to the container if there are no focusable elements in the dialog',
683+
fakeAsync(() => {
684+
dialog.open(DialogWithoutFocusableElements);
685+
686+
viewContainerFixture.detectChanges();
687+
flushMicrotasks();
688+
689+
expect(document.activeElement.tagName)
690+
.toBe('MD-DIALOG-CONTAINER', 'Expected dialog container to be focused.');
691+
}));
692+
682693
});
683694

684695
describe('dialog content elements', () => {
@@ -893,6 +904,9 @@ class DialogWithInjectedData {
893904
constructor(@Inject(MD_DIALOG_DATA) public data: any) { }
894905
}
895906

907+
@Component({template: '<p>Pasta</p>'})
908+
class DialogWithoutFocusableElements {}
909+
896910
// Create a real (non-test) NgModule as a workaround for
897911
// https://github.com/angular/angular/issues/10760
898912
const TEST_DIRECTIVES = [
@@ -901,7 +915,8 @@ const TEST_DIRECTIVES = [
901915
DirectiveWithViewContainer,
902916
ComponentWithOnPushViewContainer,
903917
ContentElementDialog,
904-
DialogWithInjectedData
918+
DialogWithInjectedData,
919+
DialogWithoutFocusableElements
905920
];
906921

907922
@NgModule({
@@ -912,7 +927,8 @@ const TEST_DIRECTIVES = [
912927
ComponentWithChildViewContainer,
913928
PizzaMsg,
914929
ContentElementDialog,
915-
DialogWithInjectedData
930+
DialogWithInjectedData,
931+
DialogWithoutFocusableElements,
916932
],
917933
})
918934
class DialogTestModule { }

0 commit comments

Comments
 (0)