Alerts display brief messages for the user without interrupting their use of the app.
Alerts give users brief and potentially time-sensitive information in an unobtrusive manner.
The MaterialĀ UI Alert component includes several props for quickly customizing its styles to provide immediate visual cues about its contents.
{{ādemoā: āSimpleAlert.jsā}}
This component is no longer documented in the Material Design guidelines, but MaterialĀ UI will continue to support it.
A key trait of the alert pattern is that it should not interrupt the userās experience of the app. Alerts should not be confused with alert dialogs (ARIA), which are intended to interrupt the user to obtain a response. Use the MaterialĀ UI Dialog component if you need this behavior.
import Alert from '@mui/material/Alert';
The Alert component wraps around its content, and stretches to fill its enclosing container.
The severity
prop accepts four values representing different statesāsuccess
(the default), info
, warning
, and error
āwith corresponding icon and color combinations for each:
{{ādemoā: āBasicAlerts.jsā}}
The Alert component comes with two alternative style optionsāfilled
and outlined
āwhich you can set using the variant
prop.
{{ādemoā: āFilledAlerts.jsā}}
{{ādemoā: āOutlinedAlerts.jsā}}
When using an outlined Alert with the Snackbar component, background content will be visible and bleed through the Alert by default. You can prevent this by adding
bgcolor: 'background.paper'
to thesx
prop on the Alert component:
<Alert sx={{ bgcolor: 'background.paper' }} />
Check out the Snackbarācustomization doc for an example of how to use these two components together.
Use the color
prop to override the default color for the specified severity
āfor instance, to apply warning
colors to a success
Alert:
{{ādemoā: āColorAlerts.jsā}}
Add an action to your Alert with the action
prop.
This lets you insert any elementāan HTML tag, an SVG icon, or a React component such as a MaterialĀ UI Buttonāafter the Alertās message, justified to the right.
If you provide an onClose
callback to the Alert without setting the action
prop, the component will display a close icon (ā) by default.
{{ādemoā: āActionAlerts.jsā}}
Use the icon
prop to override an Alertās icon.
As with the action
prop, your icon
can be an HTML element, an SVG icon, or a React component.
Set this prop to false
to remove the icon altogether.
If you need to override all instances of an icon for a given severity
, you can use the iconMapping
prop instead.
You can define this prop globally by customizing your appās theme. See Theme componentsāDefault props for details.
{{ādemoā: āIconAlerts.jsā}}
To add a title to an Alert, import the Alert Title component:
import AlertTitle from '@mui/material/AlertTitle';
You can nest this component above the message in your Alert for a neatly styled and properly aligned title, as shown below:
{{ādemoā: āDescriptionAlerts.jsā}}
You can use Transition components like Collapse to add motion to an Alertās entrance and exit.
{{ādemoā: āTransitionAlerts.jsā}}
Here are some factors to consider to ensure that your Alert is accessible:
tabindex
of 0
so it can be reached by keyboard-only users.The Alert component is composed of a root Paper component (which renders as a <div>
) that houses an icon, a message, and an optional action:
<div class="MuiPaper-root MuiAlert-root" role="alert">
<div class="MuiAlert-icon">
<!-- svg icon here -->
</div>
<div class="MuiAlert-message">This is how an Alert renders in the DOM.</div>
<div class="MuiAlert-action">
<!-- optional action element here -->
</div>
</div>