Skip to content

Dialog

A focused surface for decisions that need your attention.

Draft

Installation

Terminal
pnpm dlx shadcn@latest add \
  https://react-toolbox.io/r/dialog.json \
  https://react-toolbox.io/r/button.json \
  https://react-toolbox.io/r/input.json

New here? Set up your project first. Styles, fonts, and required components are included. The command includes the components used by the examples below.

Usage

Supply a title to name the dialog. Focus moves into the dialog when it opens and returns to the trigger when it closes; customize this with onOpenAutoFocus and onCloseAutoFocus.

Composition

Compose the form, body, and footer independently. DialogRoot coordinates focus and dismissal; close it after a successful save.

Terminal
pnpm dlx shadcn@latest add https://react-toolbox.io/r/example-recipes.json

A form with an independent footer

Current name: Javi

Confirm a destructive action

Focus Cancel first, prevent repeated submission while pending, and keep failures open for retry. Pass your application’s delete callback to the editable example; this demo only changes local state.

Show composition code
tsx
'use client';
import { useState } from 'react';
import { Button } from '@/components/ui/material/button';


import { DestructiveActionRecipe } from '@/components/ui/examples/recipes';


export default function DialogComposition() {
  const [deleted, setDeleted] = useState(false);
  return (
    <div className="space-y-8">
      <section aria-label="Profile form" className="space-y-4">
        <h3 className="text-toolbox-subheading">A form with an independent footer</h3>
        <ProfileDialog />
      </section>
      <section aria-label="Destructive confirmation" className="space-y-4">
        <h3 className="text-toolbox-subheading">Confirm a destructive action</h3>
        <p className="text-toolbox-body/6">
          Focus Cancel first, prevent repeated submission while pending, and keep failures open for retry. Pass your
          application’s delete callback to the editable example; this demo only changes local state.
        </p>
        {deleted ? (
          <p role="status">Example draft deleted.</p>
        ) : (
          <DestructiveActionRecipe
            itemName="example draft"
            onDelete={async () => {
              await new Promise<void>((resolve) => setTimeout(resolve, 700));
              setDeleted(true);
            }}
          />
        )}
      </section>
    </div>
  );
}




import { Input } from '@/components/ui/material/input';
import {
  DialogRoot,
  DialogTrigger,
  DialogPortal,
  DialogOverlay,
  DialogContent,
  DialogBody,
  DialogTitle,
  DialogDescription,
  DialogFooter,
  DialogClose,
} from '@/components/ui/material/dialog';

function ProfileDialog() {
  const [open, setOpen] = useState(false);
  const [name, setName] = useState('Javi');
  return (
    <div className="space-y-4">
      <DialogRoot open={open} onOpenChange={setOpen}>
        <DialogTrigger asChild>
          <Button raised primary>
            Edit profile
          </Button>
        </DialogTrigger>
        <DialogPortal>
          <DialogOverlay />
          <DialogContent>
            <DialogBody className="flex flex-col gap-5">
              <DialogTitle>Edit profile</DialogTitle>
              <DialogDescription>Change the name shown to your teammates.</DialogDescription>
              <form
                id="profile-composition"
                onSubmit={(event) => {
                  event.preventDefault();
                  setName(String(new FormData(event.currentTarget).get('name')));
                  setOpen(false);
                }}
              >
                <Input label="Display name" name="name" defaultValue={name} required />
              </form>
            </DialogBody>
            <DialogFooter>
              <DialogClose asChild>
                <Button>Cancel</Button>
              </DialogClose>
              <Button primary type="submit" form="profile-composition">
                Save changes
              </Button>
            </DialogFooter>
          </DialogContent>
        </DialogPortal>
      </DialogRoot>
      <p className="text-toolbox-body">
        Current name: <output>{name}</output>
      </p>
    </div>
  );
}

State and callbacks

  • With Dialog, set active to false in your action, onEscKeyDown, or onOverlayClick handler. These callbacks do not close it automatically.
  • delay keeps the surface mounted for its exit animation. It is not a dismissal timeout.

API reference

Key props and defaults. Full types and inline documentation are included with the source in material/dialog.

Dialog

Property Type Default
actions DialogActionProps[] []
active boolean false
onCloseAutoFocus (event: Event) => void

Cancel the default return focus, for example after deleting the opener.

—
onEscKeyDown (event: KeyboardEvent) => void —
onOpenAutoFocus (event: Event) => void

Cancel the default focus placement and focus a specific field instead.

—
onOverlayClick MouseEventHandler<HTMLDivElement> —
title ReactNode —
type 'small' | 'normal' | 'large' | 'fullscreen' | 'custom' 'normal'

Material guidelines

  • Interrupt the current task only when a focused decision is needed. Use a clear title, concise content, and at most two related actions.
  • In a left-to-right dialog, place the dismissive action before the affirmative action. Name the outcome, such as Save, rather than using an ambiguous response.
  • Keep keyboard focus inside the open dialog and return it to the trigger on dismissal.

Material Design 1: Dialogs