Dialog

A modal window over the page.

Source
$npx @dinachi/cli@latest add dialog
DialogTitle
DialogDescription
DialogClose
modal
disablePointerDismissal

Usage

import { Button } from "@/components/ui/button"
import {
  Dialog,
  DialogClose,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"

export function RenameProject() {
  return (
    <Dialog>
      <DialogTrigger render={<Button />}>Rename project</DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Rename project</DialogTitle>
          <DialogDescription>The new name shows everywhere at once.</DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <DialogClose>Cancel</DialogClose>
          <Button>Save</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  )
}

Examples

A header, two fields and footer actions.
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogDescription,
  DialogFooter,
  DialogClose,
  DialogTrigger,
} from '@/components/ui/dialog';
import { Button } from '@/components/ui/button';

export function Example() {
  return (
    <Dialog>
      <DialogTrigger render={<Button />}>Open Dialog</DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Edit Profile</DialogTitle>
          <DialogDescription>
            Make changes to your profile here. Click save when you're done.
          </DialogDescription>
        </DialogHeader>
        <div className="space-y-4 py-4">
          <div className="space-y-2">
            <label htmlFor="name" className="text-sm font-medium">Name</label>
            <input id="name" defaultValue="John Doe" className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm" />
          </div>
          <div className="space-y-2">
            <label htmlFor="username" className="text-sm font-medium">Username</label>
            <input id="username" defaultValue="@johndoe" className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm" />
          </div>
        </div>
        <DialogFooter>
          <DialogClose>Cancel</DialogClose>
          <Button>Save changes</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
}

Notes

Composing the layers

DialogContent is DialogPortal, DialogBackdrop, DialogViewport and DialogPopup stacked in that order. Compose them yourself to style or drop a layer; the agent still finds the popup.

Popups inside a dialog

A Select inside a dialog renders its list in place by default, so it stays inside the dialog's focus trap and stacking. The With form example composes Input, Select, Radio, Checkbox and Textarea this way.

API

openboolean
Controlled open state.
defaultOpenboolean = false
Starting open state when uncontrolled.
onOpenChange
agent
(open, details) => void
Runs on open or close, for people and agents. details.cancel() refuses it.
modalboolean | "trap-focus" = true
Traps focus, locks page scroll and blocks outside clicks. trap-focus only traps focus.
disablePointerDismissalboolean = false
Keeps the dialog open on an outside click.
onOpenChangeComplete(open) => void
Runs once the open or close animation finishes.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as dialog.

Reads

label

The DialogTitle while open. The trigger's name while closed, or while open with no title.

open

Whether the dialog is showing. Open dialogs are listed first.

Actions

open()

While closed. act returns the opened dialog under appeared.

close()

While open.

An agent's open or close runs through onOpenChange. isAgentChange(details) is true for it; details.cancel() refuses it and tells the agent. Controls inside report the dialog's title as in.

await agent.act({
  steps: [{ ref: "Open Dialog", action: "open" }],
})

Accessibility

  • The popup is a dialog named by DialogTitle and described by DialogDescription.
  • Give every dialog a DialogTitle; the agent reads the same name.
  • While modal, focus is trapped and the page behind is inert.
  • Focus returns to the trigger on close, unless finalFocus says otherwise.
  • Reduced motion turns the animation into a fade.