Skip to content
+

useDialogs

Imperative APIs to open and interact with dialogs.

Toolpad Core offers a set of abstractions that makes interacting with dialogs simpler. It has an imperative API to open and close dialogs, and allows dialogs to be stacked on top of each other.

First thing you need to do is install the DialogsProvider at the root of your application.

import { DialogsProvider } from '@toolpad/core/useDialogs';

function App({ children }) {
  return <DialogsProvider>{children}</DialogsProvider>;
}

To get access to the dialogs API you first have to call the useDialogs hook.

import { useDialogs } from '@toolpad/core/useDialogs';

function MyApp() {
  const dialogs = useDialogs();
  // ...
}

Basic dialog

Dialogs are React components that taken open and onClose properties and return a Dialog component. The open property reflects the open state of the dialog and you can call the onClose handler to close it.

function MyCustomDialog({ open, onClose }: DialogProps) {
  return (
    <Dialog fullWidth open={open} onClose={() => onClose()}>
      <DialogTitle>Custom dialog</DialogTitle>
      <DialogContent>I am a custom dialog</DialogContent>
      <DialogActions>
        <Button onClick={() => onClose()}>Close me</Button>
      </DialogActions>
    </Dialog>
  );
}

Now you can call the dialogs.open function and pass the component as a first parameter.

Press Enter to start editing

With dialog payload

You can pass a payload to the dialog with the second parameter. The payload stays constant for the lifetime of the dialog.

Press Enter to start editing

With dialog result

A dialog can return a value with the onClose handler. The promise returned by the open method is resolved with the value that was passed to onClose.

Press Enter to start editing

Stacked dialogs

Dialogs can be stacked. A dialog can open other another dialog which comes to the foreground upon opening. Closing the latter reveals the former again.

Press Enter to start editing

System dialogs

Toolpad comes with a set of system dialogs that improve on the native window.alert, window.confirm, and window.prompt APIs. These APIs are very similar, but they create dialogs that follow your application theme.

Alert

Analog to window.alert it opens a dialog with a message for the user. The only action to be taken is to acknowledge the message after which the dialog closes. The dialog title and button text are customizable with the title and okText properties.

Press Enter to start editing

Confirm

Analog to window.confirm it opens a dialog with a question for the user. The user can either confirm or cancel and the dialog result is a boolean which is true when the user confirmed. The dialog title and button texts are customizable with the title, okText, and cancelText properties.

Press Enter to start editing

Prompt

Analog to window.prompt it opens a dialog inquiring the user for some input text. The user can fill the input box and upon confirmation the promise returned from the prompt call is resolved with its value. The dialog title and button texts are customizable with the title, okText, and cancelText properties.

Press Enter to start editing

API

See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.