React Easy Modals
is a headless, zero-dependency modal stack for React. Open a modal like you call a function, and get its answer back.No answer yetconst modals = useModals(); async function deleteProject() { const answer = await modals.open(ConfirmDelete, { project: "acme-web", }); if (answer === "delete") removeProject();}Get started
Install. One package, no dependencies besides React.
npm install react-easy-modalsAdd the provider. It renders the stack at the root of your app.
<ModalProvider> <App /></ModalProvider>Open a modal. Any component works. Its close value is the result.
const ok = await modals.open(Confirm);
Features
A new modal hides the one below it until it closes.
- Calls show up here.
function StepModal({ close, step, onNext }) { return ( <Modal> <ModalContent> <ModalTitle>Step {step}</ModalTitle> <Button onClick={() => close()}>Back</Button> <Button onClick={onNext}>Next step</Button> </ModalContent> </Modal> );} // Each step waits underneath the next one until it closesconst openStep = (step: number) => modals.open(StepModal, { step, onNext: () => openStep(step + 1), });shadcn/ui
Add a Modal built on your Base UI Dialog, from the @alexandrejoly registry.
npx shadcn@latest add @alexandrejoly/easy-modalRemoved: <Dialog open={open} onOpenChange={setOpen}>Removed: <DialogContent>…</DialogContent>Removed: </Dialog>Added: const result = await modals.open(MyModal);Docs
- Getting Started
Install react-easy-modals, add the provider and open your first modal.
- How It Works
The modal stack, opening and closing modals, and the props each modal receives.
- Props
The props every modal component receives.
- Transitions
Animate a modal out before it unmounts with useBeforeClose.
- Lazy Loading
Load modal components on demand with dynamic imports.
- Nested Modals
Open modals inside other modals and close any level of the chain.
- Registry
A ready-made shadcn/ui Modal built on the Base UI Dialog.
- ModalProvider
The provider that renders the modal stack, its backdrop and its loading state.
- useModals
Open and close modals, and read the current stack.
- useModal
Read the current modal's props from any child component.
- useModalAt
Reach a parent or the root modal from inside a nested modal.
- useBeforeClose
Block or delay a modal's close, for confirmations and exit animations.