Skip to content

refactor(live-announcer): add unique class to identify live element #12309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cdk/a11y/live-announcer/live-announcer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('LiveAnnouncer', () => {
// Call the lifecycle hook manually since Angular won't do it in tests.
announcer.ngOnDestroy();

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

Expand Down Expand Up @@ -184,7 +184,7 @@ describe('CdkAriaLive', () => {


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

@Component({template: `<button (click)="announceText('Test')">Announce</button>`})
Expand Down
12 changes: 7 additions & 5 deletions src/cdk/a11y/live-announcer/live-announcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ export type AriaLivePoliteness = 'off' | 'polite' | 'assertive';

@Injectable({providedIn: 'root'})
export class LiveAnnouncer implements OnDestroy {
private readonly _liveElement: Element;
private readonly _liveElement: HTMLElement;

constructor(
@Optional() @Inject(LIVE_ANNOUNCER_ELEMENT_TOKEN) elementToken: any,
@Inject(DOCUMENT) private _document: any) {

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

Expand Down Expand Up @@ -72,10 +72,12 @@ export class LiveAnnouncer implements OnDestroy {
}
}

private _createLiveElement(): Element {
private _createLiveElement(): HTMLElement {
let liveEl = this._document.createElement('div');

liveEl.classList.add('cdk-live-announcer-element');
liveEl.classList.add('cdk-visually-hidden');

liveEl.setAttribute('aria-atomic', 'true');
liveEl.setAttribute('aria-live', 'polite');

Expand Down