Skip to main content

Dialog and ConfirmDialog

import { Dialog, ConfirmDialog } from "@study/ui"

A dialog is visible as long as it is rendered; onClose removes it.

Result
Loading...
Live Editor
function Example() {
  const [open, setOpen] = useState(null);
  return (
    <div style={{ display: "flex", gap: 10 }}>
      <Button variant="primary" icon="plus" onClick={() => setOpen("new")}>Create course</Button>
      <Button variant="danger" icon="trash" onClick={() => setOpen("delete")}>Delete course</Button>
      {open === "new" && (
        <Dialog title="New course" onClose={() => setOpen(null)}
          footer={<><Button onClick={() => setOpen(null)}>Cancel</Button><Button variant="primary" onClick={() => setOpen(null)}>Create</Button></>}>
          <TextField label="Name" placeholder="e.g. Control Engineering" autoFocus />
        </Dialog>
      )}
      {open === "delete" && (
        <ConfirmDialog title="Delete Control Engineering?" confirm="Delete" danger onClose={() => setOpen(null)}
          onConfirm={() => new Promise((r) => setTimeout(r, 800))}>
          The course's files, notes and flashcards then stay in the trash for 30 days.
        </ConfirmDialog>
      )}
    </div>
  );
}

When to use​

✓ DoShort things that belong to the page: create, rename, ask.
✗ Don'tLong forms or dialogs in dialogs – use a page of its own for that.
  • At the bottom right: Cancel first, the main action last.
  • ConfirmDialog before anything that cannot easily be undone; its onConfirm may be a promise – the button loads, an error appears in the dialog.
  • More under Patterns › Confirming deletion.

Props​

PropTypeDefaultMeaning
onClose *() => void–Escape, the close button and the Cancel of a footer call it; remove the dialog then.
title *ReactNode–
classNamestring–
closeOnBackdropbooleantrueWhether a click beside the dialog closes it. Default yes.
descriptionReactNode–A sentence below the title.
footerReactNode–The buttons at the bottom: Cancel first, the main action last.
hideCloseboolean–Hides the close button at the top, e.g. while something is being saved.
hideTitleboolean–Keeps the title for screen readers only, for dialogs whose content says enough (the search).
placement"center" | "top"centertop – near the top of the window, for the search, so the results have room below.
size"sm" | "md" | "lg"md
PropTypeDefaultMeaning
children *ReactNode–What happens, and whether it can be undone.
confirm *string–The button's verb, e.g. "Delete".
onClose *() => void–
onConfirm *() => unknown–Runs when confirmed; the dialog stays open with the error when it fails.
title *ReactNode–
cancelstringuiText().cancel
dangerboolean–Red, for deleting and leaving.
describeError((error: unknown) => string)–Turns an error into a sentence. Default its message.

Accessibility​

  • A real <dialog> as a modal: the rest of the page is locked, the focus stays inside.
  • Title and description are connected with aria-labelledby/aria-describedby.
  • Escape and the × close it; afterwards the focus returns to the button that opened it.
  • In the ConfirmDialog, "Cancel" has the focus – Enter does no harm.