This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
modal.ts
48 lines (35 loc) · 1.51 KB
/
modal.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
42
43
44
45
46
47
48
import { CSSResultGroup, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
import { WebComponentsBase } from '../base';
import { ModalContainer } from './modal-container';
import { modalStyle } from './styles/index.style';
import { ModalOptions } from './types';
const WebComponentsBaseElement = WebComponentsBase(LitElement);
const styles: CSSResultGroup[] = [WebComponentsBaseElement.styles, modalStyle];
@customElement('superviz-modal')
export class Modal extends WebComponentsBaseElement {
static styles = styles;
private modal: ModalContainer;
getContainer = () => window.document.querySelector('superviz-modal-container');
createContainer = (options: ModalOptions) => {
this.modal = document.createElement('superviz-modal-container') as ModalContainer;
this.modal.setOptions(options);
};
protected firstUpdated(): void {
window.document.body.addEventListener('superviz-modal--open', this.createModal);
window.document.body.addEventListener('superviz-modal--close', this.destroyModal);
}
private createModal = ({ detail }: CustomEvent) => {
this.createContainer(detail);
window.document.body.appendChild(this.modal);
};
private destroyModal = () => {
this.modal = undefined;
this.getContainer()?.remove();
};
disconnectedCallback(): void {
this.destroyModal();
window.document.body.removeEventListener('superviz-modal--open', this.createModal);
window.document.body.removeEventListener('superviz-modal--close', this.destroyModal);
}
}