Skip to content

Commit 1ef0f5e

Browse files
crisbetojosephperrott
authored andcommitted
fix(snack-bar): announcing same message twice to screen readers (angular#13298)
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 da03a83 commit 1ef0f5e

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,16 @@ describe('MatSnackBar', () => {
186186
}));
187187

188188

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

192-
snackBar.open(simpleMessage);
192+
snackBar.open(simpleMessage, undefined, {announcementMessage: simpleMessage});
193193
viewContainerFixture.detectChanges();
194194

195195
expect(overlayContainerElement.childElementCount)
196196
.toBe(1, 'Expected the overlay with the default announcement message to be added');
197197

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

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

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

+4-2
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)