Skip to content

Commit

Permalink
feat(live-announcer): add ability to clear live element (#11996)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
crisbeto authored and vivian-hu-zz committed Nov 9, 2018
1 parent ba31c72 commit 4a1c8ed
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
28 changes: 28 additions & 0 deletions src/cdk/a11y/live-announcer/live-announcer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ describe('LiveAnnouncer', () => {
expect(ariaLiveElement.getAttribute('aria-live')).toBe('polite');
}));

it('should be able to clear out the aria-live element manually', fakeAsync(() => {
announcer.announce('Hey Google');
tick(100);
expect(ariaLiveElement.textContent).toBe('Hey Google');

announcer.clear();
expect(ariaLiveElement.textContent).toBeFalsy();
}));

it('should be able to clear out the aria-live element by setting a duration', fakeAsync(() => {
announcer.announce('Hey Google', 2000);
tick(100);
expect(ariaLiveElement.textContent).toBe('Hey Google');

tick(2000);
expect(ariaLiveElement.textContent).toBeFalsy();
}));

it('should clear the duration of previous messages when announcing a new one', fakeAsync(() => {
announcer.announce('Hey Google', 2000);
tick(100);
expect(ariaLiveElement.textContent).toBe('Hey Google');

announcer.announce('Hello there');
tick(2500);
expect(ariaLiveElement.textContent).toBe('Hello there');
}));

it('should remove the aria-live element from the DOM on destroy', fakeAsync(() => {
announcer.announce('Hey Google');

Expand Down
65 changes: 60 additions & 5 deletions src/cdk/a11y/live-announcer/live-announcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,55 @@ export class LiveAnnouncer implements OnDestroy {

/**
* Announces a message to screenreaders.
* @param message Message to be announced to the screenreader
* @param politeness The politeness of the announcer element
* @param message Message to be announced to the screenreader.
* @returns Promise that will be resolved when the message is added to the DOM.
*/
announce(message: string, politeness: AriaLivePoliteness = 'polite'): Promise<void> {
this._liveElement.textContent = '';
announce(message: string): Promise<void>;

/**
* Announces a message to screenreaders.
* @param message Message to be announced to the screenreader.
* @param politeness The politeness of the announcer element.
* @returns Promise that will be resolved when the message is added to the DOM.
*/
announce(message: string, politeness?: AriaLivePoliteness): Promise<void>;

/**
* Announces a message to screenreaders.
* @param message Message to be announced to the screenreader.
* @param duration Time in milliseconds after which to clear out the announcer element. Note
* that this takes effect after the message has been added to the DOM, which can be up to
* 100ms after `announce` has been called.
* @returns Promise that will be resolved when the message is added to the DOM.
*/
announce(message: string, duration?: number): Promise<void>;

/**
* Announces a message to screenreaders.
* @param message Message to be announced to the screenreader.
* @param politeness The politeness of the announcer element.
* @param duration Time in milliseconds after which to clear out the announcer element. Note
* that this takes effect after the message has been added to the DOM, which can be up to
* 100ms after `announce` has been called.
* @returns Promise that will be resolved when the message is added to the DOM.
*/
announce(message: string, politeness?: AriaLivePoliteness, duration?: number): Promise<void>;

announce(message: string, ...args: any[]): Promise<void> {
let politeness: AriaLivePoliteness;
let duration: number;

if (args.length === 1 && typeof args[0] === 'number') {
duration = args[0];
} else {
[politeness, duration] = args;
}

this.clear();
clearTimeout(this._previousTimeout);

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

// This 100ms timeout is necessary for some browser + screen-reader combinations:
// - Both JAWS and NVDA over IE11 will not announce anything without a non-zero timeout.
Expand All @@ -68,11 +108,26 @@ export class LiveAnnouncer implements OnDestroy {
this._previousTimeout = setTimeout(() => {
this._liveElement.textContent = message;
resolve();

if (typeof duration === 'number') {
this._previousTimeout = setTimeout(() => this.clear(), duration);
}
}, 100);
});
});
}

/**
* Clears the current text from the announcer element. Can be used to prevent
* screen readers from reading the text out again while the user is going
* through the page landmarks.
*/
clear() {
if (this._liveElement) {
this._liveElement.textContent = '';
}
}

ngOnDestroy() {
clearTimeout(this._previousTimeout);

Expand Down
4 changes: 4 additions & 0 deletions src/lib/snack-bar/snack-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ export class MatSnackBar implements OnDestroy {
if (this._openedSnackBarRef == snackBarRef) {
this._openedSnackBarRef = null;
}

if (config.announcementMessage) {
this._live.clear();
}
});

if (this._openedSnackBarRef) {
Expand Down

0 comments on commit 4a1c8ed

Please sign in to comment.