Skip to content

fix(dialog): move focus into container if no focusable elements are found #6524

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2017
Merged
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
9 changes: 8 additions & 1 deletion src/lib/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export function throwMdDialogContentAlreadyAttachedError() {
],
host: {
'class': 'mat-dialog-container',
'tabindex': '-1',
'[attr.role]': '_config?.role',
'[attr.aria-labelledby]': '_ariaLabelledBy',
'[attr.aria-describedby]': '_config?.ariaDescribedBy || null',
Expand Down Expand Up @@ -155,7 +156,13 @@ export class MdDialogContainer extends BasePortalHost {
// If were to attempt to focus immediately, then the content of the dialog would not yet be
// ready in instances where change detection has to run first. To deal with this, we simply
// wait for the microtask queue to be empty.
this._focusTrap.focusInitialElementWhenReady();
this._focusTrap.focusInitialElementWhenReady().then(hasMovedFocus => {
// If we didn't find any focusable elements inside the dialog, focus the
// container so the user can't tab into other elements behind it.
if (!hasMovedFocus) {
this._elementRef.nativeElement.focus();
}
});
}

/** Restores focus to the element that was focused before the dialog opened. */
Expand Down
1 change: 1 addition & 0 deletions src/lib/dialog/dialog.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ $mat-dialog-button-margin: 8px !default;
box-sizing: border-box;
overflow: auto;
max-width: $mat-dialog-max-width;
outline: 0;

// The dialog container should completely fill its parent overlay element.
width: 100%;
Expand Down
20 changes: 18 additions & 2 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,17 @@ describe('MdDialog', () => {
document.body.removeChild(input);
}));

it('should move focus to the container if there are no focusable elements in the dialog',
fakeAsync(() => {
dialog.open(DialogWithoutFocusableElements);

viewContainerFixture.detectChanges();
flushMicrotasks();

expect(document.activeElement.tagName)
.toBe('MD-DIALOG-CONTAINER', 'Expected dialog container to be focused.');
}));

});

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

@Component({template: '<p>Pasta</p>'})
class DialogWithoutFocusableElements {}

// Create a real (non-test) NgModule as a workaround for
// https://github.com/angular/angular/issues/10760
const TEST_DIRECTIVES = [
Expand All @@ -901,7 +915,8 @@ const TEST_DIRECTIVES = [
DirectiveWithViewContainer,
ComponentWithOnPushViewContainer,
ContentElementDialog,
DialogWithInjectedData
DialogWithInjectedData,
DialogWithoutFocusableElements
];

@NgModule({
Expand All @@ -912,7 +927,8 @@ const TEST_DIRECTIVES = [
ComponentWithChildViewContainer,
PizzaMsg,
ContentElementDialog,
DialogWithInjectedData
DialogWithInjectedData,
DialogWithoutFocusableElements,
],
})
class DialogTestModule { }