Skip to content

Commit 69ffd33

Browse files
josephperrottvivian-hu-zz
authored andcommitted
fix(snackbar): set aria role based on if announcement message is provided (#13993)
1 parent 81f8fb9 commit 69ffd33

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

src/lib/snack-bar/snack-bar-container.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ export class MatSnackBarContainer extends BasePortalOutlet implements OnDestroy
8080

8181
// Based on the ARIA spec, `alert` and `status` roles have an
8282
// implicit `assertive` and `polite` politeness respectively.
83-
if (snackBarConfig.politeness === 'assertive') {
83+
if (snackBarConfig.politeness === 'assertive' && !snackBarConfig.announcementMessage) {
8484
this._role = 'alert';
85-
} else if (snackBarConfig.politeness === 'polite') {
86-
this._role = 'status';
87-
} else {
85+
} else if (snackBarConfig.politeness === 'off') {
8886
this._role = null;
87+
} else {
88+
this._role = 'status';
8989
}
9090
}
9191

src/lib/snack-bar/snack-bar.spec.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,31 @@ describe('MatSnackBar', () => {
6767
testViewContainerRef = viewContainerFixture.componentInstance.childViewContainer;
6868
});
6969

70-
it('should have the role of `alert` with an `assertive` politeness', () => {
71-
snackBar.open(simpleMessage, simpleActionLabel, {politeness: 'assertive'});
70+
it('should have the role of `alert` with an `assertive` politeness if no announcement message ' +
71+
'is provided', () => {
72+
snackBar.openFromComponent(BurritosNotification,
73+
{announcementMessage: '', politeness: 'assertive'});
74+
7275
viewContainerFixture.detectChanges();
7376

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

82+
it('should have the role of `status` with an `assertive` politeness if an announcement message ' +
83+
'is provided', () => {
84+
snackBar.openFromComponent(BurritosNotification,
85+
{announcementMessage: 'Yay Burritos', politeness: 'assertive'});
86+
viewContainerFixture.detectChanges();
87+
88+
const containerElement = overlayContainerElement.querySelector('snack-bar-container')!;
89+
expect(containerElement.getAttribute('role'))
90+
.toBe('status', 'Expected snack bar container to have role="status"');
91+
});
92+
7993
it('should have the role of `status` with a `polite` politeness', () => {
80-
snackBar.open(simpleMessage, simpleActionLabel, {politeness: 'polite'});
94+
snackBar.openFromComponent(BurritosNotification, {politeness: 'polite'});
8195
viewContainerFixture.detectChanges();
8296

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

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

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

0 commit comments

Comments
 (0)