2022-10-21 19:17:06 -04:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-06-16 13:35:50 -04:00
|
|
|
import { filters, findByCode, mapMangledModuleLazy } from "@webpack";
|
2023-01-24 21:25:29 -05:00
|
|
|
import type { ComponentType, PropsWithChildren, ReactNode, Ref } from "react";
|
|
|
|
|
2023-05-05 19:36:00 -04:00
|
|
|
import { LazyComponent } from "./react";
|
2022-09-30 21:58:07 -04:00
|
|
|
|
2023-06-12 20:36:25 -04:00
|
|
|
export const enum ModalSize {
|
2022-10-14 16:38:49 -04:00
|
|
|
SMALL = "small",
|
|
|
|
MEDIUM = "medium",
|
|
|
|
LARGE = "large",
|
|
|
|
DYNAMIC = "dynamic",
|
|
|
|
}
|
|
|
|
|
2023-06-12 20:36:25 -04:00
|
|
|
const enum ModalTransitionState {
|
2022-10-17 15:18:25 -04:00
|
|
|
ENTERING,
|
|
|
|
ENTERED,
|
|
|
|
EXITING,
|
|
|
|
EXITED,
|
|
|
|
HIDDEN,
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ModalProps {
|
|
|
|
transitionState: ModalTransitionState;
|
|
|
|
onClose(): Promise<void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ModalOptions {
|
|
|
|
modalKey?: string;
|
|
|
|
onCloseRequest?: (() => void);
|
|
|
|
onCloseCallback?: (() => void);
|
|
|
|
}
|
|
|
|
|
2023-01-24 21:25:29 -05:00
|
|
|
type RenderFunction = (props: ModalProps) => ReactNode;
|
2022-10-17 15:18:25 -04:00
|
|
|
|
2023-01-06 21:19:28 -05:00
|
|
|
export const Modals = mapMangledModuleLazy(".closeWithCircleBackground", {
|
|
|
|
ModalRoot: filters.byCode(".root"),
|
|
|
|
ModalHeader: filters.byCode(".header"),
|
|
|
|
ModalContent: filters.byCode(".content"),
|
|
|
|
ModalFooter: filters.byCode(".footerSeparator"),
|
|
|
|
ModalCloseButton: filters.byCode(".closeWithCircleBackground"),
|
2023-01-24 21:25:29 -05:00
|
|
|
}) as {
|
|
|
|
ModalRoot: ComponentType<PropsWithChildren<{
|
|
|
|
transitionState: ModalTransitionState;
|
|
|
|
size?: ModalSize;
|
|
|
|
role?: "alertdialog" | "dialog";
|
|
|
|
className?: string;
|
|
|
|
fullscreenOnMobile?: boolean;
|
|
|
|
"aria-label"?: string;
|
|
|
|
"aria-labelledby"?: string;
|
|
|
|
onAnimationEnd?(): string;
|
|
|
|
}>>;
|
|
|
|
ModalHeader: ComponentType<PropsWithChildren<{
|
|
|
|
/** Flex.Justify.START */
|
|
|
|
justify?: string;
|
|
|
|
/** Flex.Direction.HORIZONTAL */
|
|
|
|
direction?: string;
|
|
|
|
/** Flex.Align.CENTER */
|
|
|
|
align?: string;
|
|
|
|
/** Flex.Wrap.NO_WRAP */
|
|
|
|
wrap?: string;
|
|
|
|
separator?: boolean;
|
|
|
|
|
|
|
|
className?: string;
|
|
|
|
}>>;
|
|
|
|
/** This also accepts Scroller props but good luck with that */
|
|
|
|
ModalContent: ComponentType<PropsWithChildren<{
|
|
|
|
className?: string;
|
|
|
|
scrollerRef?: Ref<HTMLElement>;
|
|
|
|
[prop: string]: any;
|
|
|
|
}>>;
|
|
|
|
ModalFooter: ComponentType<PropsWithChildren<{
|
|
|
|
/** Flex.Justify.START */
|
|
|
|
justify?: string;
|
|
|
|
/** Flex.Direction.HORIZONTAL_REVERSE */
|
|
|
|
direction?: string;
|
|
|
|
/** Flex.Align.STRETCH */
|
|
|
|
align?: string;
|
|
|
|
/** Flex.Wrap.NO_WRAP */
|
|
|
|
wrap?: string;
|
|
|
|
separator?: boolean;
|
|
|
|
|
|
|
|
className?: string;
|
|
|
|
}>>;
|
|
|
|
ModalCloseButton: ComponentType<{
|
|
|
|
focusProps?: any;
|
|
|
|
onClick(): void;
|
|
|
|
withCircleBackground?: boolean;
|
|
|
|
hideOnFullscreen?: boolean;
|
|
|
|
className?: string;
|
|
|
|
}>;
|
|
|
|
};
|
2022-10-17 15:18:25 -04:00
|
|
|
|
2023-06-16 13:35:50 -04:00
|
|
|
export type ImageModal = ComponentType<{
|
|
|
|
className?: string;
|
|
|
|
src: string;
|
|
|
|
placeholder: string;
|
|
|
|
original: string;
|
|
|
|
width?: number;
|
|
|
|
height?: number;
|
|
|
|
animated?: boolean;
|
|
|
|
responsive?: boolean;
|
|
|
|
renderLinkComponent(props: any): ReactNode;
|
|
|
|
maxWidth?: number;
|
|
|
|
maxHeight?: number;
|
|
|
|
shouldAnimate?: boolean;
|
|
|
|
onClose?(): void;
|
|
|
|
shouldHideMediaOptions?: boolean;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export const ImageModal = LazyComponent(() => findByCode(".renderLinkComponent", ".responsive") as ImageModal);
|
|
|
|
|
2023-01-24 21:25:29 -05:00
|
|
|
export const ModalRoot = LazyComponent(() => Modals.ModalRoot);
|
|
|
|
export const ModalHeader = LazyComponent(() => Modals.ModalHeader);
|
|
|
|
export const ModalContent = LazyComponent(() => Modals.ModalContent);
|
|
|
|
export const ModalFooter = LazyComponent(() => Modals.ModalFooter);
|
|
|
|
export const ModalCloseButton = LazyComponent(() => Modals.ModalCloseButton);
|
2022-10-17 15:18:25 -04:00
|
|
|
|
|
|
|
const ModalAPI = mapMangledModuleLazy("onCloseRequest:null!=", {
|
|
|
|
openModal: filters.byCode("onCloseRequest:null!="),
|
|
|
|
closeModal: filters.byCode("onCloseCallback&&"),
|
|
|
|
openModalLazy: m => m?.length === 1 && filters.byCode(".apply(this,arguments)")(m),
|
2023-02-28 23:26:13 -05:00
|
|
|
closeAllModals: filters.byCode(".value.key,")
|
2022-10-17 15:18:25 -04:00
|
|
|
});
|
|
|
|
|
2022-11-25 13:25:35 -05:00
|
|
|
/**
|
|
|
|
* Wait for the render promise to resolve, then open a modal with it.
|
|
|
|
* This is equivalent to render().then(openModal)
|
|
|
|
* You should use the Modal components exported by this file
|
|
|
|
*/
|
2022-10-17 15:18:25 -04:00
|
|
|
export function openModalLazy(render: () => Promise<RenderFunction>, options?: ModalOptions & { contextKey?: string; }): Promise<string> {
|
|
|
|
return ModalAPI.openModalLazy(render, options);
|
|
|
|
}
|
|
|
|
|
2022-11-25 13:25:35 -05:00
|
|
|
/**
|
|
|
|
* Open a Modal with the given render function.
|
|
|
|
* You should use the Modal components exported by this file
|
|
|
|
*/
|
2022-10-17 15:18:25 -04:00
|
|
|
export function openModal(render: RenderFunction, options?: ModalOptions, contextKey?: string): string {
|
|
|
|
return ModalAPI.openModal(render, options, contextKey);
|
|
|
|
}
|
|
|
|
|
2022-11-25 13:25:35 -05:00
|
|
|
/**
|
|
|
|
* Close a modal by its key
|
|
|
|
*/
|
2022-10-17 15:18:25 -04:00
|
|
|
export function closeModal(modalKey: string, contextKey?: string): void {
|
|
|
|
return ModalAPI.closeModal(modalKey, contextKey);
|
2022-09-16 16:59:34 -04:00
|
|
|
}
|
2023-02-28 23:26:13 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Close all open modals
|
|
|
|
*/
|
|
|
|
export function closeAllModals(): void {
|
|
|
|
return ModalAPI.closeAllModals();
|
|
|
|
}
|