Skip to content

Commit 557ef14

Browse files
author
ijz953
committed
feat(dialog): add ariaLabelledBy config option
this change adds the ability to set the `aria-labelledby` of a dialog. user can customize the `aria-labelledby` without mat-dialog-title. no breaking changes. existing functionality is preserved without setting the ariaLabelledBy config option.
1 parent e7b0e40 commit 557ef14

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

src/lib/dialog/dialog-config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ export class MatDialogConfig<D = any> {
8989
/** ID of the element that describes the dialog. */
9090
ariaDescribedBy?: string | null = null;
9191

92+
/** ID of the element that labels the dialog. */
93+
ariaLabelledBy?: string | null = null;
94+
9295
/** Aria label to assign to the dialog element */
9396
ariaLabel?: string | null = null;
9497

src/lib/dialog/dialog-container.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class MatDialogContainer extends BasePortalOutlet {
8787
_animationStateChanged = new EventEmitter<AnimationEvent>();
8888

8989
/** ID of the element that should be considered as the dialog's label. */
90-
_ariaLabelledBy: string | null = null;
90+
_ariaLabelledBy: string | null;
9191

9292
/** ID for the container DOM element. */
9393
_id: string;
@@ -101,6 +101,7 @@ export class MatDialogContainer extends BasePortalOutlet {
101101
public _config: MatDialogConfig) {
102102

103103
super();
104+
this._ariaLabelledBy = _config.ariaLabelledBy || null;
104105
}
105106

106107
/**

src/lib/dialog/dialog.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,51 @@ describe('MatDialog', () => {
11801180
}
11811181
});
11821182

1183+
describe('aria-labelledby', () => {
1184+
it('should be able to set a custom aria-labelledby', () => {
1185+
dialog.open(PizzaMsg, {
1186+
ariaLabelledBy: 'Labelled By',
1187+
viewContainerRef: testViewContainerRef
1188+
});
1189+
viewContainerFixture.detectChanges();
1190+
1191+
const container = overlayContainerElement.querySelector('mat-dialog-container')!;
1192+
expect(container.getAttribute('aria-labelledby')).toBe('Labelled By');
1193+
});
1194+
1195+
it('should not set the aria-labelledby automatically if it has an aria-label ' +
1196+
'and an aria-labelledby', fakeAsync(() => {
1197+
dialog.open(ContentElementDialog, {
1198+
ariaLabel: 'Hello there',
1199+
ariaLabelledBy: 'Labelled By',
1200+
viewContainerRef: testViewContainerRef
1201+
});
1202+
viewContainerFixture.detectChanges();
1203+
tick();
1204+
viewContainerFixture.detectChanges();
1205+
1206+
const container = overlayContainerElement.querySelector('mat-dialog-container')!;
1207+
expect(container.hasAttribute('aria-labelledby')).toBe(false);
1208+
}));
1209+
1210+
it('should set the aria-labelledby attribute to the config provided aria-labelledby ' +
1211+
'instead of the mat-dialog-title id', fakeAsync(() => {
1212+
dialog.open(ContentElementDialog, {
1213+
ariaLabelledBy: 'Labelled By',
1214+
viewContainerRef: testViewContainerRef
1215+
});
1216+
viewContainerFixture.detectChanges();
1217+
flush();
1218+
let title = overlayContainerElement.querySelector('[mat-dialog-title]')!;
1219+
let container = overlayContainerElement.querySelector('mat-dialog-container')!;
1220+
flush();
1221+
viewContainerFixture.detectChanges();
1222+
1223+
expect(title.id).toBeTruthy('Expected title element to have an id.');
1224+
expect(container.getAttribute('aria-labelledby')).toBe('Labelled By');
1225+
}));
1226+
});
1227+
11831228
describe('aria-label', () => {
11841229
it('should be able to set a custom aria-label', () => {
11851230
dialog.open(PizzaMsg, {

tools/public_api_guard/lib/dialog.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export declare class MatDialogClose implements OnInit, OnChanges {
5757
export declare class MatDialogConfig<D = any> {
5858
ariaDescribedBy?: string | null;
5959
ariaLabel?: string | null;
60+
ariaLabelledBy?: string | null;
6061
autoFocus?: boolean;
6162
backdropClass?: string;
6263
closeOnNavigation?: boolean;

0 commit comments

Comments
 (0)