Skip to content

Radix UI Dialog Close Not Working — Fix Close Button

DodaTech Updated 2026-06-24 1 min read

In this fix, you'll learn how to properly close Radix UI Dialogs. This matters for modal dismiss behavior, and appears in confirmation dialogs and forms.

The Problem

The following code demonstrates the typical mistake:

<Dialog>
  <DialogTrigger>Open</DialogTrigger>
  <DialogContent>
    <p>Content</p>
    <button onClick={() => {/* manually close */}}>Close</button>
  </DialogContent>
</Dialog>
// Close button doesn't close the dialog properly

The Fix

Here is the corrected version:

<Dialog>
  <DialogTrigger asChild>
    <Button>Open</Button>
  </DialogTrigger>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Title</DialogTitle>
    </DialogHeader>
    <p>Content here</p>
    <DialogFooter>
      <DialogClose asChild>
        <Button variant="outline">Close</Button>
      </DialogClose>
    </DialogFooter>
  </DialogContent>
</Dialog>
// Dialog closes correctly with Close button

Prevention

  • Use the DialogClose component for close buttons inside dialogs.
  • DialogClose automatically calls the close handler and respects Escape key.
  • Use asChild when DialogClose wraps a styled Button component.
  • DialogClose can be placed anywhere inside DialogContent.
  • Escape key closes the dialog by default (Radix handles this).

Common Mistakes with ui dialog close

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world RADIX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Do I need a DialogClose component?

No — the dialog can be closed by clicking outside (if modal allows), pressing Escape, or via the X button that DialogContent renders automatically. DialogClose is for custom close buttons.

How do I prevent dialog close on specific conditions?

Use the onEscapeKeyDown and onInteractOutside event handlers on DialogContent. Call event.preventDefault() to prevent close. Check your form state before allowing close.

Can I have custom close behavior (like confirm before close)?

Yes — use the onOpenChange handler on Dialog: onOpenChange={(open) => { if (!open && hasUnsavedChanges) { confirmAndClose(); } else { setOpen(open); } }}.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro