2024-05-27 14:30:25 -04:00
|
|
|
|
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';
|
2025-02-28 15:17:55 -05:00
|
|
|
|
import { useDraggable } from './useDraggable';
|
2024-05-27 14:30:25 -04:00
|
|
|
|
|
2025-02-28 15:17:55 -05:00
|
|
|
|
interface DialogContextValue {
|
|
|
|
|
|
isDraggable?: boolean;
|
|
|
|
|
|
shouldCloseOnEsc?: boolean;
|
|
|
|
|
|
shouldCloseOnOverlayClick?: boolean;
|
|
|
|
|
|
showOverlay?: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const DialogContext = React.createContext<DialogContextValue>({
|
|
|
|
|
|
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) => (
|
|
|
|
|
|
<DialogContext.Provider
|
|
|
|
|
|
value={{ isDraggable, shouldCloseOnEsc, shouldCloseOnOverlayClick, showOverlay }}
|
|
|
|
|
|
>
|
|
|
|
|
|
<DialogPrimitive.Root {...props} />
|
|
|
|
|
|
</DialogContext.Provider>
|
|
|
|
|
|
);
|
2024-05-27 14:30:25 -04:00
|
|
|
|
|
|
|
|
|
|
const DialogTrigger = DialogPrimitive.Trigger;
|
|
|
|
|
|
|
|
|
|
|
|
const DialogPortal = DialogPrimitive.Portal;
|
|
|
|
|
|
|
|
|
|
|
|
const DialogClose = DialogPrimitive.Close;
|
|
|
|
|
|
|
|
|
|
|
|
const DialogOverlay = React.forwardRef<
|
|
|
|
|
|
React.ElementRef<typeof DialogPrimitive.Overlay>,
|
2025-02-28 15:17:55 -05:00
|
|
|
|
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay> & {
|
|
|
|
|
|
className?: string;
|
|
|
|
|
|
}
|
2024-05-27 14:30:25 -04:00
|
|
|
|
>(({ className, ...props }, ref) => (
|
2025-02-28 15:17:55 -05:00
|
|
|
|
<div
|
2024-05-27 14:30:25 -04:00
|
|
|
|
ref={ref}
|
|
|
|
|
|
className={cn(
|
2025-02-28 15:17:55 -05:00
|
|
|
|
'data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 fixed inset-0 z-40 bg-black/60',
|
2024-05-27 14:30:25 -04:00
|
|
|
|
className
|
|
|
|
|
|
)}
|
|
|
|
|
|
{...props}
|
|
|
|
|
|
/>
|
|
|
|
|
|
));
|
|
|
|
|
|
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName;
|
|
|
|
|
|
|
2025-02-28 15:17:55 -05:00
|
|
|
|
interface DialogContentProps
|
2025-03-25 09:32:29 -04:00
|
|
|
|
extends React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> {
|
|
|
|
|
|
unstyled?: boolean;
|
|
|
|
|
|
}
|
2025-02-28 15:17:55 -05:00
|
|
|
|
|
2024-05-27 14:30:25 -04:00
|
|
|
|
const DialogContent = React.forwardRef<
|
|
|
|
|
|
React.ElementRef<typeof DialogPrimitive.Content>,
|
2025-02-28 15:17:55 -05:00
|
|
|
|
DialogContentProps & {
|
|
|
|
|
|
children?: React.ReactNode;
|
|
|
|
|
|
}
|
2025-03-25 09:32:29 -04:00
|
|
|
|
>(({ className, children, unstyled, ...props }, ref) => {
|
2025-02-28 15:17:55 -05:00
|
|
|
|
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(
|
2025-03-25 09:32:29 -04:00
|
|
|
|
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',
|
2025-02-28 15:17:55 -05:00
|
|
|
|
!isDraggable ? 'translate-x-[-50%] translate-y-[-50%]' : '',
|
|
|
|
|
|
className
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
const style = isDraggable ? { ...props.style, transform: initialTransform } : props.style;
|
|
|
|
|
|
|
|
|
|
|
|
const content = (
|
2024-05-27 14:30:25 -04:00
|
|
|
|
<DialogPrimitive.Content
|
2025-02-28 15:17:55 -05:00
|
|
|
|
ref={setRefs}
|
|
|
|
|
|
className={contentClassName}
|
2024-05-27 14:30:25 -04:00
|
|
|
|
{...props}
|
2025-02-28 15:17:55 -05:00
|
|
|
|
style={style}
|
|
|
|
|
|
onPointerDown={isDraggable ? handlePointerDown : props.onPointerDown}
|
|
|
|
|
|
onEscapeKeyDown={event => {
|
|
|
|
|
|
if (!shouldCloseOnEsc) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
}}
|
|
|
|
|
|
onInteractOutside={event => {
|
|
|
|
|
|
if (!shouldCloseOnOverlayClick) {
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
}}
|
2024-05-27 14:30:25 -04:00
|
|
|
|
>
|
|
|
|
|
|
{children}
|
2025-03-25 09:32:29 -04:00
|
|
|
|
{!unstyled && (
|
|
|
|
|
|
<DialogPrimitive.Close className="ring-offset-background focus:ring-ring data-[state=open]:bg-accent data-[state=open]:text-muted-foreground absolute right-4 top-4 rounded-sm opacity-70 transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:pointer-events-none">
|
|
|
|
|
|
<Cross2Icon className="text-primary h-4 w-4" />
|
|
|
|
|
|
<span className="sr-only">Close</span>
|
|
|
|
|
|
</DialogPrimitive.Close>
|
|
|
|
|
|
)}
|
2024-05-27 14:30:25 -04:00
|
|
|
|
</DialogPrimitive.Content>
|
2025-02-28 15:17:55 -05:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<DialogPortal>
|
|
|
|
|
|
{showOverlay && !isDraggable && <DialogOverlay />}
|
|
|
|
|
|
{content}
|
|
|
|
|
|
</DialogPortal>
|
|
|
|
|
|
);
|
|
|
|
|
|
});
|
2024-05-27 14:30:25 -04:00
|
|
|
|
DialogContent.displayName = DialogPrimitive.Content.displayName;
|
|
|
|
|
|
|
2025-02-28 15:17:55 -05:00
|
|
|
|
const DialogHeader = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div
|
|
|
|
|
|
className={cn(
|
|
|
|
|
|
'drag-handle relative flex select-none flex-col space-y-1.5 text-center sm:text-left',
|
|
|
|
|
|
className
|
|
|
|
|
|
)}
|
|
|
|
|
|
{...props}
|
|
|
|
|
|
>
|
|
|
|
|
|
{props.children}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
2024-05-27 14:30:25 -04:00
|
|
|
|
DialogHeader.displayName = 'DialogHeader';
|
|
|
|
|
|
|
|
|
|
|
|
const DialogFooter = ({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) => (
|
|
|
|
|
|
<div
|
|
|
|
|
|
className={cn('flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2', className)}
|
|
|
|
|
|
{...props}
|
|
|
|
|
|
/>
|
|
|
|
|
|
);
|
|
|
|
|
|
DialogFooter.displayName = 'DialogFooter';
|
|
|
|
|
|
|
|
|
|
|
|
const DialogTitle = React.forwardRef<
|
|
|
|
|
|
React.ElementRef<typeof DialogPrimitive.Title>,
|
2025-02-28 15:17:55 -05:00
|
|
|
|
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title> & {
|
|
|
|
|
|
className?: string;
|
|
|
|
|
|
}
|
2024-05-27 14:30:25 -04:00
|
|
|
|
>(({ className, ...props }, ref) => (
|
|
|
|
|
|
<DialogPrimitive.Title
|
|
|
|
|
|
ref={ref}
|
2025-02-28 15:17:55 -05:00
|
|
|
|
className={cn('text-primary-light text-xl font-normal leading-none tracking-tight', className)}
|
2024-05-27 14:30:25 -04:00
|
|
|
|
{...props}
|
|
|
|
|
|
/>
|
|
|
|
|
|
));
|
|
|
|
|
|
DialogTitle.displayName = DialogPrimitive.Title.displayName;
|
|
|
|
|
|
|
|
|
|
|
|
const DialogDescription = React.forwardRef<
|
|
|
|
|
|
React.ElementRef<typeof DialogPrimitive.Description>,
|
2025-02-28 15:17:55 -05:00
|
|
|
|
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description> & {
|
|
|
|
|
|
className?: string;
|
|
|
|
|
|
}
|
2024-05-27 14:30:25 -04:00
|
|
|
|
>(({ className, ...props }, ref) => (
|
|
|
|
|
|
<DialogPrimitive.Description
|
|
|
|
|
|
ref={ref}
|
2025-02-28 15:17:55 -05:00
|
|
|
|
className={cn('text-base', className)}
|
2024-05-27 14:30:25 -04:00
|
|
|
|
{...props}
|
|
|
|
|
|
/>
|
|
|
|
|
|
));
|
|
|
|
|
|
DialogDescription.displayName = DialogPrimitive.Description.displayName;
|
|
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
Dialog,
|
|
|
|
|
|
DialogPortal,
|
|
|
|
|
|
DialogOverlay,
|
|
|
|
|
|
DialogTrigger,
|
|
|
|
|
|
DialogClose,
|
|
|
|
|
|
DialogContent,
|
|
|
|
|
|
DialogHeader,
|
|
|
|
|
|
DialogFooter,
|
|
|
|
|
|
DialogTitle,
|
|
|
|
|
|
DialogDescription,
|
|
|
|
|
|
};
|