2022-07-23 00:39:50 +00:00
|
|
|
import * as DialogPrimitive from "@radix-ui/react-dialog";
|
|
|
|
import { useRouter } from "next/router";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { ReactNode } from "react";
|
|
|
|
import React, { 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";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { SVGComponent } from "@calcom/types/SVGComponent";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { ButtonProps } from "../../components/button";
|
|
|
|
import { Button } from "../../components/button";
|
2022-07-23 00:39:50 +00:00
|
|
|
|
2023-02-16 22:39:57 +00:00
|
|
|
export type DialogProps = React.ComponentProps<(typeof DialogPrimitive)["Root"]> & {
|
2022-07-23 00:39:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 15:01:25 +00:00
|
|
|
return <DialogPrimitive.Root {...dialogProps}>{children}</DialogPrimitive.Root>;
|
2022-07-23 00:39:50 +00:00
|
|
|
}
|
2023-02-16 22:39:57 +00:00
|
|
|
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;
|
2023-01-24 14:00:45 +00:00
|
|
|
enableOverflow?: boolean;
|
2022-07-23 00:39:50 +00:00
|
|
|
};
|
|
|
|
|
2023-01-24 14:00:45 +00:00
|
|
|
// enableOverflow:- use this prop whenever content inside DialogContent could overflow and require scrollbar
|
2022-07-23 00:39:50 +00:00
|
|
|
export const DialogContent = React.forwardRef<HTMLDivElement, DialogContentProps>(
|
2023-01-24 14:00:45 +00:00
|
|
|
({ children, title, Icon, enableOverflow, type = "creation", ...props }, forwardedRef) => {
|
2022-10-05 14:33:29 +00:00
|
|
|
return (
|
|
|
|
<DialogPrimitive.Portal>
|
2023-03-08 10:59:06 +00:00
|
|
|
<DialogPrimitive.Overlay className="fadeIn fixed inset-0 z-50 bg-gray-500 bg-opacity-80 transition-opacity" />
|
2022-10-05 14:33:29 +00:00
|
|
|
<DialogPrimitive.Content
|
|
|
|
{...props}
|
|
|
|
className={classNames(
|
2023-03-20 09:34:08 +00:00
|
|
|
"fadeIn fixed left-1/2 top-1/2 z-50 min-w-[270px] -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",
|
2022-10-05 14:33:29 +00:00
|
|
|
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-20 13:29:37 +00:00
|
|
|
"max-h-[95vh]",
|
2023-01-24 14:00:45 +00:00
|
|
|
enableOverflow ? "overflow-auto" : "overflow-visible",
|
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: {
|
2023-02-16 22:39:57 +00:00
|
|
|
dialogCloseProps?: React.ComponentProps<(typeof DialogPrimitive)["Close"]>;
|
2022-11-28 19:14:38 +00:00
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|