Vencord/src/utils/modal.tsx

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-10-14 19:34:35 +00:00
import { filters } from "../webpack";
import { lazyWebpack } from "./misc";
import { mapMangledModuleLazy } from "../webpack/webpack";
2022-10-14 19:34:35 +00:00
const ModalRoot = lazyWebpack(filters.byCode("headerIdIsManaged:"));
const Modals = mapMangledModuleLazy("onCloseRequest:null!=", {
openModal: filters.byCode("onCloseRequest:null!="),
closeModal: filters.byCode("onCloseCallback&&")
});
2022-09-08 19:47:53 +00:00
let modalId = 1337;
2022-10-14 20:38:49 +00:00
export enum ModalSize {
SMALL = "small",
MEDIUM = "medium",
LARGE = "large",
DYNAMIC = "dynamic",
}
2022-09-08 19:47:53 +00: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 19:47:53 +00:00
let key = `Vencord${modalId++}`;
2022-10-14 19:34:35 +00:00
Modals.openModal(props => (
<ModalRoot {...props} {...modalProps}>
2022-09-08 19:47:53 +00:00
<Component />
2022-10-14 19:34:35 +00:00
</ModalRoot>
), { modalKey: key });
2022-09-08 19:47:53 +00:00
return key;
2022-10-08 18:36:57 +00:00
}
2022-09-08 19:47:53 +00: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 19:34:35 +00:00
Modals.closeModal(key);
2022-09-16 20:59:34 +00:00
}