2021-08-03 17:36:55 +00:00
|
|
|
import * as DialogPrimitive from "@radix-ui/react-dialog";
|
2022-03-06 23:06:18 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import React, { ReactNode, useState } from "react";
|
2021-08-03 17:36:55 +00:00
|
|
|
|
2022-05-04 04:06:20 +00:00
|
|
|
import classNames from "@calcom/lib/classNames";
|
|
|
|
|
2022-03-06 23:06:18 +00:00
|
|
|
export type DialogProps = React.ComponentProps<typeof DialogPrimitive["Root"]> & {
|
|
|
|
name?: string;
|
|
|
|
clearQueryParamsOnClose?: string[];
|
|
|
|
};
|
2021-08-23 12:45:25 +00:00
|
|
|
export function Dialog(props: DialogProps) {
|
2022-03-06 23:06:18 +00:00
|
|
|
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 {
|
|
|
|
clearQueryParamsOnClose.forEach((queryParam) => {
|
|
|
|
delete router.query[queryParam];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
router.push(
|
|
|
|
{
|
|
|
|
pathname: router.pathname,
|
|
|
|
query: {
|
|
|
|
...router.query,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
undefined,
|
|
|
|
{ shallow: true }
|
|
|
|
);
|
|
|
|
setOpen(open);
|
|
|
|
};
|
|
|
|
// handles initial state
|
|
|
|
if (!open && router.query["dialog"] === name) {
|
|
|
|
setOpen(true);
|
|
|
|
}
|
|
|
|
// allow overriding
|
|
|
|
if (!("open" in dialogProps)) {
|
|
|
|
dialogProps.open = open;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 17:36:55 +00:00
|
|
|
return (
|
2022-03-06 23:06:18 +00:00
|
|
|
<DialogPrimitive.Root {...dialogProps}>
|
2022-04-29 17:17:34 +00:00
|
|
|
<DialogPrimitive.Overlay className="fadeIn fixed inset-0 z-40 bg-black bg-opacity-50 transition-opacity" />
|
2021-08-03 17:36:55 +00:00
|
|
|
{children}
|
|
|
|
</DialogPrimitive.Root>
|
|
|
|
);
|
|
|
|
}
|
2022-05-05 14:29:49 +00:00
|
|
|
type DialogContentProps = React.ComponentProps<typeof DialogPrimitive["Content"]> & {
|
|
|
|
size?: "xl" | "lg";
|
|
|
|
};
|
2021-08-03 17:36:55 +00:00
|
|
|
|
2021-08-23 12:45:25 +00:00
|
|
|
export const DialogContent = React.forwardRef<HTMLDivElement, DialogContentProps>(
|
|
|
|
({ children, ...props }, forwardedRef) => (
|
2022-03-16 19:55:18 +00:00
|
|
|
<DialogPrimitive.Portal>
|
2022-05-05 14:29:49 +00:00
|
|
|
<DialogPrimitive.Overlay className="fadeIn fixed inset-0 z-40 bg-gray-500 bg-opacity-75 transition-opacity" />
|
|
|
|
{/*zIndex one less than Toast */}
|
2022-03-16 19:55:18 +00:00
|
|
|
<DialogPrimitive.Content
|
|
|
|
{...props}
|
2022-05-04 04:06:20 +00:00
|
|
|
className={classNames(
|
2022-05-05 14:29:49 +00:00
|
|
|
"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-0.5 sm:max-w-[98vw]"
|
|
|
|
: props.size == "lg"
|
|
|
|
? "p-6 sm:max-w-[70rem]"
|
|
|
|
: "p-6 sm:max-w-[35rem]",
|
2022-05-06 18:34:48 +00:00
|
|
|
"h-[80vh] max-h-[560px] overflow-scroll overscroll-auto md:h-auto md:max-h-[inherit]",
|
|
|
|
`${props.className || ""}`
|
2022-05-04 04:06:20 +00:00
|
|
|
)}
|
2022-03-16 19:55:18 +00:00
|
|
|
ref={forwardedRef}>
|
|
|
|
{children}
|
|
|
|
</DialogPrimitive.Content>
|
|
|
|
</DialogPrimitive.Portal>
|
2021-08-23 12:45:25 +00:00
|
|
|
)
|
|
|
|
);
|
2021-08-03 17:36:55 +00:00
|
|
|
|
2021-09-14 13:21:18 +00:00
|
|
|
type DialogHeaderProps = {
|
2021-10-18 07:02:25 +00:00
|
|
|
title: React.ReactNode;
|
|
|
|
subtitle?: React.ReactNode;
|
2021-09-14 13:21:18 +00:00
|
|
|
};
|
|
|
|
|
2021-10-18 07:02:25 +00:00
|
|
|
export function DialogHeader(props: DialogHeaderProps) {
|
2021-08-06 12:32:08 +00:00
|
|
|
return (
|
|
|
|
<div className="mb-8">
|
2022-02-09 00:05:13 +00:00
|
|
|
<h3 className="leading-16 font-cal text-xl text-gray-900" id="modal-title">
|
2021-10-18 07:02:25 +00:00
|
|
|
{props.title}
|
2021-08-06 12:32:08 +00:00
|
|
|
</h3>
|
2021-12-15 13:47:38 +00:00
|
|
|
{props.subtitle && <div className="text-sm text-gray-400">{props.subtitle}</div>}
|
2021-10-12 09:35:44 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function DialogFooter(props: { children: ReactNode }) {
|
|
|
|
return (
|
|
|
|
<div>
|
2022-02-09 00:05:13 +00:00
|
|
|
<div className="mt-5 flex justify-end space-x-2 rtl:space-x-reverse">{props.children}</div>
|
2021-08-06 12:32:08 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-03 17:36:55 +00:00
|
|
|
DialogContent.displayName = "DialogContent";
|
|
|
|
|
|
|
|
export const DialogTrigger = DialogPrimitive.Trigger;
|
|
|
|
export const DialogClose = DialogPrimitive.Close;
|