Snackbar
Snackbars provide brief messages about app processes at the bottom of the screen.Overview
Snackbars inform users of a process that an app has performed or will perform. They appear temporarily, towards the bottom of the screen. They shouldn’t interrupt the user experience, and they don’t require user input to disappear.Basic Usage
Auto-hide Duration
Snackbars can automatically hide after a specified duration:null to disable auto-hide:
Positioning
Control the position of the snackbar usinganchorOrigin:
With Alert
For more complex notifications, combine Snackbar with Alert:Props
Main Props
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | - | If true, the component is shown. |
message | ReactNode | - | The message to display. |
action | ReactNode | - | The action to display. It renders after the message, at the end of the snackbar. |
autoHideDuration | number | null | null | Number of milliseconds to wait before auto-calling onClose. Set to null to disable. |
onClose | (event: SyntheticEvent | Event, reason: SnackbarCloseReason) => void | - | Callback fired when the component requests to be closed. |
anchorOrigin | { vertical: 'top' | 'bottom', horizontal: 'left' | 'center' | 'right' } | { vertical: 'bottom', horizontal: 'left' } | The anchor of the Snackbar. |
resumeHideDuration | number | - | Number of milliseconds to wait before dismissing after user interaction. |
disableWindowBlurListener | boolean | false | If true, the autoHideDuration timer will expire even if the window is not focused. |
transitionDuration | number | { enter?: number, exit?: number } | { enter: theme.transitions.duration.enteringScreen, exit: theme.transitions.duration.leavingScreen } | The duration for the transition. |
key | any | - | When displaying multiple consecutive snackbars, add the key prop to ensure independent treatment. |
sx | SxProps<Theme> | - | System prop for defining CSS overrides. |
Close Reasons
TheonClose callback receives a reason parameter:
'timeout'-autoHideDurationexpired'clickaway'- User clicked outside the snackbar'escapeKeyDown'- User pressed Escape key
Slot Props
The Snackbar component supports slot-based customization:slots.root- The root div elementslots.content- The SnackbarContent componentslots.clickAwayListener- The ClickAwayListener componentslots.transition- The transition component (default: Grow)
slotProps to pass props to each slot:
Consecutive Snackbars
When displaying multiple snackbars, use thekey prop to ensure each is treated independently:
Transitions
Customize the transition component:Accessibility
Snackbars should:- Use appropriate ARIA attributes
- Not block critical UI elements
- Provide sufficient time for users to read messages
- Allow dismissal via Escape key
CSS Classes
The component has the following CSS classes available:.MuiSnackbar-root- Root element.MuiSnackbar-anchorOriginTopCenter- Applied when anchorOrigin=.MuiSnackbar-anchorOriginBottomCenter- Applied when anchorOrigin=.MuiSnackbar-anchorOriginTopRight- Applied when anchorOrigin=.MuiSnackbar-anchorOriginBottomRight- Applied when anchorOrigin=.MuiSnackbar-anchorOriginTopLeft- Applied when anchorOrigin=.MuiSnackbar-anchorOriginBottomLeft- Applied when anchorOrigin=
Best Practices
- Keep messages brief: Snackbars should contain a single line of text
- Use for confirmations: Best for confirming actions like “Email sent”
- Avoid critical information: Don’t use snackbars for critical errors
- Limit actions: Include at most one action button
- Don’t stack: Show one snackbar at a time (queue multiple messages)
- Auto-hide: Set appropriate
autoHideDuration(typically 4-10 seconds)
API Reference
For complete API documentation, see:Source Location
~/workspace/source/packages/mui-material/src/Snackbar/Snackbar.d.ts:208