import { useEffect, useId, type ReactNode } from 'react'; import { Modal as WPModal } from '@wordpress/components'; import { X } from 'lucide-react'; import { __ } from '@wordpress/i18n'; import { cn } from '@/lib/utils'; import { Button } from './button'; interface ModalProps { open: boolean; onClose: () => void; title: string; /** Line under the title. */ description?: ReactNode; /** Controls beside the close button (e.g. date filters). */ headerActions?: ReactNode; footer?: ReactNode; size?: 'md' | 'lg'; /** 'alertdialog' for confirmations; they also stay open on an outside click. */ role?: 'dialog' | 'alertdialog'; children?: ReactNode; } /** * WordPress's Modal with the app's header (title, description, header * actions, close button), a scrolling body and a footer. Its own header is * hidden: WordPress 6.0's Modal has no slot for a description or actions. * It opens on the first field (or the element marked data-autofocus) * instead of WordPress's first tabbable element, the close button. */ function Modal( { open, onClose, title, description, headerActions, footer, size = 'md', role = 'dialog', children }: ModalProps ) { const id = useId(); useEffect( () => { if ( ! open ) { return undefined; } const timer = window.setTimeout( () => { const content = document.getElementById( `${ id }-content` ); const first = content?.querySelector( '[data-autofocus], textarea, input, select' ); ( first ?? content?.closest( '.components-modal__frame' ) )?.focus(); } ); return () => window.clearTimeout( timer ); }, [ open, id ] ); if ( ! open ) { return null; } return (

{ title }

{ description && (

{ description }

) }
{ headerActions }
{ children }
{ footer &&
{ footer }
}
); } export { Modal };