Skip to content

Commit ba0b96e

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 4701c7b commit ba0b96e

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
@@ -1163,6 +1163,51 @@ describe('MatDialog', () => {
11631163
}
11641164
});
11651165

1166+
describe('aria-labelledby', () => {
1167+
it('should be able to set a custom aria-labelledby', () => {
1168+
dialog.open(PizzaMsg, {
1169+
ariaLabelledBy: 'Labelled By',
1170+
viewContainerRef: testViewContainerRef
1171+
});
1172+
viewContainerFixture.detectChanges();
1173+
1174+
const container = overlayContainerElement.querySelector('mat-dialog-container')!;
1175+
expect(container.getAttribute('aria-labelledby')).toBe('Labelled By');
1176+
});
1177+
1178+
it('should not set the aria-labelledby automatically if it has an aria-label ' +
1179+
'and an aria-labelledby', fakeAsync(() => {
1180+
dialog.open(ContentElementDialog, {
1181+
ariaLabel: 'Hello there',
1182+
ariaLabelledBy: 'Labelled By',
1183+
viewContainerRef: testViewContainerRef
1184+
});
1185+
viewContainerFixture.detectChanges();
1186+
tick();
1187+
viewContainerFixture.detectChanges();
1188+
1189+
const container = overlayContainerElement.querySelector('mat-dialog-container')!;
1190+
expect(container.hasAttribute('aria-labelledby')).toBe(false);
1191+
}));
1192+
1193+
it('should set the aria-labelledby attribute to the config provided aria-labelledby ' +
1194+
'instead of the mat-dialog-title id', fakeAsync(() => {
1195+
dialog.open(ContentElementDialog, {
1196+
ariaLabelledBy: 'Labelled By',
1197+
viewContainerRef: testViewContainerRef
1198+
});
1199+
viewContainerFixture.detectChanges();
1200+
flush();
1201+
let title = overlayContainerElement.querySelector('[mat-dialog-title]')!;
1202+
let container = overlayContainerElement.querySelector('mat-dialog-container')!;
1203+
flush();
1204+
viewContainerFixture.detectChanges();
1205+
1206+
expect(title.id).toBeTruthy('Expected title element to have an id.');
1207+
expect(container.getAttribute('aria-labelledby')).toBe('Labelled By');
1208+
}));
1209+
});
1210+
11661211
describe('aria-label', () => {
11671212
it('should be able to set a custom aria-label', () => {
11681213
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)