Snackbar API
Demos
For examples and details on the usage of this React component, visit the component demo pages:
Import
import Snackbar from '@mui/material/Snackbar';
// or
import { Snackbar } from '@mui/material';
Props
Props of the native component are also available.
The anchor of the Snackbar
. On smaller screens, the component grows to occupy all the available width, the horizontal alignment is ignored.
Type:
{ horizontal: 'center' | 'left' | 'right', vertical: 'bottom' | 'top' }
Default:
{ vertical: 'bottom', horizontal: 'left' }
The number of milliseconds to wait before automatically calling the onClose
function. onClose
should then set the state of the open
prop to hide the Snackbar. This behavior is disabled by default with the null
value.
Type:
number
Default:
null
If true
, the autoHideDuration
timer will expire even if the window is not focused.
Type:
bool
Default:
false
When displaying multiple consecutive Snackbars from a parent rendering a single <Snackbar/>, add the key prop to ensure independent treatment of each message. e.g. <Snackbar key={message} />, otherwise, the message may update-in-place and features such as autoHideDuration may be canceled.
Type:
any
Callback fired when the component requests to be closed. Typically onClose
is used to set state in the parent component, which is used to control the Snackbar
open
prop. The reason
parameter can optionally be used to control the response to onClose
, for example ignoring clickaway
.
Type:
func
Signature:
function(event: React.SyntheticEvent | Event, reason: string) => void
event
The event source of the callback.reason
Can be:"timeout"
(autoHideDuration
expired),"clickaway"
, or"escapeKeyDown"
.
The number of milliseconds to wait before dismissing after user interaction. If autoHideDuration
prop isn't specified, it does nothing. If autoHideDuration
prop is specified but resumeHideDuration
isn't, we default to autoHideDuration / 2
ms.
Type:
number
The system prop that allows defining system overrides as well as additional CSS styles.
Type:
Array<func | object | bool> | func | object
The component used for the transition. Follow this guide to learn more about the requirements for this component.
Type:
elementType
Default:
Grow
The duration for the transition, in milliseconds. You may specify a single timeout for all transitions, or individually with an object.
Type:
number | { appear?: number, enter?: number, exit?: number }
Default:
{
enter: theme.transitions.duration.enteringScreen,
exit: theme.transitions.duration.leavingScreen,
}
Props applied to the transition element. By default, the element is based on this Transition
component.
Type:
object
Default:
{}
ref
is forwarded to the root element.Theme default props
You can useMuiSnackbar
to change the default props of this component with the theme.CSS
The following class names are useful for styling with CSS (the state classes are marked).
To learn more, visit the component customization page.
Styles applied to the root element if anchorOrigin={{ 'top', 'center' }}
.
Styles applied to the root element if anchorOrigin={{ 'bottom', 'center' }}
.
Styles applied to the root element if anchorOrigin={{ 'top', 'right' }}
.
Styles applied to the root element if anchorOrigin={{ 'bottom', 'right' }}
.
Styles applied to the root element if anchorOrigin={{ 'top', 'left' }}
.
You can override the style of the component using one of these customization options:
- With a global class name.
- With a rule name as part of the component's
styleOverrides
property in a custom theme.