Use createPushModal to define your modal registry and generate the necessary components and functions to manage them. You must provide a modals object where each key represents a modal name and the value is either the component itself or a configuration object containing a Wrapper and the Component.
Modal Configuration Options:
Component: The React component to render inside the modal.Wrapper (optional): A component that handles the modal container/overlay logic. It must accept open, onOpenChange, children, and an optional defaultOpen prop. If not provided, the system defaults to @radix-ui/react-dialog's Root.
To use the system, you must render the returned ModalProvider at the root of your application (or a high-level component) to enable the modal stack.
import { createPushModal } from './path-to-factory';
interface MyModalProps {
title: string;
}
const { ModalProvider, pushModal, popModal } = createPushModal({
modals: {
// Simple component usage
alert: ({ title }: MyModalProps) => <div>{title}</div>,
// Advanced usage with custom Wrapper
settings: {
Wrapper: ({ children, open, onOpenChange }) => (
<div className={open ? 'visible' : 'hidden'}>
<button onClick={() => onOpenChange(false)}>Close</button>
{children}
</div>
),
Component: ({ user }: { user: string }) => <div>Settings for {user}</div>
}
}
});
// In your App root:
function App() {
return (
<ModalProvider>
<YourContent />
</ModalProvider>
);
}