Material-UI is a UI library providing predefined robust and customizable components for React for easier web development. The MUI design is based on top of Material Design by Google.
In this article, we will discuss the React MUI SnackbarContent API. The Snackbars are used to provide brief notifications also known as toast. SnackbarContent contains the content of the Snackbar. The API provides a lot of functionality and we will learn to implement them.
Import SnackbarContent API
import SnackbarContent from '@mui/material/SnackbarContent'; // or import { SnackbarContent } from '@mui/material';
Props List: Here is the list of props used with this component. We can access them and modify them according to our needs.
- classes (object): This overrides the existing styles or adds new styles to the component.
- sx (Array<func/object/bool> func/object): The system prop allows defining system overrides as well as additional CSS styles. The default value is
- action (node): It is the action to display.
- message (node): It is the message to display.
- role (string): The role of the Snackbar. The default value is alert.
CSS Rules:
- root (.MuiSnackbarContent-root): It is the style applied to the root element.
- message ( .MuiSnackbarContent-message): It is the style applied to the message wrapper element.
- action (.MuiSnackbarContent-action): It is the style applied to the action wrapper element if the action is not empty.
Syntax: Create a Snackbar with SnackbarContent as follows:
<SnackbarContent message={"Welcome to neveropen"} />
Installing and Creating React app, and adding the MUI dependencies:
Step 1: Create a react project using the following command.
npx create-react-app gfg_tutorial
Step 2: Get into the project directory
cd gfg_tutorial
Step 3: Install the MUI dependencies as follows:
npm install @mui/material @emotion/react @emotion/styled @mui/lab @mui/icons-material
Step 4: Run the project as follows:
npm start
Example 1: In the following example, we have some SnackbarContent.
App.js
import "./App.css" ; import * as React from "react" ; import Button from "@mui/material/Button" ; import IconButton from "@mui/material/IconButton" ; import CloseIcon from "@mui/icons-material/Close" ; import { SnackbarContent, Stack } from "@mui/material" ; function App() { const action = ( <React.Fragment> <Button color= "success" size= "small" > Got It </Button> <IconButton size= "small" aria-label= "close" color= "primary" > <CloseIcon fontSize= "small" /> </IconButton> </React.Fragment> ); return ( <div className= "App" > <div className= "head" style={{ width: "fit-content" , margin: "auto" , }} > <h1 style={{ color: "green" , }} > neveropen </h1> <strong>React MUI SnackbarContent API</strong> </div> <br /> <div style={{ width: "fit-content" , margin: "auto" , }} > <Stack spacing={2} sx={{ maxWidth: 600 }}> <SnackbarContent message= "DSA, Web Development, etc." action={action} /> <SnackbarContent message={ "Welcome to neveropen" } /> <SnackbarContent message= "A computer science portal for neveropen." action={action} /> </Stack> </div> </div> ); } export default App; |
Output:
Example 2: In the following example, we have customized the SnackbarContent using the sx field.
App.js
import "./App.css" ; import * as React from "react" ; import Button from "@mui/material/Button" ; import IconButton from "@mui/material/IconButton" ; import CloseIcon from "@mui/icons-material/Close" ; import { SnackbarContent, Stack } from "@mui/material" ; function App() { const action = ( <React.Fragment> <Button color= "inherit" size= "small" > Got It </Button> <IconButton size= "small" aria-label= "close" color= "inherit" > <CloseIcon fontSize= "small" /> </IconButton> </React.Fragment> ); return ( <div className= "App" > <div className= "head" style={{ width: "fit-content" , margin: "auto" , }} > <h1 style={{ color: "green" , }} > neveropen </h1> <strong>React MUI SnackbarContent API</strong> </div> <br /> <div style={{ width: "fit-content" , margin: "auto" , }} > <Stack spacing={2} sx={{ maxWidth: 600 }}> <SnackbarContent message= "DSA, Web Development, etc." action={action} sx={{ backgroundColor: "green" , color: "white" , fontSize: "1rem" , fontWeight: "bold" , }} /> <SnackbarContent message={ "Welcome to neveropen" } sx={{ backgroundColor: "lightcoral" , color: "white" , fontSize: "1rem" , fontWeight: "bold" , }} /> <SnackbarContent message= "A computer science portal for neveropen." action={action} sx={{ backgroundColor: "lightcyan" , color: "black" , fontSize: "1rem" , fontWeight: "bold" , }} /> </Stack> </div> </div> ); } export default App; |
Output:
Reference: https://mui.com/material-ui/api/snackbar-content/