import { Dialog as MuiDialog, SxProps, Theme } from '@mui/material'; import { ForwardRefExoticComponent, PropsWithChildren, ReactNode, RefAttributes, createContext, forwardRef, useImperativeHandle, useMemo, useState, } from 'react'; import { Panel } from '../Panels'; import Spinner from '../Spinner'; const DialogContext = createContext( undefined, ); const Dialog: ForwardRefExoticComponent< PropsWithChildren & RefAttributes > = forwardRef((props, ref) => { const { children: externalChildren, dialogProps = {}, loading, openInitially = false, wide, } = props; const { // Do not initialize the external open state because we need it to // determine whether the dialog is controlled or uncontrolled. open: externalOpen, PaperProps: paperProps = {}, ...restDialogProps } = dialogProps; const { sx: externalPaperSx, ...restPaperProps } = paperProps; const [controlOpen, setControlOpen] = useState(openInitially); const open = useMemo( () => externalOpen ?? controlOpen, [controlOpen, externalOpen], ); const children = useMemo( () => (loading ? : externalChildren), [externalChildren, loading], ); const paperSx = useMemo>( () => ({ minWidth: wide ? { xs: 'calc(100%)', md: '50em' } : null, overflow: 'visible', ...externalPaperSx, }), [externalPaperSx, wide], ); useImperativeHandle( ref, () => ({ open, setOpen: setControlOpen, }), [open], ); return ( {children} ); }); Dialog.displayName = 'Dialog'; export { DialogContext }; export default Dialog;