import * as React from 'react'; import * as DialogPrimitive from '@radix-ui/react-dialog'; import { Cross2Icon } from '@radix-ui/react-icons'; import { cn } from '../../lib/utils'; import { useDraggable } from './useDraggable'; interface DialogContextValue { isDraggable?: boolean; shouldCloseOnEsc?: boolean; shouldCloseOnOverlayClick?: boolean; showOverlay?: boolean; } const DialogContext = React.createContext({ isDraggable: false, shouldCloseOnEsc: true, shouldCloseOnOverlayClick: true, }); interface DialogRootProps extends DialogPrimitive.DialogProps { isDraggable?: boolean; shouldCloseOnEsc?: boolean; shouldCloseOnOverlayClick?: boolean; showOverlay?: boolean; } const Dialog = ({ isDraggable, shouldCloseOnEsc = true, shouldCloseOnOverlayClick = true, showOverlay = true, ...props }: DialogRootProps) => ( ); const DialogTrigger = DialogPrimitive.Trigger; const DialogPortal = DialogPrimitive.Portal; const DialogClose = DialogPrimitive.Close; const DialogOverlay = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef & { className?: string; } >(({ className, ...props }, ref) => (
)); DialogOverlay.displayName = DialogPrimitive.Overlay.displayName; interface DialogContentProps extends React.ComponentPropsWithoutRef { unstyled?: boolean; } const DialogContent = React.forwardRef< React.ElementRef, DialogContentProps & { children?: React.ReactNode; } >(({ className, children, unstyled, ...props }, ref) => { const { isDraggable, shouldCloseOnEsc, shouldCloseOnOverlayClick, showOverlay } = React.useContext(DialogContext); const { handlePointerDown, setRefs, initialTransform } = useDraggable( { enabled: isDraggable, }, ref ); // When not isDraggable, Tailwind centers the dialog. // When isDraggable, we remove the built‑in centering so our inline transform takes over. const contentClassName = cn( unstyled ? '' : 'w-full', 'max-w-md bg-muted data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] fixed left-[50%] top-[50%] z-50 grid gap-4 p-4 shadow-lg duration-200 sm:rounded-lg', !isDraggable ? 'translate-x-[-50%] translate-y-[-50%]' : '', className ); const style = isDraggable ? { ...props.style, transform: initialTransform } : props.style; const content = ( { if (!shouldCloseOnEsc) { event.preventDefault(); } }} onInteractOutside={event => { if (!shouldCloseOnOverlayClick) { event.preventDefault(); } }} > {children} {!unstyled && ( Close )} ); return ( {showOverlay && !isDraggable && } {content} ); }); DialogContent.displayName = DialogPrimitive.Content.displayName; const DialogHeader = ({ className, ...props }: React.HTMLAttributes) => { return (
{props.children}
); }; DialogHeader.displayName = 'DialogHeader'; const DialogFooter = ({ className, ...props }: React.HTMLAttributes) => (
); DialogFooter.displayName = 'DialogFooter'; const DialogTitle = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef & { className?: string; } >(({ className, ...props }, ref) => ( )); DialogTitle.displayName = DialogPrimitive.Title.displayName; const DialogDescription = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef & { className?: string; } >(({ className, ...props }, ref) => ( )); DialogDescription.displayName = DialogPrimitive.Description.displayName; export { Dialog, DialogPortal, DialogOverlay, DialogTrigger, DialogClose, DialogContent, DialogHeader, DialogFooter, DialogTitle, DialogDescription, };