2022-07-23 00:39:50 +00:00
|
|
|
import * as DialogPrimitive from "@radix-ui/react-dialog";
|
|
|
|
import { useRouter } from "next/router";
|
2022-12-10 22:48:26 +00:00
|
|
|
import React, { ReactNode, useState } from "react";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
import classNames from "@calcom/lib/classNames";
|
2022-10-05 14:33:29 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-12-18 10:44:44 +00:00
|
|
|
import { SVGComponent } from "@calcom/types/SVGComponent";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2022-11-28 19:14:38 +00:00
|
|
|
import { Button, ButtonProps } from "../../components/button";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
|
|
|
export type DialogProps = React.ComponentProps<typeof DialogPrimitive["Root"]> & {
|
|
|
|
name?: string;
|
|
|
|
clearQueryParamsOnClose?: string[];
|
|
|
|
};
|
|
|
|
export function Dialog(props: DialogProps) {
|
|
|
|
const router = useRouter();
|
|
|
|
const { children, name, ...dialogProps } = props;
|
|
|
|
// only used if name is set
|
|
|
|
const [open, setOpen] = useState(!!dialogProps.open);
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
const clearQueryParamsOnClose = ["dialog", ...(props.clearQueryParamsOnClose || [])];
|
|
|
|
dialogProps.onOpenChange = (open) => {
|
|
|
|
if (props.onOpenChange) {
|
|
|
|
props.onOpenChange(open);
|
|
|
|
}
|
|
|
|
// toggles "dialog" query param
|
|
|
|
if (open) {
|
|
|
|
router.query["dialog"] = name;
|
|
|
|
} else {
|
2023-01-06 20:05:23 +00:00
|
|
|
const query = router.query;
|
2022-07-23 00:39:50 +00:00
|
|
|
clearQueryParamsOnClose.forEach((queryParam) => {
|
2023-01-06 20:05:23 +00:00
|
|
|
delete query[queryParam];
|
2022-07-23 00:39:50 +00:00
|
|
|
});
|
2023-01-06 20:05:23 +00:00
|
|
|
router.push(
|
|
|
|
{
|
|
|
|
pathname: router.pathname,
|
|
|
|
query,
|
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
{ shallow: true }
|
|
|
|
);
|
2022-07-23 00:39:50 +00:00
|
|
|
}
|
|
|
|
setOpen(open);
|
|
|
|
};
|
|
|
|
// handles initial state
|
|
|
|
if (!open && router.query["dialog"] === name) {
|
|
|
|
setOpen(true);
|
|
|
|
}
|
|
|
|
// allow overriding
|
|
|
|
if (!("open" in dialogProps)) {
|
|
|
|
dialogProps.open = open;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DialogPrimitive.Root {...dialogProps}>
|
|
|
|
<DialogPrimitive.Overlay className="fadeIn fixed inset-0 z-40 bg-black bg-opacity-50 transition-opacity" />
|
|
|
|
{children}
|
|
|
|
</DialogPrimitive.Root>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
type DialogContentProps = React.ComponentProps<typeof DialogPrimitive["Content"]> & {
|
2022-09-12 22:04:33 +00:00
|
|
|
size?: "xl" | "lg" | "md";
|
2022-11-23 02:55:25 +00:00
|
|
|
type?: "creation" | "confirmation";
|
2022-07-23 00:39:50 +00:00
|
|
|
title?: string;
|
2022-09-15 09:47:59 +00:00
|
|
|
description?: string | JSX.Element | undefined;
|
2022-07-23 00:39:50 +00:00
|
|
|
closeText?: string;
|
2022-08-03 16:01:29 +00:00
|
|
|
actionDisabled?: boolean;
|
2022-12-18 10:44:44 +00:00
|
|
|
Icon?: SVGComponent;
|
2022-07-23 00:39:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const DialogContent = React.forwardRef<HTMLDivElement, DialogContentProps>(
|
2022-11-28 19:14:38 +00:00
|
|
|
({ children, title, Icon, type = "creation", ...props }, forwardedRef) => {
|
2022-10-05 14:33:29 +00:00
|
|
|
return (
|
|
|
|
<DialogPrimitive.Portal>
|
|
|
|
<DialogPrimitive.Overlay className="fadeIn fixed inset-0 z-40 bg-gray-500 bg-opacity-75 transition-opacity" />
|
|
|
|
{/*zIndex one less than Toast */}
|
|
|
|
<DialogPrimitive.Content
|
|
|
|
{...props}
|
|
|
|
className={classNames(
|
|
|
|
"fadeIn fixed left-1/2 top-1/2 z-[9998] min-w-[360px] -translate-x-1/2 -translate-y-1/2 rounded bg-white text-left shadow-xl focus-visible:outline-none sm:w-full sm:align-middle",
|
|
|
|
props.size == "xl"
|
|
|
|
? "p-8 sm:max-w-[90rem]"
|
|
|
|
: props.size == "lg"
|
|
|
|
? "p-8 sm:max-w-[70rem]"
|
|
|
|
: props.size == "md"
|
2022-12-14 17:30:55 +00:00
|
|
|
? "p-8 sm:max-w-[48rem]"
|
2022-10-05 14:33:29 +00:00
|
|
|
: "p-8 sm:max-w-[35rem]",
|
2023-01-16 17:08:34 +00:00
|
|
|
"max-h-[95vh] overflow-auto",
|
2022-10-05 14:33:29 +00:00
|
|
|
`${props.className || ""}`
|
|
|
|
)}
|
|
|
|
ref={forwardedRef}>
|
2022-11-23 02:55:25 +00:00
|
|
|
{type === "creation" && (
|
2022-07-23 00:39:50 +00:00
|
|
|
<div>
|
2022-12-01 00:59:27 +00:00
|
|
|
<DialogHeader title={title} subtitle={props.description} />
|
2022-10-05 14:33:29 +00:00
|
|
|
<div className="flex flex-col space-y-6">{children}</div>
|
2022-07-23 00:39:50 +00:00
|
|
|
</div>
|
2022-10-05 14:33:29 +00:00
|
|
|
)}
|
2022-11-23 02:55:25 +00:00
|
|
|
{type === "confirmation" && (
|
2022-10-05 14:33:29 +00:00
|
|
|
<div className="flex">
|
|
|
|
{Icon && (
|
|
|
|
<div className="mr-4 inline-flex h-10 w-10 items-center justify-center rounded-full bg-gray-300">
|
|
|
|
<Icon className="h-4 w-4 text-black" />
|
|
|
|
</div>
|
2022-09-05 14:23:18 +00:00
|
|
|
)}
|
2022-12-01 00:59:27 +00:00
|
|
|
<div className="w-full">
|
|
|
|
<DialogHeader title={title} subtitle={props.description} />
|
|
|
|
<div className="flex flex-col space-y-6">{children}</div>
|
2022-10-05 14:33:29 +00:00
|
|
|
</div>
|
2022-09-05 14:23:18 +00:00
|
|
|
</div>
|
2022-10-05 14:33:29 +00:00
|
|
|
)}
|
|
|
|
</DialogPrimitive.Content>
|
|
|
|
</DialogPrimitive.Portal>
|
|
|
|
);
|
|
|
|
}
|
2022-07-23 00:39:50 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
type DialogHeaderProps = {
|
|
|
|
title: React.ReactNode;
|
|
|
|
subtitle?: React.ReactNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function DialogHeader(props: DialogHeaderProps) {
|
2022-12-01 00:59:27 +00:00
|
|
|
if (!props.title) return null;
|
|
|
|
|
2022-07-23 00:39:50 +00:00
|
|
|
return (
|
2022-12-01 00:59:27 +00:00
|
|
|
<div className="mb-4">
|
2022-09-05 14:23:18 +00:00
|
|
|
<h3 className="leading-20 text-semibold font-cal pb-1 text-xl text-gray-900" id="modal-title">
|
2022-07-23 00:39:50 +00:00
|
|
|
{props.title}
|
|
|
|
</h3>
|
2022-12-01 00:59:27 +00:00
|
|
|
{props.subtitle && <div className="text-sm text-gray-500">{props.subtitle}</div>}
|
|
|
|
</div>
|
2022-07-23 00:39:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function DialogFooter(props: { children: ReactNode }) {
|
2022-12-01 00:11:59 +00:00
|
|
|
return <div className="mt-7 flex justify-end space-x-2 rtl:space-x-reverse ">{props.children}</div>;
|
2022-07-23 00:39:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DialogContent.displayName = "DialogContent";
|
|
|
|
|
|
|
|
export const DialogTrigger = DialogPrimitive.Trigger;
|
2022-11-28 19:14:38 +00:00
|
|
|
// export const DialogClose = DialogPrimitive.Close;
|
|
|
|
|
|
|
|
export function DialogClose(
|
|
|
|
props: {
|
|
|
|
dialogCloseProps?: React.ComponentProps<typeof DialogPrimitive["Close"]>;
|
|
|
|
children?: ReactNode;
|
|
|
|
onClick?: (e: React.MouseEvent<HTMLElement, MouseEvent>) => void;
|
|
|
|
disabled?: boolean;
|
|
|
|
color?: ButtonProps["color"];
|
|
|
|
} & React.ComponentProps<typeof Button>
|
|
|
|
) {
|
|
|
|
const { t } = useLocale();
|
|
|
|
return (
|
|
|
|
<DialogPrimitive.Close asChild {...props.dialogCloseProps}>
|
|
|
|
{/* This will require the i18n string passed in */}
|
|
|
|
<Button color={props.color || "minimal"} {...props}>
|
|
|
|
{props.children ? props.children : t("Close")}
|
|
|
|
</Button>
|
|
|
|
</DialogPrimitive.Close>
|
|
|
|
);
|
|
|
|
}
|