Skip to content

Commit dad5922

Browse files
crisbetojelbourn
authored andcommitted
feat(dialog): add ariaLabel and focusOnOpen config options (#6558)
Based on the discussion on #6360 (comment), these changes add the ability to set the `aria-label` of a dialog, as well as the element that should be focus when the dialog is opened.
1 parent ea54edb commit dad5922

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/lib/dialog/dialog-config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ export class MatDialogConfig<D = any> {
8181
/** ID of the element that describes the dialog. */
8282
ariaDescribedBy?: string | null = null;
8383

84+
/** Aria label to assign to the dialog element */
85+
ariaLabel?: string | null = null;
86+
87+
/** Whether the dialog should focus the first focusable element on open. */
88+
autoFocus?: boolean = true;
8489

8590
// TODO(jelbourn): add configuration for lifecycle hooks, ARIA labelling.
8691
}

src/lib/dialog/dialog-container.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ export function throwMatDialogContentAlreadyAttachedError() {
7171
'class': 'mat-dialog-container',
7272
'tabindex': '-1',
7373
'[attr.role]': '_config?.role',
74-
'[attr.aria-labelledby]': '_ariaLabelledBy',
74+
'[attr.aria-labelledby]': '_config?.ariaLabel ? null : _ariaLabelledBy',
75+
'[attr.aria-label]': '_config?.ariaLabel',
7576
'[attr.aria-describedby]': '_config?.ariaDescribedBy || null',
7677
'[@slideDialog]': '_state',
7778
'(@slideDialog.start)': '_onAnimationStart($event)',
@@ -144,7 +145,9 @@ export class MatDialogContainer extends BasePortalOutlet {
144145
// If were to attempt to focus immediately, then the content of the dialog would not yet be
145146
// ready in instances where change detection has to run first. To deal with this, we simply
146147
// wait for the microtask queue to be empty.
147-
this._focusTrap.focusInitialElementWhenReady();
148+
if (this._config.autoFocus) {
149+
this._focusTrap.focusInitialElementWhenReady();
150+
}
148151
}
149152

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

src/lib/dialog/dialog.spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,18 @@ describe('MatDialog', () => {
788788
.toBe('INPUT', 'Expected first tabbable element (input) in the dialog to be focused.');
789789
}));
790790

791+
it('should allow disabling focus of the first tabbable element', fakeAsync(() => {
792+
dialog.open(PizzaMsg, {
793+
viewContainerRef: testViewContainerRef,
794+
autoFocus: false
795+
});
796+
797+
viewContainerFixture.detectChanges();
798+
flushMicrotasks();
799+
800+
expect(document.activeElement.tagName).not.toBe('INPUT');
801+
}));
802+
791803
it('should re-focus trigger element when dialog closes', fakeAsync(() => {
792804
// Create a element that has focus before the dialog is opened.
793805
let button = document.createElement('button');
@@ -930,6 +942,32 @@ describe('MatDialog', () => {
930942
}));
931943

932944
});
945+
946+
describe('aria-label', () => {
947+
it('should be able to set a custom aria-label', () => {
948+
dialog.open(PizzaMsg, {
949+
ariaLabel: 'Hello there',
950+
viewContainerRef: testViewContainerRef
951+
});
952+
viewContainerFixture.detectChanges();
953+
954+
const container = overlayContainerElement.querySelector('mat-dialog-container')!;
955+
expect(container.getAttribute('aria-label')).toBe('Hello there');
956+
});
957+
958+
it('should not set the aria-labelledby automatically if it has an aria-label', fakeAsync(() => {
959+
dialog.open(ContentElementDialog, {
960+
ariaLabel: 'Hello there',
961+
viewContainerRef: testViewContainerRef
962+
});
963+
viewContainerFixture.detectChanges();
964+
tick();
965+
viewContainerFixture.detectChanges();
966+
967+
const container = overlayContainerElement.querySelector('mat-dialog-container')!;
968+
expect(container.hasAttribute('aria-labelledby')).toBe(false);
969+
}));
970+
});
933971
});
934972

935973
describe('MatDialog with a parent MatDialog', () => {

0 commit comments

Comments
 (0)