Loading component…
Loading component…
A modal window on a dimmed backdrop
To reuse this local React demo, copy all files from the Code tab into the same folder, then import Dialog from ./dialog.
import { useState } from "react";
import { Dialog } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
export function EditAgent({ agent, onSave }: { agent: { name: string }; onSave: (name: string) => void }) {
const [open, setOpen] = useState(false);
const [name, setName] = useState(agent.name);
return (
<Dialog
open={open}
onOpenChange={setOpen}
trigger={<Button variant="secondary">Edit</Button>}
title="Edit agent"
description="Changes apply from its next run."
footer={
<>
<Button variant="secondary" onClick={() => setOpen(false)}>
Cancel
</Button>
<Button
onClick={() => {
onSave(name);
setOpen(false);
}}
>
Save
</Button>
</>
}
>
<input aria-label="Name" value={name} onChange={(e) => setName(e.target.value)} />
</Dialog>
);
}| Name | Type | Default | Description |
|---|---|---|---|
| open | boolean | Shows it – pass it with onOpenChange to control it, or leave both out and use trigger | |
| defaultOpen | boolean | false | Open from the start when it isn't controlled |
| onOpenChange | (open: boolean) => void | Called with false on Escape, a backdrop press, the × (and AlertDialog's buttons), with true from the trigger | |
| trigger | ReactElement | A button that opens it – it keeps its own onClick, gets aria-haspopup and aria-expanded, and the focus comes back to it on close | |
| title | ReactNode | Required. The heading – also the dialog's accessible name (aria-labelledby) | |
| description | ReactNode | A quiet line under the title, read out with it (aria-describedby) | |
| children | ReactNode | The content – it scrolls when it is taller than the window, a hairline shows at the edge that has more | |
| footer | ReactNode | The actions on the gray tray below the card, aligned to the end – they stay in view while the content scrolls and wrap rather than squeeze | |
| size | "sm" \| "md" \| "lg" | "md" (AlertDialog "sm") | 400, 480 or 720px wide – never wider than the window |
| width | number \| string | Any width instead of a size, e.g. 560 or "40rem" | |
| closeOnBackdrop | boolean | true | A press on the backdrop closes it. Off, the card gives a small nudge instead. AlertDialog never closes this way |
| closeButton | boolean | true | The × in the corner. AlertDialog has none – its Cancel is the way out |
| closeLabel | string | "Close" | The ×'s accessible name and tooltip – for other languages |
| initialFocus | RefObject<HTMLElement> | The control that gets the focus on open. By default: an element with data-autofocus, else the first control in the content, then in the footer | |
| contained | boolean | false | Stays inside its nearest positioned parent – an app frame, a preview – instead of covering the window; Tab still goes round inside |
| ...dialog props | ComponentProps<"dialog"> | className, style, id, aria-* … go to the <dialog> layer |
On phones (or in a frame narrower than 520px) it becomes a sheet from the bottom: full width, the actions within the thumb's reach and above the home bar, AlertDialog's two buttons sharing the row
Long content scrolls inside while the title and the actions stay put; long titles and unbroken words wrap
The page under it stops scrolling while it is open, without shifting sideways
Touch screens get bigger buttons and a bigger ×
Dark mode built in: switches when an ancestor (usually <html>) has the class dark or data-theme="dark"
@import "tailwindcss"; in your main CSS file.