Skip to content

feat(dialog): add ariaLabelledBy config option #14943

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
Feb 5, 2019
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
3 changes: 3 additions & 0 deletions src/lib/dialog/dialog-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ export class MatDialogConfig<D = any> {
/** ID of the element that describes the dialog. */
ariaDescribedBy?: string | null = null;

/** ID of the element that labels the dialog. */
ariaLabelledBy?: string | null = null;

/** Aria label to assign to the dialog element */
ariaLabel?: string | null = null;

Expand Down
3 changes: 2 additions & 1 deletion src/lib/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class MatDialogContainer extends BasePortalOutlet {
_animationStateChanged = new EventEmitter<AnimationEvent>();

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

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

super();
this._ariaLabelledBy = _config.ariaLabelledBy || null;
}

/**
Expand Down
45 changes: 45 additions & 0 deletions src/lib/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,51 @@ describe('MatDialog', () => {
}
});

describe('aria-labelledby', () => {
it('should be able to set a custom aria-labelledby', () => {
dialog.open(PizzaMsg, {
ariaLabelledBy: 'Labelled By',
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();

const container = overlayContainerElement.querySelector('mat-dialog-container')!;
expect(container.getAttribute('aria-labelledby')).toBe('Labelled By');
});

it('should not set the aria-labelledby automatically if it has an aria-label ' +
'and an aria-labelledby', fakeAsync(() => {
dialog.open(ContentElementDialog, {
ariaLabel: 'Hello there',
ariaLabelledBy: 'Labelled By',
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
tick();
viewContainerFixture.detectChanges();

const container = overlayContainerElement.querySelector('mat-dialog-container')!;
expect(container.hasAttribute('aria-labelledby')).toBe(false);
}));

it('should set the aria-labelledby attribute to the config provided aria-labelledby ' +
'instead of the mat-dialog-title id', fakeAsync(() => {
dialog.open(ContentElementDialog, {
ariaLabelledBy: 'Labelled By',
viewContainerRef: testViewContainerRef
});
viewContainerFixture.detectChanges();
flush();
let title = overlayContainerElement.querySelector('[mat-dialog-title]')!;
let container = overlayContainerElement.querySelector('mat-dialog-container')!;
flush();
viewContainerFixture.detectChanges();

expect(title.id).toBeTruthy('Expected title element to have an id.');
expect(container.getAttribute('aria-labelledby')).toBe('Labelled By');
}));
});

describe('aria-label', () => {
it('should be able to set a custom aria-label', () => {
dialog.open(PizzaMsg, {
Expand Down
1 change: 1 addition & 0 deletions tools/public_api_guard/lib/dialog.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export declare class MatDialogClose implements OnInit, OnChanges {
export declare class MatDialogConfig<D = any> {
ariaDescribedBy?: string | null;
ariaLabel?: string | null;
ariaLabelledBy?: string | null;
autoFocus?: boolean;
backdropClass?: string;
closeOnNavigation?: boolean;
Expand Down