Skip to content

Commit b8d9d85

Browse files
committed
fix(dialog): dialog content not being read out by screen readers
Currently we focus the first focusable element inside a dialog as per the accessibility recommendations, however moving focus to the first item causes some screen readers not to read out the rest of the dialog content. These changes switch to focusing the dialog container by default which allows screen readers to read out everything. If users press tab, they'll land on the first tabbable element anyways. The old behavior can still be opted into via the `autoFocus` option. Fixes #10591.
1 parent bc8fc75 commit b8d9d85

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

e2e/components/dialog-e2e.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('dialog', () => {
5959
element(by.id('default')).click();
6060

6161
await waitForDialog();
62-
expectFocusOn('mat-dialog-container input');
62+
expectFocusOn('mat-dialog-container');
6363
});
6464

6565
it('should restore focus to the element that opened the dialog', async () => {

src/lib/dialog/dialog-config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,11 @@ export class MatDialogConfig<D = any> {
9292
/** Aria label to assign to the dialog element */
9393
ariaLabel?: string | null = null;
9494

95+
// Note that this is disabled by default, because while the a11y recommendations are to focus
96+
// the first focusable element, doing so prevents screen readers from reading out the
97+
// rest of the dialog's content.
9598
/** Whether the dialog should focus the first focusable element on open. */
96-
autoFocus?: boolean = true;
99+
autoFocus?: boolean = false;
97100

98101
/**
99102
* Whether the dialog should restore focus to the

src/lib/dialog/dialog.spec.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -932,18 +932,32 @@ describe('MatDialog', () => {
932932
beforeEach(() => document.body.appendChild(overlayContainerElement));
933933
afterEach(() => document.body.removeChild(overlayContainerElement));
934934

935-
it('should focus the first tabbable element of the dialog on open', fakeAsync(() => {
935+
it('should focus the dialog container by default', fakeAsync(() => {
936936
dialog.open(PizzaMsg, {
937-
viewContainerRef: testViewContainerRef
937+
viewContainerRef: testViewContainerRef,
938938
});
939939

940940
viewContainerFixture.detectChanges();
941941
flushMicrotasks();
942942

943943
expect(document.activeElement!.tagName)
944-
.toBe('INPUT', 'Expected first tabbable element (input) in the dialog to be focused.');
944+
.toBe('MAT-DIALOG-CONTAINER', 'Expected dialog container to be focused.');
945945
}));
946946

947+
it('should focus the first tabbable element of the dialog on open when autoFocus is enabled',
948+
fakeAsync(() => {
949+
dialog.open(PizzaMsg, {
950+
viewContainerRef: testViewContainerRef,
951+
autoFocus: true
952+
});
953+
954+
viewContainerFixture.detectChanges();
955+
flushMicrotasks();
956+
957+
expect(document.activeElement!.tagName)
958+
.toBe('INPUT', 'Expected first tabbable element (input) in the dialog to be focused.');
959+
}));
960+
947961
it('should allow disabling focus of the first tabbable element', fakeAsync(() => {
948962
dialog.open(PizzaMsg, {
949963
viewContainerRef: testViewContainerRef,

0 commit comments

Comments
 (0)