import { forwardRef, useCallback } from 'react' import { ModalV2Props } from './modalV2.types' import { useResponsiveTokens } from '../../hooks/useResponsiveTokens' import type { ModalV2TokensType } from './modalV2.tokens.types' import { useBreakpoints } from '../../hooks/useBreakPoints' import { useModal } from '../Modal/useModal' import { useId } from 'react' import Block from '../Primitives/Block/Block' import MobileModalV2 from './mobileModalV2' import styled from 'styled-components' import { modalBackdropAnimationsV2 } from './modalAnimationV2' import { modalContentAnimationsV2 } from './modalAnimationV2' import ModalV2Header from './ModalV2Header' import ModalV2Skeleton from './ModalV2Skeleton' import ModalV2Footer from './ModalV2Footer' import { createPortal } from 'react-dom' import { filterBlockedProps } from '../../utils/prop-helpers' const AnimatedBackdrop = styled(Block)<{ $isAnimatingIn: boolean }>` ${({ $isAnimatingIn }) => modalBackdropAnimationsV2($isAnimatingIn)} ` const AnimatedModalContent = styled(Block)<{ $isAnimatingIn: boolean }>` ${({ $isAnimatingIn }) => modalContentAnimationsV2($isAnimatingIn)} ` const ModalV2 = forwardRef( ( { isOpen, isCustom = false, onClose, title, subtitle, children, primaryAction, secondaryAction, showCloseButton = true, showHeader = true, showFooter = true, closeOnBackdropClick = true, customHeader, customFooter, headerSlot, showDivider = true, dimensions = {}, useDrawerOnMobile = true, skeleton, ...props }, ref ) => { const filteredRest = filterBlockedProps(props) const { 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledBy, 'aria-describedby': ariaDescribedBy, ...dialogProps } = filteredRest const modalTokens = useResponsiveTokens('MODALV2') const { innerWidth } = useBreakpoints() const isMobile = innerWidth < 1024 const { shouldRender, isAnimatingIn, portalContainer } = useModal( isOpen, onClose ) const baseId = useId() const shouldUseGeneratedHeader = showHeader && !customHeader const titleId = shouldUseGeneratedHeader && title ? `${baseId}-title` : undefined const subtitleId = shouldUseGeneratedHeader && subtitle ? `${baseId}-subtitle` : undefined const handleBackdropClick = useCallback(() => { if (closeOnBackdropClick) { onClose() } }, [closeOnBackdropClick, onClose]) if (!shouldRender || !portalContainer) return null const shouldShowSkeleton = skeleton?.show const skeletonVariant = skeleton?.variant || 'pulse' const shouldShowBodySkeleton = Boolean( shouldShowSkeleton && skeleton?.bodySkeletonProps?.show ) const hasFooterContent = Boolean( customFooter || primaryAction || secondaryAction || shouldShowSkeleton ) const { width, height, minWidth = '', minHeight = '', maxWidth = '90vw', maxHeight = '90vh', } = dimensions const modalContent = (() => { if (isMobile && useDrawerOnMobile) { return ( {children} ) } return ( ) })() return createPortal(modalContent, portalContainer) } ) ModalV2.displayName = 'ModalV2' export default ModalV2