Vencord/src/utils/modal.tsx

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-10-14 15:34:35 -04:00
import { filters } from "../webpack";
import { lazyWebpack } from "./misc";
import { mapMangledModuleLazy } from "../webpack/webpack";
2022-10-14 15:34:35 -04:00
const ModalRoot = lazyWebpack(filters.byCode("headerIdIsManaged:"));
const Modals = mapMangledModuleLazy("onCloseRequest:null!=", {
openModal: filters.byCode("onCloseRequest:null!="),
closeModal: filters.byCode("onCloseCallback&&")
});
2022-09-08 15:47:53 -04:00
let modalId = 1337;
2022-10-14 16:38:49 -04:00
export enum ModalSize {
SMALL = "small",
MEDIUM = "medium",
LARGE = "large",
DYNAMIC = "dynamic",
}
2022-09-08 15:47:53 -04:00
/**
* Open a modal
* @param Component The component to render in the modal
* @returns The key of this modal. This can be used to close the modal later with closeModal
*/
export function openModal(Component: React.ComponentType, modalProps: Record<string, any>) {
2022-09-08 15:47:53 -04:00
let key = `Vencord${modalId++}`;
2022-10-14 15:34:35 -04:00
Modals.openModal(props => (
<ModalRoot {...props} {...modalProps}>
2022-09-08 15:47:53 -04:00
<Component />
2022-10-14 15:34:35 -04:00
</ModalRoot>
), { modalKey: key });
2022-09-08 15:47:53 -04:00
return key;
2022-10-08 14:36:57 -04:00
}
2022-09-08 15:47:53 -04:00
/**
* Close a modal by key. The id you need for this is returned by openModal.
* @param key The key of the modal to close
*/
export function closeModal(key: string) {
2022-10-14 15:34:35 -04:00
Modals.closeModal(key);
2022-09-16 16:59:34 -04:00
}