Snackbar
Snackbars provide brief notifications. The component is also known as a toast.
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.
Snackbars contain a single line of text directly related to the operation performed. They may contain a text action, but no icons. You can use them to display notifications.
Frequency
Only one snackbar may be displayed at a time.
Simple snackbars
A basic snackbar that aims to reproduce Google Keep's snackbar behavior.
<Button onClick={handleClick}>Open simple snackbar</Button>
<Snackbar
open={open}
autoHideDuration={6000}
onClose={handleClose}
message="Note archived"
action={action}
/>
Customization
Here are some examples of customizing the component. You can learn more about this in the overrides documentation page.
<Button variant="outlined" onClick={handleClick}>
Open success snackbar
</Button>
<Snackbar open={open} autoHideDuration={6000} onClose={handleClose}>
<Alert onClose={handleClose} severity="success" sx={{ width: '100%' }}>
This is a success message!
</Alert>
</Snackbar>
<Alert severity="error">This is an error message!</Alert>
<Alert severity="warning">This is a warning message!</Alert>
<Alert severity="info">This is an information message!</Alert>
<Alert severity="success">This is a success message!</Alert>
Positioned snackbars
In wide layouts, snackbars can be left-aligned or center-aligned if they are consistently placed on the same spot at the bottom of the screen, however there may be circumstances where the placement of the snackbar needs to be more flexible.
You can control the position of the snackbar by specifying the anchorOrigin
prop.
{buttons}
<Snackbar
anchorOrigin={{ vertical, horizontal }}
open={open}
onClose={handleClose}
message="I love snacks"
key={vertical + horizontal}
/>
Transitions
Consecutive Snackbars
When multiple snackbar updates are necessary, they should appear one at a time.
Change transition
Grow is the default transition but you can use a different one.
<Button onClick={handleClick(GrowTransition)}>Grow Transition</Button>
<Button onClick={handleClick(Fade)}>Fade Transition</Button>
<Button onClick={handleClick(SlideTransition)}>Slide Transition</Button>
<Snackbar
open={state.open}
onClose={handleClose}
TransitionComponent={state.Transition}
message="I love snacks"
key={state.Transition.name}
/>
Control Slide direction
You can change the direction of the Slide transition.
Example of making the slide transition to the left:
import Slide from '@material-ui/core/Slide';
function TransitionLeft(props) {
return <Slide {...props} direction="left" />;
}
export default function MyComponent() {
return <Snackbar TransitionComponent={TransitionLeft} />;
}
Other examples:
<Button onClick={handleClick(TransitionLeft)}>Right</Button>
<Button onClick={handleClick(TransitionUp)}>Up</Button>
<Button onClick={handleClick(TransitionRight)}>Left</Button>
<Button onClick={handleClick(TransitionDown)}>Down</Button>
<Snackbar
open={open}
onClose={handleClose}
TransitionComponent={transition}
message="I love snacks"
key={transition ? transition.name : ''}
/>
Complementary projects
For more advanced use cases you might be able to take advantage of:
notistack
This example demonstrates how to use notistack. notistack has an imperative API that makes it easy to display snackbars, without having to handle their open/close state. It also enables you to stack them on top of one another (although this is discouraged by the Material Design guidelines).
Accessibility
(WAI-ARIA: https://www.w3.org/TR/wai-aria-1.1/#alert)
- By default, the snackbar won't auto-hide. However, if you decide to use the
autoHideDuration
prop, it's recommended to give the user sufficient time to respond.