提示信息
展示上下文中的成功、警告、错误或信息提示。
本指南覆盖默认 Emotion 环境的 Material UI Core;扩展包与额外示例按清单单独标注。
使用场景与业务边界
严重程度只是提示语义,不证明业务成功;持续性错误还需要恢复操作。
原生组成与范围
Alert, AlertTitle
保持 Material UI 原生组合与类型,不替换为 JGuang/Base UI/Radix 同名 API。示例条目不是额外组件。
原生安装与初始化
npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1
采用原生 npm 包与 Emotion,不提供虚构的 JGuang UI Registry 地址。将示例放在原生 ThemeProvider 内,按项目选择 CssBaseline 或 ScopedCssBaseline。先检查 React、现有 Provider、锁文件与 SSR 配置;不要覆盖用户文件或忽略 peer 冲突。本批实际验收 React 19.2.8;React 18 及以下须匹配 react-is。完整初始化规则见随附的双语 setup 文档。
关键原生参数
仅解释本指南选出的关键参数,不是平铺的通用 API。原生继承来源、slot 结构、可空值与泛型仍按固定源码保留。
| 所属组件.属性 | 用途说明 | 声明来源 |
|---|---|---|
Alert.severity | Alert 原生语义严重程度,与自定义颜色不同。 | Alert |
Alert.variant | 所属组件的原生视觉枚举,不可替换为通用 variant 属性。 | Alert |
Alert.onClose | 原生关闭请求,reason/event 签名因组件不同,宿主须更新 open。 | Alert |
可运行原生示例
下列机器键只选择示例入口,不会作为通用 variant 属性传入每个组件。原生示例 JSX 与数据保持上游原文;JGuang 编写的验收示例明确标注。
- 基础介绍 ·
SimpleAlert· 上游原生示例 - 提示严重程度 ·
BasicAlerts· 上游原生示例 - 填充样式 ·
FilledAlerts· 上游原生示例 - 描边样式 ·
OutlinedAlerts· 上游原生示例 - 颜色 ·
ColorAlerts· 上游原生示例 - 操作 ·
ActionAlerts· 上游原生示例 - 图标配置 ·
IconAlerts· 上游原生示例 - 标题 ·
DescriptionAlerts· 上游原生示例 - 多种过渡动画 ·
TransitionAlerts· 上游原生示例
真实使用示例
固定版本的未修改原生示例;复制时同时保留所需的局部辅助文件。
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>
);
}
示例依赖边界
@mui/material/Alert, @mui/icons-material/Check, @mui/material/Stack, @mui/material/Button, @mui/icons-material/CheckCircleOutlined, @mui/material/AlertTitle, react, @mui/material/Box, @mui/material/IconButton, @mui/material/Collapse, @mui/icons-material/Close
直接导入 Icons/System/Utils 的示例需要匹配 9.4.0 包。局部数据与辅助文件属于示例闭包,不是额外组件。远程图片和模拟操作不提供后端。
固定上游 API 元数据参考
以下表格保留上游元数据,不是重新推断的通用类型体系。条件泛型、继承属性与回调细节以以下源码声明为准。主题默认配置可能覆盖元数据默认值。
Alert
import Alert from '@mui/material/Alert';
import { Alert } from '@mui/material';
原生继承: Paper. 父级与 DOM 属性未被伪造为本组件新属性。
| 属性 | 必需 | 上游元数据类型/约束 | 元数据默认值 |
|---|---|---|---|
action | 否 | node | — |
children | 否 | node | — |
classes | 否 | object | — |
closeText | 否 | string | 'Close' |
color | 否 | 'error' | 'info' | 'success' | 'warning' | string | — |
icon | 否 | node | — |
iconMapping | 否 | { error?: node, info?: node, success?: node, warning?: node } | — |
onClose | 否 | func | — |
role | 否 | string | 'alert' |
severity | 否 | 'error' | 'info' | 'success' | 'warning' | string | 'success' |
slotProps | 否 | { action?: func | object, closeButton?: func | object, closeIcon?: func | object, icon?: func | object, message?: func | object, root?: func | object } | {} |
slots | 否 | { action?: elementType, closeButton?: elementType, closeIcon?: elementType, icon?: elementType, message?: elementType, root?: elementType } | {} |
sx | 否 | Array<func | object | bool> | func | object | — |
variant | 否 | 'filled' | 'outlined' | 'standard' | string | 'standard' |
AlertTitle
import AlertTitle from '@mui/material/AlertTitle';
import { AlertTitle } from '@mui/material';
原生继承: Typography. 父级与 DOM 属性未被伪造为本组件新属性。
| 属性 | 必需 | 上游元数据类型/约束 | 元数据默认值 |
|---|---|---|---|
children | 否 | node | — |
classes | 否 | object | — |
sx | 否 | Array<func | object | bool> | func | object | — |
原生源码声明
声明与 import 从固定源码提取,不平铺联合类型或外部继承。英文注释/类型属于原始源码参考,不代表上游正文已全文翻译。共享类型仍位于原生包中。
vendor/src/Alert/Alert.d.ts
import * as React from 'react';
import { OverridableStringUnion } from '@mui/types';
import { SxProps } from '@mui/system';
import { SvgIconProps } from '../SvgIcon';
import { Theme } from '../styles';
import { InternalStandardProps as StandardProps } from '../internal';
import { IconButtonProps } from '../IconButton';
import { PaperProps } from '../Paper';
import { AlertClasses } from './alertClasses';
import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';
export type AlertColor = 'success' | 'info' | 'warning' | 'error';
export interface AlertPropsVariantOverrides {}
export interface AlertPropsColorOverrides {}
export interface AlertRootSlotPropsOverrides {}
export interface AlertIconSlotPropsOverrides {}
export interface AlertMessageSlotPropsOverrides {}
export interface AlertActionSlotPropsOverrides {}
export interface AlertCloseButtonSlotPropsOverrides {}
export interface AlertCloseIconSlotPropsOverrides {}
export interface AlertSlots {
/**
* The component that renders the root slot.
* @default Paper
*/
root: React.ElementType;
/**
* The component that renders the icon slot.
* @default div
*/
icon: React.ElementType;
/**
* The component that renders the message slot.
* @default div
*/
message: React.ElementType;
/**
* The component that renders the action slot.
* @default div
*/
action: React.ElementType;
/**
* The component that renders the close button.
* @default IconButton
*/
closeButton: React.ElementType;
/**
* The component that renders the close icon.
* @default svg
*/
closeIcon: React.ElementType;
}
export type AlertSlotsAndSlotProps = CreateSlotsAndSlotProps<
AlertSlots,
{
/**
* Props forwarded to the root slot.
* By default, the available props are based on the [Paper](https://mui.com/material-ui/api/paper/#props) component.
*/
root: SlotProps<React.ElementType<PaperProps>, AlertRootSlotPropsOverrides, AlertOwnerState>;
/**
* Props forwarded to the icon slot.
* By default, the available props are based on a div element.
*/
icon: SlotProps<'div', AlertIconSlotPropsOverrides, AlertOwnerState>;
/**
* Props forwarded to the message slot.
* By default, the available props are based on a div element.
*/
message: SlotProps<'div', AlertMessageSlotPropsOverrides, AlertOwnerState>;
/**
* Props forwarded to the action slot.
* By default, the available props are based on a div element.
*/
action: SlotProps<'div', AlertActionSlotPropsOverrides, AlertOwnerState>;
/**
* Props forwarded to the closeButton slot.
* By default, the available props are based on the [IconButton](https://mui.com/material-ui/api/icon-button/#props) component.
*/
closeButton: SlotProps<
React.ElementType<IconButtonProps>,
AlertCloseButtonSlotPropsOverrides,
AlertOwnerState
>;
/**
* Props forwarded to the closeIcon slot.
* By default, the available props are based on the [SvgIcon](https://mui.com/material-ui/api/svg-icon/#props) component.
*/
closeIcon: SlotProps<
React.ElementType<SvgIconProps>,
AlertCloseIconSlotPropsOverrides,
AlertOwnerState
>;
}
>;
export interface AlertProps extends StandardProps<PaperProps, 'variant'>, AlertSlotsAndSlotProps {
/**
* The action to display. It renders after the message, at the end of the alert.
*/
action?: React.ReactNode;
/**
* Override or extend the styles applied to the component.
*/
classes?: Partial<AlertClasses> | undefined;
/**
* Override the default label for the *close popup* icon button.
*
* For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
* @default 'Close'
*/
closeText?: string | undefined;
/**
* The color of the component. Unless provided, the value is taken from the `severity` prop.
* It supports both default and custom theme colors, which can be added as shown in the
* [palette customization guide](https://mui.com/material-ui/customization/palette/#custom-colors).
*/
color?: OverridableStringUnion<AlertColor, AlertPropsColorOverrides> | undefined;
/**
* The severity of the alert. This defines the color and icon used.
* @default 'success'
*/
severity?: OverridableStringUnion<AlertColor, AlertPropsColorOverrides> | undefined;
/**
* Override the icon displayed before the children.
* Unless provided, the icon is mapped to the value of the `severity` prop.
* Set to `false` to remove the `icon`.
*/
icon?: React.ReactNode;
/**
* The ARIA role attribute of the element.
* @default 'alert'
*/
role?: string | undefined;
/**
* The component maps the `severity` prop to a range of different icons,
* for instance success to `<SuccessOutlined>`.
* If you wish to change this mapping, you can provide your own.
* Alternatively, you can use the `icon` prop to override the icon displayed.
*/
iconMapping?:
| Partial<Record<OverridableStringUnion<AlertColor, AlertPropsColorOverrides>, React.ReactNode>>
| undefined;
/**
* Callback fired when the component requests to be closed.
* When provided and no `action` prop is set, a close icon button is displayed that triggers the callback when clicked.
* @param {React.SyntheticEvent} event The event source of the callback.
*/
onClose?: ((event: React.SyntheticEvent) => void) | undefined;
/**
* The variant to use.
* @default 'standard'
*/
variant?:
| OverridableStringUnion<'standard' | 'filled' | 'outlined', AlertPropsVariantOverrides>
| undefined;
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme> | undefined;
}
export interface AlertOwnerState extends AlertProps {}
export default function Alert(props: AlertProps): React.JSX.Element;
vendor/src/AlertTitle/AlertTitle.d.ts
import * as React from 'react';
import { SxProps } from '@mui/system';
import { Theme } from '../styles';
import { TypographyProps } from '../Typography';
import { AlertTitleClasses } from './alertTitleClasses';
export interface AlertTitleProps extends TypographyProps<'div'> {
/**
* The content of the component.
*/
children?: React.ReactNode;
/**
* Override or extend the styles applied to the component.
*/
classes?: Partial<AlertTitleClasses> | undefined;
/**
* The system prop that allows defining system overrides as well as additional CSS styles.
*/
sx?: SxProps<Theme> | undefined;
}
export default function AlertTitle(props: AlertTitleProps): React.JSX.Element;
安装后验收
执行严格类型检查、生产构建和实际交互。检查键盘标签/焦点、原生支持的禁用/错误/只读语义、深浅色、窄屏和减少动画。按真实宿主数据检查回调,不把复制提示词当作安装完成。初始化工具还需检查服务端 HTML 与无告警水合。
来源与许可
https://github.com/mui/material-ui/tree/809a7717b4c050ba3f69b75300689f07c050a16e
MIT · @mui/material@9.4.0. 保留上游原始内容与许可证。上游完整原文保留于 vendor/docs,不标为全文中文化。