Design Converter
Education
Software Development Executive - II
Last updated on Aug 2, 2024
Last updated on Aug 2, 2024
Have you ever wondered how to effectively inform users about app processes without interrupting their experience?
The Material UI Snackbar is a versatile component that delivers brief messages to users about app processes.
This blog will help you master the snackbar component, ensuring you can use it effectively in your projects.
A Snackbar component informs users of a process that an app has performed or will perform, appearing temporarily at the bottom of the screen. It's a non-intrusive way to communicate brief messages, also known as toasts, without requiring user input to disappear.
Snackbars play a crucial role in user interaction. They provide essential notifications about processes an app has performed or will perform without interrupting the user's workflow. Snackbars don’t require user input to disappear, making them a seamless part of the user experience.
Creating a simple snackbar is straightforward. Here's an example of a basic snackbar that mimics Google Keep's snackbar behavior:
1import React, { useState } from 'react'; 2import Snackbar from '@mui/material/Snackbar'; 3import Button from '@mui/material/Button'; 4 5function SimpleSnackbar() { 6 const [open, setOpen] = useState(false); 7 8 const handleClick = () => { 9 setOpen(true); 10 }; 11 12 const handleClose = (event, reason) => { 13 if (reason === 'clickaway') { 14 return; 15 } 16 setOpen(false); 17 }; 18 19 return ( 20 <div> 21 <Button onClick={handleClick}>Open simple snackbar</Button> 22 <Snackbar 23 open={open} 24 autoHideDuration={6000} 25 onClose={handleClose} 26 message="Note archived" 27 /> 28 </div> 29 ); 30} 31 32export default SimpleSnackbar;
This example demonstrates a basic snackbar that appears at the bottom of the screen and automatically dismisses after a set duration.
For more advanced use cases, you can customize the snackbar component. For instance, you can change its position, style, and behavior. The following example shows how to customize a snackbar:
1import React, { useState } from 'react'; 2import Snackbar from '@mui/material/Snackbar'; 3import Button from '@mui/material/Button'; 4import MuiAlert from '@mui/material/Alert'; 5 6const Alert = React.forwardRef(function Alert(props, ref) { 7 return <MuiAlert elevation={6} ref={ref} variant="filled" {...props} />; 8}); 9 10function CustomizedSnackbar() { 11 const [open, setOpen] = useState(false); 12 13 const handleClick = () => { 14 setOpen(true); 15 }; 16 17 const handleClose = (event, reason) => { 18 if (reason === 'clickaway') { 19 return; 20 } 21 setOpen(false); 22 }; 23 24 return ( 25 <div> 26 <Button onClick={handleClick}>Open customized snackbar</Button> 27 <Snackbar open={open} autoHideDuration={6000} onClose={handleClose}> 28 <Alert onClose={handleClose} severity="success"> 29 This is a success message! 30 </Alert> 31 </Snackbar> 32 </div> 33 ); 34} 35 36export default CustomizedSnackbar;
This example utilizes the Alert component from Material UI to create a more visually appealing snackbar.
Snackbars can be positioned in various locations on the screen by specifying the anchorOrigin prop. For example, you can center-align a snackbar in wide layouts:
1<Snackbar 2 anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }} 3 open={open} 4 onClose={handleClose} 5 message="Centered snackbar" 6/>
Positioned snackbars are helpful when you need to place notifications at specific spots on the screen.
The Snackbar component supports four global variants: plain, outlined (default), soft, and solid. These variants allow you to style the snackbar according to your app's theme.
Snackbars come in three sizes: sm, md (default), and lg. This flexibility ensures that the snackbar fits well within different layouts and design requirements.
You can customize the color of the snackbar using the color prop. Every palette included in the theme is available, allowing you to match the snackbar with your app's color scheme.
Control how long the snackbar is displayed using the autoHideDuration prop. If not provided, the snackbar will remain visible until dismissed by the user. There are three reasons for the snackbar to close: user interaction, auto-hide, and dismiss.
Use the startDecorator and endDecorator props to add icons or actions to either side of the snackbar. The invertedColors prop can be set to true to invert the colors of the children, increasing contrast.
Customize the snackbar's animation using the animationDuration prop. Snackbars support theming in terms of color and typography, allowing for a consistent design across your app.
Snackbars support content labeling for accessibility and are legible by most screen readers, including TalkBack. Text shown in snackbars is automatically given to accessibility services, therefore further content labels are typically unneeded.
Snackbars should follow WAI-ARIA guidelines for accessibility. This ensures that all users, including those who rely on screen readers, can access the information provided by the snackbar.
To create and show a snackbar, call the make function and use the show method on the returned Snackbar instance. Only one snackbar will be shown at a time, and showing a new snackbar will dismiss any previous ones.
By default, snackbars are anchored to the bottom edge of their parent view. However, you may use the setAnchorView method to position a snackbar above a specified view in your layout.
The default transition for snackbars is Grow, but you can customize this by changing the direction of the Slide transition. This allows for more dynamic and engaging notifications.
To add an action to a snackbar, call the setAction function on the object produced by make. Snackbars are immediately dismissed when the action is selected.
To show a snackbar with a message and an action, use the setAction method. This ensures that the snackbar is automatically dismissed when the action is clicked.
When multiple snackbar updates are necessary, ensure they appear one at a time. Showing a new snackbar will dismiss any previous ones first, maintaining a clean user experience.
Snackbars should appear above Floating Action Buttons (FABs) on mobile devices. Move the FAB vertically to accommodate the snackbar height and avoid blocking the action button.
Ensure that the snackbar does not block the floating action button, allowing users to access important actions within the app.
When a second snackbar is activated while the first is displaying, the first should begin the contraction motion downwards before the second animates upwards. This ensures a smooth transition between subsequent snackbars.
By following this guide, you should now have a comprehensive understanding of the Material UI snackbar component and how to use it effectively in your React applications. Snackbars are a powerful tool for providing brief messages about app processes without interrupting the user's workflow. Whether you need simple notifications or advanced customizations, the Material UI snackbar has you covered.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.