Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 36 ยท Navigation and Data Display

Dialogs, Snackbars and Feedback

Give users feedback with Material UI Dialog, Snackbar, Alert, CircularProgress, LinearProgress and Skeleton, including focus handling, queues and accessibility.

An interface that never talks back feels broken. When a user saves a form, deletes a record or waits for data, the app needs to say what happened or what is happening. Material UI groups these tools as feedback components: dialogs for decisions, snackbars and alerts for messages, progress indicators and skeletons for waiting.

MUI is not loadable in the live editors on this site. Real MUI snippets are read-only; the runnable examples are stand-ins that reproduce the behavior (state, focus, timers) so you can practice it.

Dialog

Dialog is a modal: it renders in a portal, adds a backdrop, traps keyboard focus, closes on Escape and returns focus to the trigger when it closes. You control it with open and onClose. Build the inside from DialogTitle, DialogContent, DialogContentText and DialogActions.

JSX
// Real Material UI, read-only
import { useState } from 'react';
import Button from '@mui/material/Button';
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 DeleteProject({ name, onConfirm }) {
  const [open, setOpen] = useState(false);
  return (
    <>
      <Button color="error" onClick={() => setOpen(true)}>Delete</Button>
      <Dialog
        open={open}
        onClose={() => setOpen(false)}
        aria-labelledby="delete-title"
        aria-describedby="delete-text"
      >
        <DialogTitle id="delete-title">Delete {name}?</DialogTitle>
        <DialogContent>
          <DialogContentText id="delete-text">
            This removes the project and its files. You cannot undo it.
          </DialogContentText>
        </DialogContent>
        <DialogActions>
          <Button onClick={() => setOpen(false)} autoFocus>Cancel</Button>
          <Button color="error" onClick={() => { onConfirm(); setOpen(false); }}>Delete</Button>
        </DialogActions>
      </Dialog>
    </>
  );
}

Useful props: fullWidth plus maxWidth="sm" (xs to xl, or false), fullScreen (use useMediaQuery to make dialogs full-screen on phones), scroll="paper" or "body" for long content, and slots/slotProps for a custom paper or transition. The onClose handler also receives a reason ('backdropClick' or 'escapeKeyDown'). Ignore the reasons you do not want, for example to force an explicit choice in a form with unsaved changes.

Accessibility: label the dialog with aria-labelledby pointing at the title, describe it with aria-describedby, and put autoFocus on the safest button for destructive confirmations (Cancel), so Enter does not delete anything.

Keep the dialog's content in its own component, mounted only while open. Then its form state resets on every opening and you do not keep hidden inputs around.

Try it: a confirm dialog with focus handling

This stand-in uses the native <dialog> element, which gives the same behaviors: modal, focus trap, Escape and backdrop. Notice the "Cancel" button is focused first, and focus returns to "Delete" afterwards.

A confirmation dialog

Snackbar and Alert

A Snackbar shows a short message at the edge of the screen, usually after an action ("Saved"). Alone it is a dark bar with text. Put an Alert inside it when the message has a severity (success, info, warning, error).

JSX
// Real Material UI, read-only
import Snackbar from '@mui/material/Snackbar';
import Alert from '@mui/material/Alert';

<Snackbar
  open={open}
  autoHideDuration={4000}
  onClose={(event, reason) => {
    if (reason === 'clickaway') {
      return; // keep it open if the user clicks elsewhere
    }
    setOpen(false);
  }}
  anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
>
  <Alert onClose={() => setOpen(false)} severity="success" variant="filled" sx={{ width: '100%' }}>
    Settings saved
  </Alert>
</Snackbar>

Remember:

  • autoHideDuration is a timer in milliseconds; it pauses while the pointer is over the snackbar or the window is unfocused.
  • Snackbar content is announced politely by screen readers (role="alert" on Alert). Do not put the only copy of important information in a message that disappears. Errors that need action belong in the page, in an Alert that stays.
  • MUI shows one Snackbar at a time by design. To queue messages, keep an array in state and show the first; the sample below does that.
  • Use Alert on its own (not inside a Snackbar) for inline, persistent messages, with AlertTitle for a heading and an action prop for a button.

Try it: a snackbar queue

Queued snackbars with auto hide

Progress indicators and Skeleton

Use progress when the app is working, and say what is working if it takes more than a second.

Component When
CircularProgress Short or unknown waits; inside a Button while saving
LinearProgress Page-level loading (top of a page) or a measurable task (variant="determinate", value={0-100})
Skeleton A placeholder shaped like the content that is loading
Backdrop A full-screen dim layer with a spinner, for blocking work
JSX
// Real Material UI, read-only
import CircularProgress from '@mui/material/CircularProgress';
import LinearProgress from '@mui/material/LinearProgress';
import Skeleton from '@mui/material/Skeleton';

<Button disabled={saving} startIcon={saving && <CircularProgress size={16} />}>
  {saving ? 'Saving...' : 'Save'}
</Button>

<LinearProgress variant="determinate" value={progress} aria-label="Upload progress" />

{loading ? (
  <>
    <Skeleton variant="text" width="60%" />
    <Skeleton variant="rectangular" height={120} />
  </>
) : (
  <Article data={data} />
)}

Skeletons reduce layout shift because they hold the space, and they feel faster than a spinner. Match their size to the real content. Progress components have the role progressbar; give them an accessible name with aria-label, and set disabled on the button while saving so users cannot submit twice.

Try it: skeleton and progress

Loading states: Skeleton and a determinate bar

Choosing the right feedback

flowchart TD
  A["Something happened"] --> B{"Does the user have to decide?"}
  B -- yes --> C["Dialog"]
  B -- no --> D{"Is it temporary news?"}
  D -- yes --> E["Snackbar with Alert"]
  D -- no --> F{"Is it still loading?"}
  F -- yes --> G["Skeleton or progress"]
  F -- no --> H["Inline Alert in the page"]

Use the least interruptive component that does the job: decisions block, news fades, waiting shows progress, lasting problems stay inline.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which button should get autoFocus in a destructive confirm dialog?

    Focus should start on the safest action. Pressing Enter by habit then cancels rather than destroys data.
  2. 2. Why ignore the 'clickaway' reason in a Snackbar's onClose?

    By default, clicking anywhere closes the snackbar. Many apps ignore that reason so only the timer or the close button hides it.
  3. 3. What is a good reason to prefer Skeleton over a spinner for page content?

    A skeleton reserves the space the real content will take, so the page does not jump when the data arrives.
  4. 4. How does MUI treat several Snackbars at once?

    The Material guidelines show one snackbar at a time. A queue in your own state gives each message its turn.

Summary

  • Dialog is a modal with a focus trap, Escape and backdrop handling; label it, and focus the safe action.
  • Snackbar is for brief news; wrap an Alert for severity, handle reason, and queue messages yourself.
  • Use Alert inline for persistent messages and errors that need action.
  • Use Skeleton, CircularProgress, LinearProgress or Backdrop for waiting, and disable buttons while saving.

Next: Bundle Size and Imports