Skip to content

Commit 1a33a5b

Browse files
devversionjelbourn
authored andcommitted
feat(live-announcer): support using a provided live element (#273)
Fixes #267.
1 parent 38cfe58 commit 1a33a5b

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

src/core/live-announcer/live-announcer.spec.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import {
99
fakeAsync,
1010
flushMicrotasks,
1111
tick,
12-
beforeEachProviders
12+
beforeEachProviders,
13+
getTestInjector
1314
} from 'angular2/testing';
14-
import {Component} from 'angular2/core';
15+
import {Component, provide} from 'angular2/core';
1516
import {By} from 'angular2/platform/browser';
16-
import {MdLiveAnnouncer} from './live-announcer';
17+
import {MdLiveAnnouncer, LIVE_ANNOUNCER_ELEMENT_TOKEN} from './live-announcer';
1718

1819
export function main() {
1920
describe('MdLiveAnnouncer', () => {
@@ -92,6 +93,28 @@ export function main() {
9293
expect(liveEl.getAttribute('aria-live')).toBe('polite');
9394
}));
9495

96+
it('should allow to use a custom live element', fakeAsyncTest(() => {
97+
let customLiveEl = document.createElement('div');
98+
99+
// We need to reset our test injector here, because it is already instantiated above.
100+
getTestInjector().reset();
101+
102+
getTestInjector().addProviders([
103+
provide(LIVE_ANNOUNCER_ELEMENT_TOKEN, {useValue: customLiveEl}),
104+
MdLiveAnnouncer
105+
]);
106+
107+
let injector = getTestInjector().createInjector();
108+
let liveService: MdLiveAnnouncer = injector.get(MdLiveAnnouncer);
109+
110+
liveService.announce('Custom Element');
111+
112+
// This flushes our 100ms timeout for the screenreaders.
113+
tick(100);
114+
115+
expect(customLiveEl.textContent).toBe('Custom Element');
116+
}));
117+
95118
});
96119
}
97120

src/core/live-announcer/live-announcer.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import {Injectable} from 'angular2/core';
1+
import {
2+
Injectable,
3+
OpaqueToken,
4+
Optional,
5+
Inject
6+
} from 'angular2/core';
7+
8+
export const LIVE_ANNOUNCER_ELEMENT_TOKEN = new OpaqueToken('mdLiveAnnouncerElement');
29

310
export type AriaLivePoliteness = 'off' | 'polite' | 'assertive';
411

@@ -7,8 +14,8 @@ export class MdLiveAnnouncer {
714

815
private _liveElement: Element;
916

10-
constructor() {
11-
this._liveElement = this._createLiveElement();
17+
constructor(@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: Element) {
18+
this._liveElement = elementToken || this._createLiveElement();
1219
}
1320

1421
/**

src/core/overlay/overlay.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import {
2-
DynamicComponentLoader,
3-
AppViewManager,
4-
OpaqueToken,
5-
Inject,
6-
Injectable, ElementRef
2+
DynamicComponentLoader,
3+
AppViewManager,
4+
OpaqueToken,
5+
Inject,
6+
Injectable,
7+
ElementRef
78
} from 'angular2/core';
89
import {OverlayState} from './overlay-state';
910
import {DomPortalHost} from '../portal/dom-portal-host';

0 commit comments

Comments
 (0)