29 lines
736 B
TypeScript
29 lines
736 B
TypeScript
import {
|
|
ModalType,
|
|
ModalEventDetail,
|
|
} from '../../../../lib/types/modal.interface';
|
|
|
|
const ModalService = {
|
|
on(event: string, callback: (props: ModalEventDetail<Object>) => void) {
|
|
document.addEventListener(event, (e: Event) =>
|
|
callback((e as CustomEvent<ModalEventDetail<Object>>).detail)
|
|
);
|
|
},
|
|
open<T>(component: ModalType<T>, props: any = {}) {
|
|
return new Promise((resolve, _) => {
|
|
document.dispatchEvent(
|
|
new CustomEvent<ModalEventDetail<T>>('open', {
|
|
detail: {
|
|
component,
|
|
props,
|
|
resolve,
|
|
target: (document.activeElement as HTMLElement) || undefined,
|
|
},
|
|
})
|
|
);
|
|
});
|
|
},
|
|
};
|
|
|
|
export default ModalService;
|