Vencord/src/utils/modal.tsx

35 lines
945 B
TypeScript
Raw Normal View History

2022-09-08 19:47:53 +00:00
import Components from "discord-types/components";
import { waitFor } from "../webpack";
export let Modal: Components.Modal;
export let modals: any;
2022-09-08 19:47:53 +00:00
waitFor("openModalLazy", m => modals = m);
waitFor("ModalRoot", m => Modal = m);
let modalId = 1337;
/**
* 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++}`;
modals.openModal(props =>
<Modal.ModalRoot {...props} {...modalProps}>
2022-09-08 19:47:53 +00:00
<Component />
</Modal.ModalRoot>
, { modalKey: key });
return key;
};
/**
* 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) {
modals.closeModal(key);
2022-09-16 20:59:34 +00:00
}