Skip to content

Dialog

A modal surface for a focused task or a decision, with a title, content and actions, or full screen on compact windows.

import { Dialog, DialogTrigger, DialogTitle, DialogHeader, DialogContent, DialogActions } from '@windmorph/react';

Purpose

A dialog is a modal surface that interrupts the user for a focused task or an important decision. A basic dialog is an alert dialog on the web, as the spec says: it needs a decision and isn't dismissed by pressing outside. Set role="dialog" (or closedBy="any") for one a press outside may close. For non-blocking supplementary content prefer a sheet, and for a brief message prefer a snackbar.

A dialog is a component, not a composite: open it from a DialogTrigger (trigger first, dialog second) or control it with open / onOpenChange. Its anatomy is the flat parts DialogTitle, DialogContent and DialogActions.

On compact windows, a task with several fields or a form is clearer as a full-screen dialog: fullScreen="compact" fills the window below 600px and stays a basic dialog above it. Give it a DialogHeader, which holds the close button, the headline and the confirming action in full screen. Without that action, the DialogActions become a 56px bottom action bar; divider draws a line under the header. fullScreen="always" fills every window, but the spec keeps full-screen dialogs to compact windows.

A basic dialog is centred; placement (top, bottom, start, end) moves it, keeping 56px from the edges of wider windows (24px on compact ones). It enters from the edge it sits against: it slides 50px away from that edge while its container grows from it. For any other spot, such as a corner near the controls that opened it, align it with classNames.positioner (items-* and justify-* classes): the positioner's padding keeps the same margin. className and style go on the outermost element, a full-window layer around the positioner; style the dialog's surface with classNames.panel.

dividers on DialogContent draws the spec's optional dividers above and below the content, for example around a list. A full-screen dialog opened from a FAB can grow out of it: origin="trigger" (or an element, or a box) gives the container transform the spec asks for.

Actions and writing

  • Use at most two actions. A single action must be an acknowledgement ("OK", "Got it"); with two, one confirms and the other dismisses. Don't add a third such as "Learn more": it leaves the task unfinished. Show more inline, or before the dialog opens. In development, DialogActions warns once when it holds more than two actions.
  • Put the dismissive action first and the confirming action last, so it sits closest to the trailing edge (and on top when they stack). Both are text buttons.
  • Write the headline as a brief, clear statement or question ("Discard draft?"). Avoid apologies ("Sorry for the interruption"), alarm ("Warning!") and ambiguity ("Are you sure?"). In a full-screen dialog, put a long headline in the content rather than the header.
  • When the dialog asks for a choice, disable the confirming action until one is made; never disable the dismissive action. A full-screen dialog's confirming action ("Save", "Create", "Send") says what happens and is never disabled.
  • When a full-screen dialog with unsaved changes is closed, confirm in a basic dialog that the changes should be discarded. Don't use the confirming action to dismiss.
  • Show errors about fields inline, where they occur, all at once. Show a general error (a failed save) in a basic dialog when the confirming action fails.
  • Use dialogs sparingly, for high-priority information or decisions. A snackbar suits low or medium priority; only a full-screen dialog may have another dialog over it.

Do and don't

Do: Ask a specific question, say what it involves, and name the actions after what they do.
Don't: Don't ask an ambiguous question with vague actions such as OK.
Do: Put the dismissive action first and the confirming action last.
Don't: Don't put the dismissive action after the confirming one.

When not to use

For a brief, non-blocking message use a Snackbar; for content that need not interrupt use a Sheet.

See also

  • Dialog on m3.material.io: Material's own guidelines, with anatomy and specs.
  • Sheets: Bottom and side sheets for supplementary content, modal or standing in the layout.
  • Menu: A temporary list of choices anchored to a trigger, with groups, selection, submenus and context menus, or a bottom sheet on phones.
  • Snackbar: A brief message with an optional action and dismiss, queued through a host.