Skip to content

Commit 2b2f582

Browse files
devversionmmalerba
authored andcommitted
refactor(live-announcer): add unique class to identify live element (#12309)
* Adds a unique class to the live announcer element that is automatically created by the `LiveAnnouncer`. Similarly to the overlay container, the live announcer should be also distinguishable (e.g. for testing)
1 parent 98a673a commit 2b2f582

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('LiveAnnouncer', () => {
6969
// Call the lifecycle hook manually since Angular won't do it in tests.
7070
announcer.ngOnDestroy();
7171

72-
expect(document.body.querySelector('[aria-live]'))
72+
expect(document.body.querySelector('.cdk-live-announcer-element'))
7373
.toBeFalsy('Expected that the aria-live element was remove from the DOM.');
7474
}));
7575

@@ -184,7 +184,7 @@ describe('CdkAriaLive', () => {
184184

185185

186186
function getLiveElement(): Element {
187-
return document.body.querySelector('[aria-live]')!;
187+
return document.body.querySelector('.cdk-live-announcer-element')!;
188188
}
189189

190190
@Component({template: `<button (click)="announceText('Test')">Announce</button>`})

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ export type AriaLivePoliteness = 'off' | 'polite' | 'assertive';
2929

3030
@Injectable({providedIn: 'root'})
3131
export class LiveAnnouncer implements OnDestroy {
32-
private readonly _liveElement: Element;
32+
private readonly _liveElement: HTMLElement;
3333

3434
constructor(
3535
@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: any,
3636
@Inject(DOCUMENT) private _document: any) {
3737

38-
// We inject the live element as `any` because the constructor signature cannot reference
39-
// browser globals (HTMLElement) on non-browser environments, since having a class decorator
40-
// causes TypeScript to preserve the constructor signature types.
38+
// We inject the live element and document as `any` because the constructor signature cannot
39+
// reference browser globals (HTMLElement, Document) on non-browser environments, since having
40+
// a class decorator causes TypeScript to preserve the constructor signature types.
4141
this._liveElement = elementToken || this._createLiveElement();
4242
}
4343

@@ -72,10 +72,12 @@ export class LiveAnnouncer implements OnDestroy {
7272
}
7373
}
7474

75-
private _createLiveElement(): Element {
75+
private _createLiveElement(): HTMLElement {
7676
let liveEl = this._document.createElement('div');
7777

78+
liveEl.classList.add('cdk-live-announcer-element');
7879
liveEl.classList.add('cdk-visually-hidden');
80+
7981
liveEl.setAttribute('aria-atomic', 'true');
8082
liveEl.setAttribute('aria-live', 'polite');
8183

0 commit comments

Comments
 (0)