Skip to content

Commit 22a3a69

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 1e1751f commit 22a3a69

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
@@ -31,6 +31,7 @@ export type AriaLivePoliteness = 'off' | 'polite' | 'assertive';
3131
export class LiveAnnouncer implements OnDestroy {
3232
private readonly _liveElement: HTMLElement;
3333
private _document: Document;
34+
private _previousMessageTimeout: number;
3435

3536
constructor(
3637
@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: any,
@@ -45,15 +46,55 @@ export class LiveAnnouncer implements OnDestroy {
4546

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

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

5899
// This 100ms timeout is necessary for some browser + screen-reader combinations:
59100
// - Both JAWS and NVDA over IE11 will not announce anything without a non-zero timeout.
@@ -64,10 +105,25 @@ export class LiveAnnouncer implements OnDestroy {
64105
setTimeout(() => {
65106
this._liveElement.textContent = message;
66107
resolve();
108+
109+
if (typeof duration === 'number') {
110+
this._previousMessageTimeout = setTimeout(() => this.clear(), duration);
111+
}
67112
}, 100);
68113
});
69114
}
70115

116+
/**
117+
* Clears the current text from the announcer element. Can be used to prevent
118+
* screen readers from reading the text out again while the user is going
119+
* through the page landmarks.
120+
*/
121+
clear() {
122+
if (this._liveElement) {
123+
this._liveElement.textContent = '';
124+
}
125+
}
126+
71127
ngOnDestroy() {
72128
if (this._liveElement && this._liveElement.parentNode) {
73129
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
@@ -200,6 +200,10 @@ export class MatSnackBar {
200200
if (this._openedSnackBarRef == snackBarRef) {
201201
this._openedSnackBarRef = null;
202202
}
203+
204+
if (config.announcementMessage) {
205+
this._live.clear();
206+
}
203207
});
204208

205209
if (this._openedSnackBarRef) {

0 commit comments

Comments
 (0)