Skip to content

Commit 4551a0a

Browse files
committed
feat(live-announcer): add ability to clear live element
Currently when we announce a message, we leave the element in place, however this can cause the element to be read out again when the user continues navigating using the arrow keys. These changes add an API where the consumer can clear the element manually or after a duration. Doing this automatically is tricky, because we'd have to make a lot of assumptions that might not be correct in all cases or may break some screen readers. These are some alternatives that were considered: * Removing the element from the a11y tree via `aria-hidden` after the screen reader has started announcing it. This works, but because of the politeness, we can't know exactly when the screen reader will announce the text, which could end up hiding it too early. * Clearing the element on the next `keydown`/`click`/`focus`. This could be flaky and might end up interrupting the screen reader when it doesn't have to (e.g. clicking on a non-focusable element). * Moving the element to a different place in the DOM (e.g. prepending it to the `body` instead of appending). This works, but we'd have to keep moving the element based on the focus direction. Fixes #11991.
1 parent e462f3d commit 4551a0a

File tree

4 files changed

+94
-6
lines changed

4 files changed

+94
-6
lines changed

src/cdk/a11y/live-announcer/live-announcer.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,34 @@ describe('LiveAnnouncer', () => {
6060
expect(ariaLiveElement.getAttribute('aria-live')).toBe('polite');
6161
}));
6262

63+
it('should be able to clear out the aria-live element manually', fakeAsync(() => {
64+
announcer.announce('Hey Google');
65+
tick(100);
66+
expect(ariaLiveElement.textContent).toBe('Hey Google');
67+
68+
announcer.clear();
69+
expect(ariaLiveElement.textContent).toBeFalsy();
70+
}));
71+
72+
it('should be able to clear out the aria-live element by setting a duration', fakeAsync(() => {
73+
announcer.announce('Hey Google', 2000);
74+
tick(100);
75+
expect(ariaLiveElement.textContent).toBe('Hey Google');
76+
77+
tick(2000);
78+
expect(ariaLiveElement.textContent).toBeFalsy();
79+
}));
80+
81+
it('should clear the duration of previous messages when announcing a new one', fakeAsync(() => {
82+
announcer.announce('Hey Google', 2000);
83+
tick(100);
84+
expect(ariaLiveElement.textContent).toBe('Hey Google');
85+
86+
announcer.announce('Hello there');
87+
tick(2500);
88+
expect(ariaLiveElement.textContent).toBe('Hello there');
89+
}));
90+
6391
it('should remove the aria-live element from the DOM on destroy', fakeAsync(() => {
6492
announcer.announce('Hey Google');
6593

src/cdk/a11y/live-announcer/live-announcer.ts

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type AriaLivePoliteness = 'off' | 'polite' | 'assertive';
3030
@Injectable({providedIn: 'root'})
3131
export class LiveAnnouncer implements OnDestroy {
3232
private readonly _liveElement: HTMLElement;
33+
private _previousMessageTimeout: number;
3334

3435
constructor(
3536
@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: any,
@@ -43,15 +44,55 @@ export class LiveAnnouncer implements OnDestroy {
4344

4445
/**
4546
* Announces a message to screenreaders.
46-
* @param message Message to be announced to the screenreader
47-
* @param politeness The politeness of the announcer element
47+
* @param message Message to be announced to the screenreader.
4848
* @returns Promise that will be resolved when the message is added to the DOM.
4949
*/
50-
announce(message: string, politeness: AriaLivePoliteness = 'polite'): Promise<void> {
51-
this._liveElement.textContent = '';
50+
announce(message: string): Promise<void>;
51+
52+
/**
53+
* Announces a message to screenreaders.
54+
* @param message Message to be announced to the screenreader.
55+
* @param politeness The politeness of the announcer element.
56+
* @returns Promise that will be resolved when the message is added to the DOM.
57+
*/
58+
announce(message: string, politeness?: AriaLivePoliteness): Promise<void>;
59+
60+
/**
61+
* Announces a message to screenreaders.
62+
* @param message Message to be announced to the screenreader.
63+
* @param duration Time in milliseconds after which to clear out the announcer element. Note
64+
* that this takes effect after the message has been added to the DOM, which can be up to
65+
* 100ms after `announce` has been called.
66+
* @returns Promise that will be resolved when the message is added to the DOM.
67+
*/
68+
announce(message: string, duration?: number): Promise<void>;
69+
70+
/**
71+
* Announces a message to screenreaders.
72+
* @param message Message to be announced to the screenreader.
73+
* @param politeness The politeness of the announcer element.
74+
* @param duration Time in milliseconds after which to clear out the announcer element. Note
75+
* that this takes effect after the message has been added to the DOM, which can be up to
76+
* 100ms after `announce` has been called.
77+
* @returns Promise that will be resolved when the message is added to the DOM.
78+
*/
79+
announce(message: string, politeness?: AriaLivePoliteness, duration?: number): Promise<void>;
80+
81+
announce(message: string, ...args: any[]): Promise<void> {
82+
let politeness: AriaLivePoliteness;
83+
let duration: number;
84+
85+
if (args.length === 1 && typeof args[0] === 'number') {
86+
duration = args[0];
87+
} else {
88+
[politeness, duration] = args;
89+
}
90+
91+
this.clear();
92+
clearTimeout(this._previousMessageTimeout);
5293

5394
// TODO: ensure changing the politeness works on all environments we support.
54-
this._liveElement.setAttribute('aria-live', politeness);
95+
this._liveElement.setAttribute('aria-live', politeness! || 'polite');
5596

5697
// This 100ms timeout is necessary for some browser + screen-reader combinations:
5798
// - Both JAWS and NVDA over IE11 will not announce anything without a non-zero timeout.
@@ -62,10 +103,25 @@ export class LiveAnnouncer implements OnDestroy {
62103
setTimeout(() => {
63104
this._liveElement.textContent = message;
64105
resolve();
106+
107+
if (typeof duration === 'number') {
108+
this._previousMessageTimeout = setTimeout(() => this.clear(), duration);
109+
}
65110
}, 100);
66111
});
67112
}
68113

114+
/**
115+
* Clears the current text from the announcer element. Can be used to prevent
116+
* screen readers from reading the text out again while the user is going
117+
* through the page landmarks.
118+
*/
119+
clear() {
120+
if (this._liveElement) {
121+
this._liveElement.textContent = '';
122+
}
123+
}
124+
69125
ngOnDestroy() {
70126
if (this._liveElement && this._liveElement.parentNode) {
71127
this._liveElement.parentNode.removeChild(this._liveElement);

src/demo-app/live-announcer/live-announcer-demo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ export class LiveAnnouncerDemo {
1919
constructor(private live: LiveAnnouncer) {}
2020

2121
announceText(message: string) {
22-
this.live.announce(message);
22+
this.live.announce(message, 1500);
2323
}
2424
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ export class MatSnackBar {
197197
if (this._openedSnackBarRef == snackBarRef) {
198198
this._openedSnackBarRef = null;
199199
}
200+
201+
if (config.announcementMessage) {
202+
this._live.clear();
203+
}
200204
});
201205

202206
if (this._openedSnackBarRef) {

0 commit comments

Comments
 (0)