/** * Copyright (c) TonTech. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import { forwardRef, useCallback, useEffect, useId } from 'react'; import { createPortal } from 'react-dom'; import type { ComponentPropsWithoutRef, ComponentRef, FC, ReactNode } from 'react'; import { DialogContext, useDialogContext } from './use-dialog-context'; interface DialogRootProps { children: ReactNode; open?: boolean; onOpenChange?: (open: boolean) => void; } const DialogRoot: FC = ({ children, open = false, onOpenChange }) => { const titleId = useId(); const handleOpenChange = useCallback((value: boolean) => onOpenChange?.(value), [onOpenChange]); return ( {children} ); }; interface DialogPortalProps { children: ReactNode; container?: Element | null; } const DialogPortal: FC = ({ children, container }) => { const { open } = useDialogContext(); if (!open || typeof document === 'undefined') return null; return createPortal(children, container ?? document.body); }; const DialogOverlay = forwardRef, ComponentPropsWithoutRef<'div'>>((props, ref) => { useEffect(() => { document.body.style.overflow = 'hidden'; return () => { document.body.style.overflow = ''; }; }, []); return
; }); DialogOverlay.displayName = 'DialogOverlay'; const DialogContent = forwardRef, ComponentPropsWithoutRef<'div'>>((props, ref) => { const { onOpenChange, titleId } = useDialogContext(); useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') onOpenChange(false); }; document.addEventListener('keydown', handleKeyDown); return () => document.removeEventListener('keydown', handleKeyDown); }, [onOpenChange]); return
; }); DialogContent.displayName = 'DialogContent'; const DialogTitle = forwardRef, ComponentPropsWithoutRef<'h2'>>((props, ref) => { const { titleId } = useDialogContext(); return

; }); DialogTitle.displayName = 'DialogTitle'; const DialogClose = forwardRef, ComponentPropsWithoutRef<'button'>>( ({ onClick, ...props }, ref) => { const { onOpenChange } = useDialogContext(); return (