Skip to content

Commit ecca4ac

Browse files
committed
fix(snack-bar): announcing same message twice to screen readers
Currently we have `role="alert"` on the snack bar which will cause screen readers to announce the message automatically. On top of it, we also use the `LiveAnnouncer` to announce the same message, if the consumer hasn't set one. These changes clear the `announcementMessage` if it's the same as the main message.
1 parent 1852563 commit ecca4ac

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,18 +154,16 @@ describe('MatSnackBar', () => {
154154
}));
155155

156156

157-
it('should default to the passed message for the announcement message', fakeAsync(() => {
157+
it('should clear the announcement message if it is the same as main message', fakeAsync(() => {
158158
spyOn(liveAnnouncer, 'announce');
159159

160-
snackBar.open(simpleMessage);
160+
snackBar.open(simpleMessage, undefined, {announcementMessage: simpleMessage});
161161
viewContainerFixture.detectChanges();
162162

163163
expect(overlayContainerElement.childElementCount)
164164
.toBe(1, 'Expected the overlay with the default announcement message to be added');
165165

166-
// Expect the live announcer to have been called with the display message and some
167-
// string for the politeness. We do not want to test for the default politeness here.
168-
expect(liveAnnouncer.announce).toHaveBeenCalledWith(simpleMessage, jasmine.any(String));
166+
expect(liveAnnouncer.announce).not.toHaveBeenCalled();
169167
}));
170168

171169
it('should be able to specify a custom announcement message', fakeAsync(() => {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,10 @@ export class MatSnackBar implements OnDestroy {
114114
// override the data to pass in our own message and action.
115115
_config.data = {message, action};
116116

117-
if (!_config.announcementMessage) {
118-
_config.announcementMessage = message;
117+
// Since the snack bar has `role="alert"`, we don't
118+
// want to announce the same message twice.
119+
if (_config.announcementMessage === message) {
120+
_config.announcementMessage = undefined;
119121
}
120122

121123
return this.openFromComponent(SimpleSnackBar, _config);

0 commit comments

Comments
 (0)