Skip to content

fix(snackbar): set aria role based on if announcement message is provided #13993

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
Nov 7, 2018
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
8 changes: 4 additions & 4 deletions src/lib/snack-bar/snack-bar-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy

// Based on the ARIA spec, `alert` and `status` roles have an
// implicit `assertive` and `polite` politeness respectively.
if (snackBarConfig.politeness === 'assertive') {
if (snackBarConfig.politeness === 'assertive' && !snackBarConfig.announcementMessage) {
this._role = 'alert';
} else if (snackBarConfig.politeness === 'polite') {
this._role = 'status';
} else {
} else if (snackBarConfig.politeness === 'off') {
this._role = null;
} else {
this._role = 'status';
}
}

Expand Down
22 changes: 18 additions & 4 deletions src/lib/snack-bar/snack-bar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,31 @@ describe('MatSnackBar', () => {
testViewContainerRef = viewContainerFixture.componentInstance.childViewContainer;
});

it('should have the role of `alert` with an `assertive` politeness', () => {
snackBar.open(simpleMessage, simpleActionLabel, {politeness: 'assertive'});
it('should have the role of `alert` with an `assertive` politeness if no announcement message ' +
'is provided', () => {
snackBar.openFromComponent(BurritosNotification,
{announcementMessage: '', politeness: 'assertive'});

viewContainerFixture.detectChanges();

const containerElement = overlayContainerElement.querySelector('snack-bar-container')!;
expect(containerElement.getAttribute('role'))
.toBe('alert', 'Expected snack bar container to have role="alert"');
});

it('should have the role of `status` with an `assertive` politeness if an announcement message ' +
'is provided', () => {
snackBar.openFromComponent(BurritosNotification,
{announcementMessage: 'Yay Burritos', politeness: 'assertive'});
viewContainerFixture.detectChanges();

const containerElement = overlayContainerElement.querySelector('snack-bar-container')!;
expect(containerElement.getAttribute('role'))
.toBe('status', 'Expected snack bar container to have role="status"');
});

it('should have the role of `status` with a `polite` politeness', () => {
snackBar.open(simpleMessage, simpleActionLabel, {politeness: 'polite'});
snackBar.openFromComponent(BurritosNotification, {politeness: 'polite'});
viewContainerFixture.detectChanges();

const containerElement = overlayContainerElement.querySelector('snack-bar-container')!;
Expand All @@ -86,7 +100,7 @@ describe('MatSnackBar', () => {
});

it('should remove the role if the politeness is turned off', () => {
snackBar.open(simpleMessage, simpleActionLabel, {politeness: 'off'});
snackBar.openFromComponent(BurritosNotification, {politeness: 'off'});
viewContainerFixture.detectChanges();

const containerElement = overlayContainerElement.querySelector('snack-bar-container')!;
Expand Down