-
Notifications
You must be signed in to change notification settings - Fork 362
/
overlay-container.ts
41 lines (37 loc) 路 1.34 KB
/
overlay-container.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { DOCUMENT } from '@angular/common';
import { inject, Injectable, OnDestroy } from '@angular/core';
/** Container inside which all toasts will render. */
@Injectable({ providedIn: 'root' })
export class OverlayContainer implements OnDestroy {
protected _document = inject(DOCUMENT);
protected _containerElement!: HTMLElement;
ngOnDestroy() {
if (this._containerElement && this._containerElement.parentNode) {
this._containerElement.parentNode.removeChild(this._containerElement);
}
}
/**
* This method returns the overlay container element. It will lazily
* create the element the first time it is called to facilitate using
* the container in non-browser environments.
* @returns the container element
*/
getContainerElement(): HTMLElement {
if (!this._containerElement) {
this._createContainer();
}
return this._containerElement;
}
/**
* Create the overlay container element, which is simply a div
* with the 'cdk-overlay-container' class on the document body
* and 'aria-live="polite"'
*/
protected _createContainer(): void {
const container = this._document.createElement('div');
container.classList.add('overlay-container');
container.setAttribute('aria-live','polite');
this._document.body.appendChild(container);
this._containerElement = container;
}
}