Reference
Alert
Alerts convey concise messages to users without disrupting their interaction with the application.
Introduction
Alerts provide users with brief, potentially time-sensitive information in a non-intrusive way.
The Material UI Alert component offers various props to customize its appearance, delivering immediate visual cues about its content.
import * as React from 'react'; import Alert from '@mui/material/Alert'; import CheckIcon from '@mui/icons-material/Check'; export default function SimpleAlert() { return ( <Alert icon={<CheckIcon fontSize="inherit" />} severity="success"> Here is a gentle confirmation that your action was successful. </Alert> ); }
This component is no longer included in the Material Design guidelines, but Material UI will continue to support it.
Usage
A defining characteristic of the alert pattern is that it does not interrupt the user's experience. Alerts should not be confused with alert dialogs (ARIA), which are designed to interrupt the user to prompt a response. For such behavior, use the Material UI Dialog component.
Basics
import Alert from '@mui/material/Alert';
The Alert component surrounds its content and expands to fill its parent container.
Severity
The
severity
prop supports four values—success
(default), info
, warning
, and error
—each with corresponding icons and colors: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="success">This is a success Alert.</Alert> <Alert severity="info">This is an info Alert.</Alert> <Alert severity="warning">This is a warning Alert.</Alert> <Alert severity="error">This is an error Alert.</Alert> </Stack> ); }
Variants
The Alert component provides two alternative styles—
filled
and outlined
—configurable via the variant
prop.Filled
import * as React from 'react'; import Alert from '@mui/material/Alert'; import Stack from '@mui/material/Stack'; export default function FilledAlerts() { return ( <Stack sx={{ width: '100%' }} spacing={2}> <Alert variant="filled" severity="success"> This is a filled success Alert. </Alert> <Alert variant="filled" severity="info"> This is a filled info Alert. </Alert> <Alert variant="filled" severity="warning"> This is a filled warning Alert. </Alert> <Alert variant="filled" severity="error"> This is a filled error Alert. </Alert> </Stack> ); }
Outlined
import * as React from 'react'; import Alert from '@mui/material/Alert'; import Stack from '@mui/material/Stack'; export default function OutlinedAlerts() { return ( <Stack sx={{ width: '100%' }} spacing={2}> <Alert variant="outlined" severity="success"> This is an outlined success Alert. </Alert> <Alert variant="outlined" severity="info"> This is an outlined info Alert. </Alert> <Alert variant="outlined" severity="warning"> This is an outlined warning Alert. </Alert> <Alert variant="outlined" severity="error"> This is an outlined error Alert. </Alert> </Stack> ); }
:::warning When using an outlined Alert with the Snackbar component, background content may be visible through the Alert by default. To prevent this, add
bgcolor: 'background.paper'
to the sx
prop on the Alert component:<Alert sx={{ bgcolor: 'background.paper' }} />
Refer to the Snackbar—customization documentation for an example of combining these components. :::
Color
Use the
color
prop to override the default color for a specified severity
, such as applying warning
colors to a success
Alert:import * as React from 'react'; import Alert from '@mui/material/Alert'; export default function ColorAlerts() { return ( <Alert severity="success" color="warning"> This is a success Alert with warning colors. </Alert> ); }
Actions
Add an action to an Alert using the
action
prop, allowing you to place any element—such as an HTML tag, SVG icon, or Material UI Button—after the Alert's message, aligned to the right.If you provide an
onClose
callback without setting the action
prop, the Alert will display a default close icon (âś•).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 severity="warning" onClose={() => {}}> This Alert displays the default close icon. </Alert> <Alert severity="success" action={ <Button color="inherit" size="small"> UNDO </Button> } > This Alert uses a Button component for its action. </Alert> </Stack> ); }
Icons
Override an Alert's icon using the
icon
prop, which accepts an HTML element, SVG icon, or React component. Set the prop to false
to remove the icon entirely.To override all icons for a given
severity
, use the iconMapping
prop instead. This can be defined globally by customizing the app's theme. See Theme components—Default props for details.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 success Alert has a custom icon. </Alert> <Alert icon={false} severity="success"> This success Alert has no icon. </Alert> <Alert iconMapping={{ success: <CheckCircleOutlineIcon fontSize="inherit" />, }} > This success Alert uses `iconMapping` to override the default icon. </Alert> </Stack> ); }
Customization
Titles
To include a title in an Alert, import the AlertTitle component:
import AlertTitle from '@mui/material/AlertTitle';
Nest the AlertTitle component above the message in your Alert for a well-styled and properly aligned title, as demonstrated below:
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="success"> <AlertTitle>Success</AlertTitle> This is a success Alert with an encouraging title. </Alert> <Alert severity="info"> <AlertTitle>Info</AlertTitle> This is an info Alert with an informative title. </Alert> <Alert severity="warning"> <AlertTitle>Warning</AlertTitle> This is a warning Alert with a cautious title. </Alert> <Alert severity="error"> <AlertTitle>Error</AlertTitle> This is an error Alert with a scary title. </Alert> </Stack> ); }
Transitions
Enhance an Alert's entrance and exit with Transition components such as Collapse.
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 }} > Click the close icon to see the Collapse transition in action! </Alert> </Collapse> <Button disabled={open} variant="outlined" onClick={() => { setOpen(true); }} > Re-open </Button> </Box> ); }
Accessibility
To ensure your Alert is accessible, consider the following:
- Alerts are designed to avoid interfering with the app's usability, so the Alert component should never affect keyboard focus.
- If an alert includes an action, that action must have a
tabindex
of0
to ensure accessibility for keyboard-only users. - Critical alerts should not disappear automatically, as timed interactions can hinder accessibility for users who need more time to read or locate the alert.
- Frequent alerts can impair the usability of your application.
- Dynamically rendered alerts are announced by screen readers, whereas alerts present on page load are not announced.
- Color alone should not convey meaning for users relying on assistive technology. Ensure that information conveyed through color is also expressed through text or hidden text accessible to screen readers.
Anatomy
The Alert component consists of a root Paper component (rendered as a
<div>
) containing an icon, a message, and an optional action:<div className="MuiPaper-root MuiAlert-root" role="alert"> <div className="MuiAlert-icon"> </div> <div className="MuiAlert-message">This is how an Alert renders in the DOM.</div> <div className="MuiAlert-action"> </div> </div>