跳转到内容

Alert

Alerts display brief messages for the user without interrupting their use of the app.

Introduction

The Alert component can be used to provide important and potentially time-sensitive information in a way that does not interfere with the user's tasks. (Source: ARIA APG.)

<Alert />

Playground

Usage

After installation, you can start building with this component using the following basic elements:

import Alert from '@mui/joy/Alert';

export default function MyApp() {
  return <Alert />;
}

Basics

The Alert component wraps around its content, and stretches to fill its enclosing container, as shown below:

<Alert>This is a basic Alert.</Alert>

Anatomy

The Alert component is composed of a single root <div> element with its role set to alert:

<div role="alert" class="JoyAlert-root">
  This is how an Alert renders in the DOM.
</div>

Overriding the root slot

Use the component prop to override the root slot with a custom element. For example, the following code snippet replaces the default <div> with a <span>:

<Alert component="span">Alert content</Alert>

// renders as:
<span role="alert" class="JoyAlert-root">
  Alert content
</span>

Customization

Variants

The Alert component supports Joy UI's four global variants: soft (default), solid, outlined, and plain.

<Alert variant="solid">This is an Alert using the solid variant.</Alert>
<Alert variant="soft">This is an Alert using the soft variant.</Alert>
<Alert variant="outlined">This is an Alert using the outlined variant.</Alert>
<Alert variant="plain">This is an Alert using the plain variant.</Alert>

Colors

Every palette included in the theme is available via the color prop. The demo below shows how the values for the color prop are affected by the global variants:

Variant:

Sizes

The Alert component comes with three sizes out of the box: sm, md (the default), and lg:

<Alert size="sm">This is a small Alert.</Alert>
<Alert size="md">This is a medium Alert.</Alert>
<Alert size="lg">This is a large Alert.</Alert>

Decorators

Use the startDecorator and endDecorator props to append actions and icons to either side of the Alert:

Common examples

Various states

Danger alerts

Accessibility

Here are some factors to consider to ensure that your Alert is accessible:

  • Because alerts are not intended to interfere with the use of the app, your Alert component should never affect the keyboard focus.
  • If an alert contains an action, that action must have a tabindex of 0 so it can be reached by keyboard-only users.
  • Essential alerts should not disappear automatically—timed interactions can make your app inaccessible to users who need extra time to understand or locate the alert.
  • Alerts that occur too frequently can inhibit the usability of your app.
  • Dynamically rendered alerts are announced by screen readers; alerts that are already present on the page when it loads are not announced.
  • Color does not add meaning to the UI for users who require assistive technology. You must ensure that any information conveyed through color is also denoted in other ways, such as within the text of the alert itself, or with additional hidden text that's read by screen readers.