-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typeahead): accessibility, options summary status message
Typeahead now announces the number of available results like 'no results available' or '3 results available' via the aria-live region Closes #2197 Fixes #1945
- Loading branch information
1 parent
fd89c3b
commit f65b8a4
Showing
5 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {TestBed, ComponentFixture, inject} from '@angular/core/testing'; | ||
import {Component} from '@angular/core'; | ||
import {By} from '@angular/platform-browser'; | ||
import {Live, ARIA_LIVE_DELAY} from './live'; | ||
|
||
|
||
|
||
function getLiveElement(): Element { | ||
return document.body.querySelector('[aria-live]'); | ||
} | ||
|
||
|
||
|
||
describe('LiveAnnouncer', () => { | ||
let live: Live; | ||
let liveElement: Element; | ||
let fixture: ComponentFixture<TestComponent>; | ||
|
||
const say = () => { fixture.debugElement.query(By.css('button')).nativeElement.click(); }; | ||
|
||
describe('live announcer', () => { | ||
beforeEach(() => TestBed.configureTestingModule({ | ||
providers: [Live, {provide: ARIA_LIVE_DELAY, useValue: null}], | ||
declarations: [TestComponent], | ||
})); | ||
|
||
beforeEach(inject([Live], (_live: Live) => { | ||
live = _live; | ||
liveElement = getLiveElement(); | ||
fixture = TestBed.createComponent(TestComponent); | ||
})); | ||
|
||
it('should correctly update the text message', () => { | ||
say(); | ||
expect(liveElement.textContent).toBe('test'); | ||
|
||
live.ngOnDestroy(); | ||
}); | ||
|
||
it('should remove the used element from the DOM on destroy', () => { | ||
say(); | ||
live.ngOnDestroy(); | ||
|
||
expect(getLiveElement()).toBeFalsy(); | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
|
||
@Component({template: `<button (click)="say()">say</button>`}) | ||
class TestComponent { | ||
constructor(public live: Live) {} | ||
say() { this.live.say('test'); } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {Injectable, Inject, InjectionToken, OnDestroy} from '@angular/core'; | ||
import {DOCUMENT} from '@angular/common'; | ||
|
||
|
||
|
||
// usefulness (and default value) of delay documented in Material's CDK | ||
// https://github.com/angular/material2/blob/6405da9b8e8532a7e5c854c920ee1815c275d734/src/cdk/a11y/live-announcer/live-announcer.ts#L50 | ||
export type ARIA_LIVE_DELAY_TYPE = number | null; | ||
export const ARIA_LIVE_DELAY = new InjectionToken<ARIA_LIVE_DELAY_TYPE>('live announcer delay'); | ||
export const DEFAULT_ARIA_LIVE_DELAY: ARIA_LIVE_DELAY_TYPE = 100; | ||
|
||
|
||
|
||
function createLiveElement(document): HTMLElement { | ||
const element = document.createElement('div'); | ||
|
||
element.setAttribute('aria-live', 'polite'); | ||
element.setAttribute('aria-atomic', 'true'); | ||
|
||
element.classList.add('sr-only'); | ||
|
||
document.body.appendChild(element); | ||
|
||
return element; | ||
} | ||
|
||
|
||
|
||
@Injectable() | ||
export class Live implements OnDestroy { | ||
private _element: HTMLElement; | ||
|
||
constructor(@Inject(DOCUMENT) document: any, @Inject(ARIA_LIVE_DELAY) private _delay: ARIA_LIVE_DELAY_TYPE) { | ||
this._element = createLiveElement(document); | ||
} | ||
|
||
ngOnDestroy() { this._element.parentElement.removeChild(this._element); } | ||
|
||
say(message: string): void { | ||
const element = this._element; | ||
const delay = this._delay; | ||
|
||
element.textContent = ''; | ||
const setText = () => element.textContent = message; | ||
if (delay === null) { | ||
setText(); | ||
} else { | ||
setTimeout(setText, delay); | ||
} | ||
} | ||
} |