cal.pub0.org/packages/ui/v2/Dialog.tsx

154 lines
4.9 KiB
TypeScript
Raw Normal View History

Feat/design system (#3051) * Storybook Boilerplate setup * Inital Setup * First story * Color Design System * Badge Story + Comp * Checkbox UI + Stories * Update Red colors + Button Group * Switch+Stories / Default brand color * Update Version + Button Group combined * Compact Butotn Group * Tidy up Selectors * Adds Tooltip to Button * TextInput * Update SB * Prefix Input * Match text area styles * Prefix Controls * Update spacing on text area * Text Input Suffix * Color Picker * Update storybook * Icon Suffix/Prefix * Datepicker + move components to monorepo * Text color on labels * Move Radio over to monorepo * Move CustomBranding to calcom/ib * Radio * IconBadge Component * Update radio indicator background * Disabled radio state * Delete yarn.lock * Revert "Delete yarn.lock" This reverts commit 9b99d244b70872153a16bec1f1f3bc651e94be7a. * Fix webhook test * Replace old toast location * Update radio path * Empty State * Update Badge.tsx * Update Badge.tsx * Banner Component+story * Creation Modal * Creation Dialog updated * Button hover dialog * Confirmation Modal * Datepicker (Booking) * PageHeader * Fix border width * PageHeader update search bar * Fix input height * Fix button group size * Add spacing between badges - font smoothing * Update button position on banner * Banner update * Fixing focus state on suffix/prefix inputs * Implement A11y addon * Add aria label * error && "text-red-800" * Fix button hover * Change colors * Generate snapshot tests for on hover button * Revert colors to demo * Change colors * Fix Linear Issues * Form Stepper component * Add padding back to input * Move ui to UI_V2 * Use V2 * Update imports for v1 * Update imports for v1 * Upgrade to nextjs in storybook root * Update website submodule * Avatar Groups * Fix webpack again * Vertical Tab Item [WIP] - active state on small item is not working currently * Vertical Tab Group * Add Github action * Fix website submodule * Fix GH action * Rename Workflow * Adds lint report for CI * Lint report fixes * NavigationItem comments * VerticalTabItem type fixes * Fix avatar blur * Fix comments * Adding isEmbed to window object * Disable components that use router mock. * Load inter via google fonts * Started select * Adding base Breadcrumb * Update readme * Formatting * Fixes * Dependencies matching * Linting * Update FormStep.stories.tsx * Linting * Update MultiSelectCheckboxes.tsx Co-authored-by: zomars <zomars@me.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>
2022-07-23 00:39:50 +00:00
import * as DialogPrimitive from "@radix-ui/react-dialog";
import { useRouter } from "next/router";
import React, { ReactNode, useState, MouseEvent } from "react";
import { Icon } from "react-feather";
import classNames from "@calcom/lib/classNames";
import Button from "./Button";
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 {
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;
}
}
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"]> & {
size?: "xl" | "lg";
type?: "creation" | "confirmation";
title?: string;
description?: string;
closeText?: string;
actionText?: string;
Icon?: Icon;
actionOnClick?: () => void;
};
export const DialogContent = React.forwardRef<HTMLDivElement, DialogContentProps>(
({ children, Icon, ...props }, forwardedRef) => (
<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-0.5 sm:max-w-[98vw]"
: props.size == "lg"
? "p-6 sm:max-w-[70rem]"
: "p-6 sm:max-w-[35rem]",
"max-h-[560px] overflow-visible overscroll-auto md:h-auto md:max-h-[inherit]",
`${props.className || ""}`
)}
ref={forwardedRef}>
{props.type === "creation" && (
<div className="pb-8">
{props.title && <DialogHeader title={props.title} />}
{props.description && <p className="pb-8 text-sm text-gray-500">Optional Description</p>}
<div className="flex flex-col gap-6">{children}</div>
</div>
)}
{props.type === "confirmation" && (
<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>
)}
<div>
{props.title && <DialogHeader title={props.title} />}
{props.description && <p className="mb-6 text-sm text-gray-500">Optional Description</p>}
</div>
</div>
)}
<DialogFooter>
<DialogClose asChild>
{/* This will require the i18n string passed in */}
<Button color="minimal">{props.closeText ?? "Close"}</Button>
</DialogClose>
<Button color="primary" onClick={props.actionOnClick}>
{props.actionText}
</Button>
</DialogFooter>
</DialogPrimitive.Content>
</DialogPrimitive.Portal>
)
);
type DialogHeaderProps = {
title: React.ReactNode;
subtitle?: React.ReactNode;
};
export function DialogHeader(props: DialogHeaderProps) {
return (
<>
<h3 className="leading-20 text-semibold font-cal text-xl text-black" id="modal-title">
{props.title}
</h3>
{props.subtitle && <div className="text-sm text-gray-400">{props.subtitle}</div>}
</>
);
}
export function DialogFooter(props: { children: ReactNode }) {
return (
<div>
<div className="mt-5 flex justify-end space-x-2 rtl:space-x-reverse">{props.children}</div>
</div>
);
}
DialogContent.displayName = "DialogContent";
export const DialogTrigger = DialogPrimitive.Trigger;
export const DialogClose = DialogPrimitive.Close;