Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 36 ยท Components and Props

Alerts, Dialogs and Snackbars

Give users feedback with Material UI: Alert severities, modal Dialog with actions, Snackbar with auto-hide, and when to use each one without overwhelming people.

Apps must talk back. A save succeeded, a field is wrong, an action is irreversible. Material UI has three main feedback components, each with a different level of interruption: Alert (inline, persistent), Snackbar (brief, non-blocking) and Dialog (blocking, demands a decision). Choosing the right one is as important as using it correctly.

Three levels of interruption

Alert Inline message, stays in the page
Snackbar Brief toast, disappears itself
Dialog Blocks the page until answered

Use the least interruptive one that does the job.

1 / 2

Alert

Alert shows a message with an icon and a color. The severity prop picks one of four meanings: error, warning, info and success. Real MUI code (read-only):

JSX
import Alert from '@mui/material/Alert';
import AlertTitle from '@mui/material/AlertTitle';

<Alert severity="success">Profile saved.</Alert>
<Alert severity="warning" variant="outlined">Your session expires soon.</Alert>
<Alert severity="error" variant="filled" onClose={() => setOpen(false)}>
  <AlertTitle>Upload failed</AlertTitle>
  The file is larger than 10 MB.
</Alert>
  • variant: standard (default, tinted), outlined or filled.
  • onClose shows a close button; action lets you place your own button instead.
  • AlertTitle adds a bold first line.
  • role="alert" is set for you, so screen readers announce the message when it appears. Use alerts for important messages only; announcing everything makes the page noisy.

Stand-in: dismissible alerts

The first stand-in imitates Alert with severities, variants and a close button. Dismiss alerts and use the reset button to bring them back.

Stand-in: Alert severities, variants and close

Dialog

A Dialog is a modal window. It dims the page, traps keyboard focus inside, closes on Escape, and returns focus to the trigger when it closes. It is controlled: you give it open and an onClose handler.

JSX
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import DialogActions from '@mui/material/DialogActions';

export default function ConfirmDelete() {
  const [open, setOpen] = useState(false);

  return (
    <>
      <Button color="error" onClick={() => setOpen(true)}>Delete project</Button>
      <Dialog
        open={open}
        onClose={() => setOpen(false)}
        aria-labelledby="delete-title"
        aria-describedby="delete-text"
      >
        <DialogTitle id="delete-title">Delete this project?</DialogTitle>
        <DialogContent>
          <DialogContentText id="delete-text">
            This permanently removes the project and its files.
          </DialogContentText>
        </DialogContent>
        <DialogActions>
          <Button onClick={() => setOpen(false)}>Cancel</Button>
          <Button color="error" onClick={() => { remove(); setOpen(false); }} autoFocus>
            Delete
          </Button>
        </DialogActions>
      </Dialog>
    </>
  );
}

Important props: fullWidth and maxWidth (xs to xl) for width, fullScreen (often useMediaQuery on small screens), scroll="paper" | "body", and slots/slotProps or PaperProps for customization. Always connect the title and description with aria-labelledby / aria-describedby. A confirmation dialog should put focus (autoFocus) on the safe or primary action and name the buttons after what they do ("Delete", not "OK").

Dialogs interrupt, so use them for decisions that need attention: destructive actions, required input, unsaved changes. Do not show a dialog for plain information; use an Alert or Snackbar.

Stand-in: a confirmation dialog

This stand-in builds the behavior of a dialog: an overlay, Escape to close, a backdrop click to close, and focus moved to the primary button when it opens. The real Dialog does all of this (and more) for you.

Stand-in: a modal confirmation dialog

Snackbar

A Snackbar shows a short message at the edge of the screen (bottom-left by default) and goes away by itself. It does not block anything. Use it to confirm completed actions ("Saved", "Item removed" with an Undo action).

JSX
import Snackbar from '@mui/material/Snackbar';

<Snackbar
  open={open}
  autoHideDuration={4000}
  onClose={(event, reason) => {
    if (reason === 'clickaway') return;   // ignore clicks elsewhere
    setOpen(false);
  }}
  message="Item archived"
  action={<Button size="small" onClick={undo}>UNDO</Button>}
  anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
/>

You can also put an Alert inside a Snackbar for colored severity:

JSX
<Snackbar open={open} autoHideDuration={4000} onClose={handleClose}>
  <Alert severity="success" variant="filled" onClose={handleClose}>Saved!</Alert>
</Snackbar>

Rules of thumb: show one snackbar at a time, keep text short, give enough time to read (at least 4 to 6 seconds), and never put the only way to complete a critical task inside one.

Stand-in: a timed snackbar with undo

Stand-in: Snackbar that hides itself and supports undo

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which component is best for a brief 'Saved' confirmation that disappears automatically?

    Snackbar is non-blocking and auto-hides, which suits confirmations.
  2. 2. Which props does Dialog need to be shown and closed?

    Dialog is controlled: you pass open and handle onClose (Escape, backdrop click).
  3. 3. What does the severity prop of Alert accept?

    severity chooses the meaning (and color/icon). variant chooses standard, outlined or filled.
  4. 4. A Snackbar onClose is called with reason 'clickaway'. What is a common good practice?

    Clicking elsewhere on the page should not dismiss a message the user may still be reading, so many apps ignore clickaway.

Summary

  • Alert is an inline, persistent message with severity, variant, AlertTitle and optional close action.
  • Dialog is a modal: controlled with open/onClose, traps focus, needs aria-labelledby and clear button labels.
  • Snackbar is a short non-blocking toast with autoHideDuration; ignore clickaway and show one at a time.
  • Choose the least interruptive feedback that fits the task.

Next: Common Props: component, ref and slots