A snackbar provides users with a brief, temporary message about app processes without interrupting their activity or experience.
The Unstyled Snackbar component is built to appear on-screen to inform users about an action that the app is taking.
Component
Usage
After installation, you can start building with this component using the following basic elements:
import SnackbarUnstyled from'@mui/base/SnackbarUnstyled';exportdefaultfunctionMyApp(){return<SnackbarUnstyled>{/* snackbar text */}</SnackbarUnstyled>;}
Basics
Unstyled Snackbar doesn't impose any restrictions on its implementation—it's up to you to design it so that it doesn't interrupt the user experience, and disappears after a set amount of time without requiring the user to take action.
Use the autoHideDuration prop to set the time (in milliseconds) that the snackbar remains on the screen.
The following demo illustrates the basic usage of Unstyled Snackbar.
Click Open snackbar to see how it behaves:
Anatomy
The Unstyled Snackbar component is composed of a single root <div> slot with no interior slots:
If you use a Click-Away Listener to let the user close the snackbar by clicking outside of it, make sure to pass the onClickAway handler returned by this hook to the Click-Away Listener.
Pass the open state to the hook and use it to show and hide the snackbar.
The demo below shows how to build a fully custom component with the useSnackbar hook that also incorporates the Click-Away Listener component:
Customization
Transitions
You can animate the open and close states of the snackbar with a render prop child and a transition component, as long as the component meets these conditions:
Is a direct child descendant of the snackbar
Has an in prop—this corresponds to the open state
Passes the exited prop to Unstyled Snackbar
Calls the onEnter callback prop when the enter transition starts—sets exited to false
Calls the onExited callback prop when the exit transition is completed—sets exited to true
These two callbacks allow the snackbar to unmount the child content when closed and keep it fully transitioned.
This is only applicable if you are using transition components using react-transition-group library internally.
The demo below shows how to create a snackbar with custom transitions:
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.
component
elementType
The component used for the root node. Either a string to use a HTML element or a component.
disableWindowBlurListener
bool
false
If true, the autoHideDuration timer will expire even if the window is not focused.
exited
bool
true
The prop used to handle exited transition and unmount the component.
onClose
func
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 Snackbaropen prop. The reason parameter can optionally be used to control the response to onClose, for example ignoring clickaway.
Signature: function(event: React.SyntheticEvent<any> | Event, reason: string) => void event: The event source of the callback. reason: Can be: "timeout" (autoHideDuration expired), "clickaway", or "escapeKeyDown".
open
bool
If true, the component is shown.
resumeHideDuration
number
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.
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.
disableWindowBlurListener
boolean
false
If true, the autoHideDuration timer will expire even if the window is not focused.
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 Snackbaropen prop.
The reason parameter can optionally be used to control the response to onClose,
for example ignoring clickaway.
open
boolean
If true, the component is shown.
ref
React.Ref<any>
resumeHideDuration
number
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.