Alert
An alert displays a short, important message in a way that attracts the user's attention without interrupting the user's task.
Note: This component is not documented in the Material Design guidelines, but MUI supports it.
Basic alerts
The alert offers four severity levels that set a distinctive icon and color.
import * as React from 'react'; import Alert from '@mui/material/Alert'; import Stack from '@mui/material/Stack'; export default function BasicAlerts() { return ( <Stack sx={{ width: '100%' }} spacing={2}> <Alert severity="error">This is an error alert — check it out!</Alert> <Alert severity="warning">This is a warning alert — check it out!</Alert> <Alert severity="info">This is an info alert — check it out!</Alert> <Alert severity="success">This is a success alert — check it out!</Alert> </Stack> ); }
Description
You can use the
AlertTitle
component to display a formatted title above the content.import * as React from 'react'; import Alert from '@mui/material/Alert'; import AlertTitle from '@mui/material/AlertTitle'; import Stack from '@mui/material/Stack'; export default function DescriptionAlerts() { return ( <Stack sx={{ width: '100%' }} spacing={2}> <Alert severity="error"> <AlertTitle>Error</AlertTitle> This is an error alert — <strong>check it out!</strong> </Alert> <Alert severity="warning"> <AlertTitle>Warning</AlertTitle> This is a warning alert — <strong>check it out!</strong> </Alert> <Alert severity="info"> <AlertTitle>Info</AlertTitle> This is an info alert — <strong>check it out!</strong> </Alert> <Alert severity="success"> <AlertTitle>Success</AlertTitle> This is a success alert — <strong>check it out!</strong> </Alert> </Stack> ); }
Actions
An alert can have an action, such as a close or undo button. It is rendered after the message, at the end of the alert.
If an
onClose
callback is provided and no action
prop is set, a close icon is displayed. The action
prop can be used to provide an alternative action, for example using a Button or IconButton.import * as React from 'react'; import Alert from '@mui/material/Alert'; import Button from '@mui/material/Button'; import Stack from '@mui/material/Stack'; export default function ActionAlerts() { return ( <Stack sx={{ width: '100%' }} spacing={2}> <Alert onClose={() => {}}>This is a success alert — check it out!</Alert> <Alert action={ <Button color="inherit" size="small"> UNDO </Button> } > This is a success alert — check it out! </Alert> </Stack> ); }
Transition
You can use a transition component such as
Collapse
to transition the appearance of the alert.import * as React from 'react'; import Box from '@mui/material/Box'; import Alert from '@mui/material/Alert'; import IconButton from '@mui/material/IconButton'; import Collapse from '@mui/material/Collapse'; import Button from '@mui/material/Button'; import CloseIcon from '@mui/icons-material/Close'; export default function TransitionAlerts() { const [open, setOpen] = React.useState(true); return ( <Box sx={{ width: '100%' }}> <Collapse in={open}> <Alert action={ <IconButton aria-label="close" color="inherit" size="small" onClick={() => { setOpen(false); }} > <CloseIcon fontSize="inherit" /> </IconButton> } sx={{ mb: 2 }} > Close me! </Alert> </Collapse> <Button disabled={open} variant="outlined" onClick={() => { setOpen(true); }} > Re-open </Button> </Box> ); }
Icons
The
icon
prop allows you to add an icon to the beginning of the alert component. This will override the default icon for the specified severity.You can change the default severity to icon mapping with the
iconMapping
prop. This can be defined globally using theme customization.Setting the icon prop to
false
will remove the icon altogether.import * as React from 'react'; import Alert from '@mui/material/Alert'; import CheckIcon from '@mui/icons-material/Check'; import CheckCircleOutlineIcon from '@mui/icons-material/CheckCircleOutline'; import Stack from '@mui/material/Stack'; export default function IconAlerts() { return ( <Stack sx={{ width: '100%' }} spacing={2}> <Alert icon={<CheckIcon fontSize="inherit" />} severity="success"> This is a success alert — check it out! </Alert> <Alert iconMapping={{ success: <CheckCircleOutlineIcon fontSize="inherit" />, }} > This is a success alert — check it out! </Alert> <Alert icon={false} severity="success"> This is a success alert — check it out! </Alert> </Stack> ); }
Variants
Two additional variants are available – outlined, and filled:
Outlined
import * as React from 'react'; import Alert from '@mui/material/Alert'; import Stack from '@mui/material/Stack'; export default function BasicAlerts() { return ( <Stack sx={{ width: '100%' }} spacing={2}> <Alert variant="outlined" severity="error"> This is an error alert — check it out! </Alert> <Alert variant="outlined" severity="warning"> This is a warning alert — check it out! </Alert> <Alert variant="outlined" severity="info"> This is an info alert — check it out! </Alert> <Alert variant="outlined" severity="success"> This is a success alert — check it out! </Alert> </Stack> ); }
Filled
import * as React from 'react'; import Alert from '@mui/material/Alert'; import Stack from '@mui/material/Stack'; export default function BasicAlerts() { return ( <Stack sx={{ width: '100%' }} spacing={2}> <Alert variant="filled" severity="error"> This is an error alert — check it out! </Alert> <Alert variant="filled" severity="warning"> This is a warning alert — check it out! </Alert> <Alert variant="filled" severity="info"> This is an info alert — check it out! </Alert> <Alert variant="filled" severity="success"> This is a success alert — check it out! </Alert> </Stack> ); }
Toast
You can use the Snackbar to display a toast with the Alert.
Color
The
color
prop will override the default color for the specified severity.import * as React from 'react'; import Alert from '@mui/material/Alert'; export default function ColorAlerts() { return ( <Alert severity="success" color="info"> This is a success alert — check it out! </Alert> ); }
Accessibility
(WAI-ARIA: https://www.w3.org/TR/wai-aria-practices/#alert)
When the component is dynamically displayed, the content is automatically announced by most screen readers. At this time, screen readers do not inform users of alerts that are present when the page loads.
Using color to add meaning only provides a visual indication, which will not be conveyed to users of assistive technologies such as screen readers. Ensure that information denoted by the color is either obvious from the content itself (for example the visible text), or is included through alternative means, such as additional hidden text.
Actions must have a tab index of 0 so that they can be reached by keyboard-only users.