This repository has been archived on 2024-06-14. You can view files and clone it, but cannot push or open issues or pull requests.
icynet-admin/components/common/Modal/services/ModalService.ts

29 lines
736 B
TypeScript
Raw Permalink Normal View History

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