import * as React from 'react'; import * as DialogPrimitive from '@radix-ui/react-dialog'; import { X } from 'lucide-react'; import { cn } from '~/utils'; interface OGDialogProps extends DialogPrimitive.DialogProps { triggerRef?: React.RefObject; triggerRefs?: React.RefObject[]; } const Dialog = React.forwardRef( ({ children, triggerRef, triggerRefs, onOpenChange, ...props }, _ref) => { const handleOpenChange = (open: boolean) => { if (!open && triggerRef?.current) { setTimeout(() => { triggerRef.current?.focus(); }, 0); } if (triggerRefs?.length) { triggerRefs.forEach((ref) => { if (ref?.current) { setTimeout(() => { ref.current?.focus(); }, 0); } }); } onOpenChange?.(open); }; return ( {children} ); }, ); const DialogTrigger = DialogPrimitive.Trigger; const DialogPortal = DialogPrimitive.Portal; const DialogClose = DialogPrimitive.Close; export const DialogOverlay = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); DialogOverlay.displayName = DialogPrimitive.Overlay.displayName; type DialogContentProps = React.ComponentPropsWithoutRef & { showCloseButton?: boolean; disableScroll?: boolean; overlayClassName?: string; }; const DialogContent = React.forwardRef< React.ElementRef, DialogContentProps >( ( { className, overlayClassName, showCloseButton = true, children, onEscapeKeyDown: propsOnEscapeKeyDown, ...props }, ref, ) => { /* Handle Escape key to prevent closing dialog if a tooltip is open (this is a workaround in order to achieve WCAG compliance which requires that our tooltips be dismissable with Escape key) */ const handleEscapeKeyDown = React.useCallback( (event: KeyboardEvent) => { const tooltips = document.querySelectorAll('.tooltip'); for (const tooltip of Array.from(tooltips)) { const computedStyle = window.getComputedStyle(tooltip); const opacity = parseFloat(computedStyle.opacity); if ( tooltip.parentElement && computedStyle.display !== 'none' && computedStyle.visibility !== 'hidden' && opacity > 0 ) { event.preventDefault(); return; } } // Call the original handler if it exists propsOnEscapeKeyDown?.(event); }, [propsOnEscapeKeyDown], ); return ( {children} {showCloseButton && ( )} ); }, ); DialogContent.displayName = DialogPrimitive.Content.displayName; const DialogHeader = ({ className, ...props }: React.HTMLAttributes) => (
); DialogHeader.displayName = 'DialogHeader'; const DialogFooter = ({ className, ...props }: React.HTMLAttributes) => (
); DialogFooter.displayName = 'DialogFooter'; const DialogTitle = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); DialogTitle.displayName = DialogPrimitive.Title.displayName; const DialogDescription = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, ...props }, ref) => ( )); DialogDescription.displayName = DialogPrimitive.Description.displayName; export { Dialog as OGDialog, DialogPortal as OGDialogPortal, DialogOverlay as OGDialogOverlay, DialogClose as OGDialogClose, DialogTrigger as OGDialogTrigger, DialogContent as OGDialogContent, DialogHeader as OGDialogHeader, DialogFooter as OGDialogFooter, DialogTitle as OGDialogTitle, DialogDescription as OGDialogDescription, };