Skip to content
View as Markdown

Dialog ​

The Dialog component provides a modal dialog with a compound component API. It is backed by Base UI's Dialog primitive.

Import ​

tsx
import { Dialog } from "@tailor-platform/app-shell";

The runnable examples also import useState from react, and Button, Field, Form, and Text from @tailor-platform/app-shell.

Basic Usage ​

tsx
function BasicUsage() {
  return (
    <Dialog.Root>
      <Dialog.Trigger render={<Button />}>Open</Dialog.Trigger>
      <Dialog.Content>
        <Dialog.Header>
          <Dialog.Title>Confirm Action</Dialog.Title>
          <Dialog.Description>Are you sure you want to proceed?</Dialog.Description>
        </Dialog.Header>
        <Dialog.Footer>
          <Dialog.Close render={<Button variant="outline" />}>Cancel</Dialog.Close>
          <Button>Confirm</Button>
        </Dialog.Footer>
      </Dialog.Content>
    </Dialog.Root>
  );
}

Sub-components ​

Sub-componentDescription
Dialog.RootManages open/close state
Dialog.TriggerElement that opens the dialog when clicked
Dialog.ContentThe main dialog panel (includes overlay and close button automatically)
Dialog.HeaderLayout wrapper for title and description
Dialog.FooterLayout wrapper for action buttons
Dialog.TitleDialog title (announced by screen readers)
Dialog.DescriptionAdditional context below the title
Dialog.CloseButton that closes the dialog

Props ​

Dialog.Root Props ​

PropTypeDefaultDescription
openboolean-Controlled open state
defaultOpenbooleanfalseInitial open state (uncontrolled)
onOpenChange(open: boolean) => void-Callback when open state changes
modalbooleantrueWhether the dialog is modal
childrenReact.ReactNode-Dialog sub-components

Dialog.Content Props ​

Accepts className and all standard HTML <div> props.

Tall Content ​

Dialog.Content is never taller than the viewport minus 2rem (16px above and below, the same margin as on the sides). When the content is taller, the whole dialog scrolls, including the title and the close button. Escape and a click on the backdrop still close it.

On open, focus moves to the first control in the dialog. In a dialog that scrolls, this happens only when that control is in view at the top. When it is below the fold (for example, a footer button after a long body), the dialog itself takes focus instead, so it opens at the top rather than scrolled to that control. Tab then moves to the control. Pass initialFocus to choose the element yourself.

To keep a header or footer in view while only the body scrolls, make Dialog.Content a flex column and give the body min-h-0 flex-1 overflow-y-auto. A className with its own max-h-* or overflow-* replaces the defaults.

Dialog.Trigger / Dialog.Close Props ​

Accept a render prop for custom element rendering (Base UI render pattern), plus standard button props.

Controlled Usage ​

tsx
function Controlled() {
  const [open, setOpen] = useState(false);
  return (
    <>
      <Button variant="outline" onClick={() => setOpen(true)}>
        Open Dialog
      </Button>
      <Dialog.Root open={open} onOpenChange={setOpen}>
        <Dialog.Content>
          <Dialog.Header>
            <Dialog.Title>Settings</Dialog.Title>
          </Dialog.Header>
          <Dialog.Footer>
            <Button onClick={() => setOpen(false)}>Done</Button>
          </Dialog.Footer>
        </Dialog.Content>
      </Dialog.Root>
    </>
  );
}

Examples ​

Delete Confirmation ​

Open the confirmation and cancel to keep the order, or confirm to remove it from this local example. No server data is changed. Restore demo order makes the example repeatable.

tsx
function DeleteConfirmation() {
  const [open, setOpen] = useState(false);
  const [deleted, setDeleted] = useState(false);

  return (
    <div className="space-y-3">
      <Text.p role="status">{deleted ? "Demo order deleted." : "Demo order: ORD-1001"}</Text.p>
      <div className="flex gap-2">
        <Dialog.Root open={open} onOpenChange={setOpen}>
          <Dialog.Trigger disabled={deleted} render={<Button variant="destructive" />}>
            Delete demo order
          </Dialog.Trigger>
          <Dialog.Content>
            <Dialog.Header>
              <Dialog.Title>Delete demo order?</Dialog.Title>
              <Dialog.Description>
                Remove ORD-1001 from this local example. No server data is changed.
              </Dialog.Description>
            </Dialog.Header>
            <Dialog.Footer>
              <Dialog.Close render={<Button type="button" variant="outline" />}>
                Cancel
              </Dialog.Close>
              <Button
                type="button"
                variant="destructive"
                onClick={() => {
                  setDeleted(true);
                  setOpen(false);
                }}
              >
                Delete order
              </Button>
            </Dialog.Footer>
          </Dialog.Content>
        </Dialog.Root>
        <Button
          type="button"
          variant="outline"
          disabled={!deleted}
          onClick={() => setDeleted(false)}
        >
          Restore demo order
        </Button>
      </div>
    </div>
  );
}

Form Dialog ​

Edit the item name and save to update the value outside the dialog. Empty or whitespace-only names are rejected through Form and Field. Cancel or close leaves the saved name unchanged; reopening restores it. Cancel is explicitly type="button" so it does not submit the form.

tsx
function FormDialog() {
  const [open, setOpen] = useState(false);
  const [name, setName] = useState("Small carton");

  return (
    <div className="space-y-3">
      <Text.p role="status">Saved item name: {name}</Text.p>
      <Dialog.Root open={open} onOpenChange={setOpen}>
        <Dialog.Trigger render={<Button variant="outline" />}>Edit item</Dialog.Trigger>
        <Dialog.Content>
          <Dialog.Header>
            <Dialog.Title>Edit item</Dialog.Title>
            <Dialog.Description>
              Save a new name, or cancel to keep the saved name.
            </Dialog.Description>
          </Dialog.Header>
          <Form<{ name: string }>
            noValidate
            onFormSubmit={(values) => {
              setName(values.name.trim());
              setOpen(false);
            }}
          >
            <Field.Root
              name="name"
              className="py-4"
              validate={(value) =>
                typeof value === "string" && value.trim() ? undefined : "Name is required."
              }
            >
              <Field.Label>Item name</Field.Label>
              <Field.Control defaultValue={name} required />
              <Field.Error>Name is required.</Field.Error>
            </Field.Root>
            <Dialog.Footer>
              <Dialog.Close render={<Button type="button" variant="outline" />}>
                Cancel
              </Dialog.Close>
              <Button type="submit">Save item</Button>
            </Dialog.Footer>
          </Form>
        </Dialog.Content>
      </Dialog.Root>
    </div>
  );
}

Stacking ​

Dialog renders through a portal to the end of body, in the portal band (--z-portal). A menu, select or combobox opened inside the dialog renders above it, because it was opened later. A toast is always above the dialog. See Styling and Theming → Z-index.

A dialog may open on top of another dialog, or on top of a sheet. Keep to these limits:

  • Two levels at most. The second dialog is for a confirmation or a warning only: a confirm before discarding changes, or a picker opened from a form dialog.
  • A third level is a step inside the dialog. A screen that needs one changes the content of the second dialog instead of opening another.
  • Render the second dialog inside the first one's content. The first dialog then knows that a dialog is open above it. Escape and a press outside close only the top dialog, and the dialog below stays open.
  • When the second dialog closes and the first stays open, focus returns to the element that opened the second one.

The limits are a guideline. The component does not enforce them.

A runnable example of a confirm dialog on top of a form dialog is in Modal Form → Second Level.

Accessibility ​

  • Dialog title is announced by screen readers via Dialog.Title
  • Focus is trapped inside the dialog while open
  • Pressing Escape closes the dialog
  • The close button is always present inside Dialog.Content
  • Sheet - Slide-in panel alternative for non-modal workflows
  • Button - Use as trigger and action buttons