shadcn/ui Dialog Not Closing— After Submit & Programmatic [Fixed 2026]
Also covers: controlled vs uncontrolled · onOpenChange · form submit close · async close · prevent outside close
⚡ The Symptom
// 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
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.
Control Dialog Open State with useState
Foundation — required for programmatic close"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>
)
}Close Dialog After Form Submit
Most common use case — submit then close"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>
)
}Close Dialog After Server Action
useActionState + setOpen after action resolves"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>
)
}Prevent Dialog Closing on Overlay Click
Sticky dialog — only close programmatically<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).
Close Dialog from a Nested Child Component
Close button inside a deeply nested componentPass setOpen as a prop, or use React Context to share the close function with deeply nested children.
// 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
- Always use controlled Dialog (useState + open + onOpenChange) when you need programmatic close
- Close Dialog only on success — let it stay open on error so the user can see the error message
- Use useEffect watching state.success to close Dialog after Server Actions complete
- Pass setOpen as a prop to child components that need to trigger close
- Use DialogClose asChild for simple cancel buttons — no need for custom close logic
- Reset form state after closing the Dialog to prevent stale data on next open
Frequently Asked Questions
Why does my shadcn/ui Dialog not close when I click outside?+−
How do I close a shadcn Dialog programmatically?+−
How do I close a shadcn Dialog after a form submits successfully?+−
What is the difference between controlled and uncontrolled Dialog in shadcn?+−
Why does my Dialog reopen immediately after I close it?+−
Can I have a Dialog without a close button?+−
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