A shadcn/ui Dialog stays open after the form is submitted
Dialog stays open after clicking the submit buttonShort answer
A shadcn Dialog is a Radix dialog. It closes only when its own state says so. A submit handler does nothing to that state unless you control it. Hold open in useState, pass it with onOpenChange, and call setOpen(false) after the submit work succeeds.
Fix it fast
1Find out which cause you have
Check whether the Dialog has an open prop. No open prop means it is uncontrolled and only its trigger, close button, overlay click or Escape can close it.
grep -rn "<Dialog" app components | grep -v DialogContent2Match what you see, then jump to its fix
- 1
<Dialog> has no open and onOpenChange propsThe Dialog is uncontrolled, so the submit handler cannot close itFix - 2
The X button, Escape and an overlay click do nothingopen is set but onOpenChange is missingFix - 3
The dialog closes instantly and validation errors or the saved result are never seenThe submit button is wrapped in DialogClose, so it closes before the work is doneFix
- 1
What does the error look like?
Dialog stays open after clicking the submit buttonForm submits and the data saves, but the modal remains on screenDialog closes before validation errors can be shown
Cause 1 of 3
The Dialog is uncontrolled, so the submit handler cannot close it
1Check
In the Radix docs the dialog can be controlled or uncontrolled. Without open, only the dialog's own parts change its state.
grep -n "open=" components/my-dialog.tsx2Fix
Keep the state yourself and close it after the async work finishes.
'use client'
import { useState } from 'react'
import { Dialog, DialogContent, DialogTrigger } from '@/components/ui/dialog'
export function EditDialog() {
const [open, setOpen] = useState(false)
async function onSubmit(e: React.FormEvent) {
e.preventDefault()
await saveChanges()
setOpen(false)
}
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger>Edit</DialogTrigger>
<DialogContent>
<form onSubmit={onSubmit}>{/* fields */}</form>
</DialogContent>
</Dialog>
)
}3Verify
Submit once. The request finishes and the dialog disappears.
npm run devConfirmed by radix-ui.com
Cause 2 of 3
open is set but onOpenChange is missing
1Check
Passing open makes the dialog controlled, so its own close actions can only ask you to change state. With no onOpenChange they have nothing to call.
grep -n "open=\|onOpenChange" components/my-dialog.tsx2Fix
Pass onOpenChange with the same setter that owns open.
<Dialog open={open} onOpenChange={setOpen}>3Verify
Open the dialog and press Escape; it should close.
npm run devConfirmed by radix-ui.com
Cause 3 of 3
The submit button is wrapped in DialogClose, so it closes before the work is done
1Check
DialogClose is the part that closes the dialog on click. If it wraps the submit button, the dialog closes on that click whatever the form does next.
grep -rn "DialogClose" components app2Fix
Use a plain submit button inside the form, and close from your handler only on success. Keep DialogClose for Cancel.
<form onSubmit={onSubmit}>
<Button type="submit">Save</Button>
<DialogClose asChild>
<Button type="button" variant="outline">Cancel</Button>
</DialogClose>
</form>3Verify
Submit with an invalid field. The dialog should stay open and show the error.
npm run devConfirmed by radix-ui.com
How do I stop it coming back?
- Control the dialog whenever its content can submit, save or fail.
- Close the dialog from the success path only, never before awaiting the request.
- Keep Cancel as a DialogClose and Save as a normal submit button.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
There is no tailwind.config.js after installing Tailwind 4 and HeroUI in Next.js
HeroUI components render unstyled
Nothing is broken: Tailwind CSS 4 is configured in CSS and no longer creates tailwind.config.js.
3 causes, each with a fixWeb apps
Hydration failed after adding next-themes dark mode
Console reports a server and client mismatch on the html element
next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.
3 causes, each with a fixWeb apps
A Tailwind class does not override the class before it
A utility you added last has no effect on the element
Tailwind does not read your class attribute from left to right.
4 causes, each with a fixWeb apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fixWeb apps
react-i18next shows the wrong plural form for a count, or prints the key instead of the text
t('item') prints the key item instead of a translated sentence
i18next picks a plural from the count option and looks for keys ending in _one, _other and so on.
4 causes, each with a fixWeb apps
useSession throws because there is no SessionProvider above it
[next-auth]: `useSession` must be wrapped in a <SessionProvider />
useSession reads the session from React Context, and SessionProvider is what creates that context.
3 causes, each with a fix