shadcn/uiDialogError Fix2026 Updated

shadcn/ui Dialog Not Closing— After Submit & Programmatic [Fixed 2026]

Also covers: controlled vs uncontrolled · onOpenChange · form submit close · async close · prevent outside close

January 20266 min readshadcn/ui · Radix UI · React

⚡ The Symptom

Error
// Submit button clicked — form submits — Dialog stays open
// OR: Close button works but Dialog reopens immediately
// OR: Need to close Dialog from inside a nested component

✅ Fix — control open state with useState

Controlled Dialog pattern
const [open, setOpen] = useState(false)

<Dialog open={open} onOpenChange={setOpen}>
  <DialogTrigger asChild><Button>Open</Button></DialogTrigger>
  <DialogContent>
    <button onClick={() => setOpen(false)}>Close</button>
  </DialogContent>
</Dialog>

Why shadcn/ui Dialog Doesn't Close

shadcn/ui Dialog is built on Radix UI's Dialog primitive, which supports both controlled and uncontrolled modes. When using the uncontrolled mode (no open prop), the Dialog manages its own state. If you need to close it from outside — after a form submit, async action, or programmatically — you must switch to controlled mode with useState.

1

Control Dialog Open State with useState

Foundation — required for programmatic close
2 min
Controlled Dialog — complete pattern
"use client"

import { useState } from "react"
import {
  Dialog, DialogContent, DialogHeader,
  DialogTitle, DialogTrigger,
} from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"

export function MyDialog() {
  const [open, setOpen] = useState(false)

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild>
        <Button onClick={() => setOpen(true)}>Open Dialog</Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>My Dialog</DialogTitle>
        </DialogHeader>
        <p>Dialog content here</p>
        <Button onClick={() => setOpen(false)}>Close</Button>
      </DialogContent>
    </Dialog>
  )
}
2

Close Dialog After Form Submit

Most common use case — submit then close
3 min
Close after successful form submission
"use client"

import { useState } from "react"
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"

export function CreateUserDialog() {
  const [open, setOpen] = useState(false)
  const [name, setName] = useState("")
  const [loading, setLoading] = useState(false)

  async function handleSubmit(e: React.FormEvent) {
    e.preventDefault()
    setLoading(true)

    try {
      await createUser({ name })   // your API call
      setOpen(false)               // ← close only on success
      setName("")                  // reset form
    } catch (err) {
      console.error(err)           // stay open on error — show feedback
    } finally {
      setLoading(false)
    }
  }

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild><Button>Create User</Button></DialogTrigger>
      <DialogContent>
        <DialogHeader><DialogTitle>Create User</DialogTitle></DialogHeader>
        <form onSubmit={handleSubmit}>
          <Input value={name} onChange={e => setName(e.target.value)} placeholder="Name" />
          <Button type="submit" disabled={loading}>
            {loading ? "Creating..." : "Create"}
          </Button>
        </form>
      </DialogContent>
    </Dialog>
  )
}
3

Close Dialog After Server Action

useActionState + setOpen after action resolves
3 min
Dialog + Server Action + close on success
"use client"

import { useActionState } from "react"
import { useEffect, useState } from "react"
import { createPost } from "@/app/actions"

export function CreatePostDialog() {
  const [open, setOpen] = useState(false)
  const [state, formAction, isPending] = useActionState(createPost, null)

  // Close dialog when action succeeds
  useEffect(() => {
    if (state?.success) {
      setOpen(false)
    }
  }, [state?.success])

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild><Button>New Post</Button></DialogTrigger>
      <DialogContent>
        <form action={formAction}>
          <Input name="title" placeholder="Post title" required />
          {state?.error && <p className="text-red-500 text-sm">{state.error}</p>}
          <Button type="submit" disabled={isPending}>
            {isPending ? "Creating..." : "Create Post"}
          </Button>
        </form>
      </DialogContent>
    </Dialog>
  )
}
4

Prevent Dialog Closing on Overlay Click

Sticky dialog — only close programmatically
2 min
Prevent outside click from closing
<DialogContent
  onInteractOutside={(e) => e.preventDefault()}
  onEscapeKeyDown={(e) => e.preventDefault()}
>
  {/* Dialog content — can only close via setOpen(false) */}
  <p>This dialog won't close on outside click or Escape</p>
  <Button onClick={() => setOpen(false)}>Close manually</Button>
</DialogContent>

Use this pattern sparingly — preventing Escape key close is an accessibility concern. Reserve it for operations that must not be accidentally dismissed (e.g., data loss confirmation, critical form completion).

5

Close Dialog from a Nested Child Component

Close button inside a deeply nested component
3 min

Pass setOpen as a prop, or use React Context to share the close function with deeply nested children.

Pass setOpen via props
// Parent
function ParentDialog() {
  const [open, setOpen] = useState(false)

  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogContent>
        <NestedForm onSuccess={() => setOpen(false)} />
      </DialogContent>
    </Dialog>
  )
}

// Child — receives close callback via prop
function NestedForm({ onSuccess }: { onSuccess: () => void }) {
  async function handleSubmit() {
    await saveData()
    onSuccess()    // closes the dialog
  }
  return <form onSubmit={handleSubmit}>...</form>
}

// ✅ Alternative — use DialogClose for simple close buttons
import { DialogClose } from "@/components/ui/dialog"

<DialogClose asChild>
  <Button variant="outline">Cancel</Button>
</DialogClose>

Prevention

Frequently Asked Questions

Why does my shadcn/ui Dialog not close when I click outside?+
shadcn/ui Dialog closes on overlay click by default (Radix UI behaviour). If it's not closing, either you've set onInteractOutside to preventDefault, or the Dialog is uncontrolled but something is re-opening it immediately. Check for state that is setting open to true on every render.
How do I close a shadcn Dialog programmatically?+
Control the Dialog with useState: const [open, setOpen] = useState(false). Pass open and onOpenChange={setOpen} to the Dialog. To close it, call setOpen(false) from anywhere — a button click, form submit callback, or after an async action.
How do I close a shadcn Dialog after a form submits successfully?+
Use controlled state with useState. In your submit handler, after the action completes successfully, call setOpen(false). For Server Actions, wait for the action to resolve, then close. If using react-hook-form, call setOpen(false) in the onSubmit function after awaiting the action.
What is the difference between controlled and uncontrolled Dialog in shadcn?+
Uncontrolled Dialog manages its own open/closed state internally — useful for simple cases like a trigger button. Controlled Dialog uses your state (useState) via open and onOpenChange props — required when you need to close the Dialog from outside, like after a form submit or an API call.
Why does my Dialog reopen immediately after I close it?+
This happens when the state managing the Dialog is being reset to true on every render. Check if the open prop is tied to a value that changes back to true — like a derived state that recalculates, or a parent component re-rendering and passing a constant true.
Can I have a Dialog without a close button?+
Yes. Remove the DialogClose component and set onInteractOutside={(e) => e.preventDefault()} on DialogContent to prevent closing on overlay click. This creates a 'sticky' dialog that can only be closed programmatically with setOpen(false).

Need Expert Help?

We Build Complex shadcn/ui UI Patterns

Softplix engineers implement dialogs, sheets, command palettes, and complex UI flows with shadcn/ui and React. Let us help.

Talk to an Engineer