# Dialog

An accessible modal window built on Radix Dialog, with overlay, header, footer, title, description, and an optional corner close button.

- Category: Overlays & Navigation
- License: MIT
- Page: https://www.saasuji.com/components/dialog
- Install: npx shadcn@latest add https://www.saasuji.com/r/dialog.json

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| open (Dialog) | `boolean` | — | Controlled open state of the dialog. |
| defaultOpen (Dialog) | `boolean` | `false` | Open state on first render for an uncontrolled dialog. |
| onOpenChange (Dialog) | `(open: boolean) => void` | — | Called when the open state changes. |
| modal (Dialog) | `boolean` | `true` | Traps focus and blocks interaction with the rest of the page while open. |
| showCloseButton (DialogContent) | `boolean` | `true` | Renders the corner close button inside the content. |
| overlayClassName (DialogContent) | `string` | — | Additional classes applied to the overlay that is rendered together with the content. |
| showCloseButton (DialogFooter) | `boolean` | `false` | Appends an outline Close button to the footer. |
| asChild (DialogTrigger, DialogClose) | `boolean` | `false` | Renders the trigger or close control as its child element instead of the default button. |
| className (DialogOverlay, DialogContent, DialogHeader, DialogFooter, DialogTitle, DialogDescription) | `string` | — | Additional classes merged onto the part that receives it. |
| children (Dialog, DialogTrigger, DialogPortal, DialogClose, DialogContent, DialogHeader, DialogFooter, DialogTitle, DialogDescription) | `React.ReactNode` | — | Trigger label and the header, body, and footer content rendered inside the modal. |
| ...props (Dialog) | `React.ComponentProps<typeof DialogPrimitive.Root>` | — | All remaining Radix Dialog root props are forwarded. |
| ...props (DialogTrigger, DialogClose) | `React.ComponentProps<typeof DialogPrimitive.Trigger> | React.ComponentProps<typeof DialogPrimitive.Close>` | — | All remaining Radix trigger and close props are forwarded to the rendered buttons. |
| ...props (DialogPortal, DialogOverlay, DialogContent) | `React.ComponentProps<typeof DialogPrimitive.Portal> | React.ComponentProps<typeof DialogPrimitive.Overlay> | React.ComponentProps<typeof DialogPrimitive.Content>` | — | All remaining Radix portal, overlay, and content props are forwarded (forceMount, onOpenAutoFocus, …). |
| ...props (DialogTitle, DialogDescription) | `React.ComponentProps<typeof DialogPrimitive.Title> | React.ComponentProps<typeof DialogPrimitive.Description>` | — | All remaining Radix title and description props are forwarded. |
| ...props (DialogHeader, DialogFooter) | `React.ComponentProps<"div">` | — | All remaining native div attributes are forwarded. |

## Source

```tsx
"use client"

import * as React from "react"
import XIcon from "lucide-react/dist/esm/icons/x"
import { Dialog as DialogPrimitive } from "radix-ui"

import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"

function Dialog({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Root>) {
  return <DialogPrimitive.Root data-slot="dialog" {...props} />
}

function DialogTrigger({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Trigger>) {
  return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />
}

function DialogPortal({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Portal>) {
  return <DialogPrimitive.Portal data-slot="dialog-portal" {...props} />
}

function DialogClose({
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Close>) {
  return <DialogPrimitive.Close data-slot="dialog-close" {...props} />
}

function DialogOverlay({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Overlay>) {
  return (
    <DialogPrimitive.Overlay
      data-slot="dialog-overlay"
      className={cn(
        "fixed inset-0 z-[1400] bg-black/50 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:animate-in data-[state=open]:fade-in-0",
        className
      )}
      {...props}
    />
  )
}

function DialogContent({
  className,
  children,
  showCloseButton = true,
  overlayClassName,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Content> & {
  showCloseButton?: boolean
  overlayClassName?: string
}) {
  return (
    <DialogPortal data-slot="dialog-portal">
      <DialogOverlay className={overlayClassName} />
      <DialogPrimitive.Content
        data-slot="dialog-content"
        className={cn(
          "fixed top-[50%] left-[50%] z-[1450] grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] gap-4 rounded-lg border bg-background p-6 shadow-lg duration-200 outline-none data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 sm:max-w-lg",
          className
        )}
        {...props}
      >
        {children}
        {showCloseButton && (
          <DialogPrimitive.Close
            data-slot="dialog-close"
            className="absolute top-4 right-4 rounded-xs opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:ring-2 focus:ring-ring focus:ring-offset-2 focus:outline-hidden disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4"
          >
            <XIcon />
            <span className="sr-only">Close</span>
          </DialogPrimitive.Close>
        )}
      </DialogPrimitive.Content>
    </DialogPortal>
  )
}

function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
  return (
    <div
      data-slot="dialog-header"
      className={cn("flex flex-col gap-2 text-center sm:text-left", className)}
      {...props}
    />
  )
}

function DialogFooter({
  className,
  showCloseButton = false,
  children,
  ...props
}: React.ComponentProps<"div"> & {
  showCloseButton?: boolean
}) {
  return (
    <div
      data-slot="dialog-footer"
      className={cn(
        "flex flex-col-reverse gap-2 sm:flex-row sm:justify-end",
        className
      )}
      {...props}
    >
      {children}
      {showCloseButton && (
        <DialogPrimitive.Close asChild>
          <Button variant="outline">Close</Button>
        </DialogPrimitive.Close>
      )}
    </div>
  )
}

function DialogTitle({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Title>) {
  return (
    <DialogPrimitive.Title
      data-slot="dialog-title"
      className={cn("text-lg leading-none font-semibold", className)}
      {...props}
    />
  )
}

function DialogDescription({
  className,
  ...props
}: React.ComponentProps<typeof DialogPrimitive.Description>) {
  return (
    <DialogPrimitive.Description
      data-slot="dialog-description"
      className={cn("text-sm text-muted-foreground", className)}
      {...props}
    />
  )
}

export {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogOverlay,
  DialogPortal,
  DialogTitle,
  DialogTrigger,
}

```
