Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A shadcn/ui Dialog stays open after the form is submitted

ErrorDialog stays open after clicking the submit button

Short 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.

  • Applies to shadcn/ui Dialog, Radix Dialog, React 19, Next.js 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 DialogContent
  2. 2Match what you see, then jump to its fix

    1. 1<Dialog> has no open and onOpenChange propsThe Dialog is uncontrolled, so the submit handler cannot close itFix
    2. 2The X button, Escape and an overlay click do nothingopen is set but onOpenChange is missingFix
    3. 3The 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

What does the error look like?

  • Dialog stays open after clicking the submit button
  • Form submits and the data saves, but the modal remains on screen
  • Dialog 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.tsx

2Fix

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 dev

Confirmed by radix-ui.com

Did this fix it?

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.tsx

2Fix

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 dev

Confirmed by radix-ui.com

Did this fix it?

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 app

2Fix

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 dev

Confirmed by radix-ui.com

Did this fix it?

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:

Related fixes

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.