React Bootstrap logoReact Bootstrap

Alert

基础反馈

基于 Bootstrap 5 的警告框组件,为典型用户操作提供情景反馈消息,支持八种颜色变体、可关闭模式与淡出动画、链接、标题与图标

Component Demo

Basic usage and examples of the Alert component

变体

实时示例

可关闭

链接颜色

附加内容

图标

API Documentation

Complete API reference for the Alert component

Props

Alert

属性名类型默认值描述
variantAlertVariant'primary'情景颜色变体,渲染为 `alert-*` 类
dismissiblebooleanfalse可关闭模式,渲染 `alert-dismissible` 类并在内容末尾渲染关闭按钮
showboolean-受控的显示状态,为 `false` 时以淡出动画移除警告框;不传时为非受控,点击关闭按钮自动隐藏
onClose() => void-点击关闭按钮时触发的回调,受控模式下需配合 `show` 一起使用
closeLabelstring'Close alert'关闭按钮的无障碍标签,向屏幕阅读器描述按钮用途
closeVariantCloseButtonVariant'white'(variant 为 dark 时)关闭按钮变体,`variant="dark"` 时默认为 `white`,用于深色背景

AlertHeading

属性名类型默认值描述
asElementType'h4'渲染的标题元素标签,可选其他标题级别或元素
属性名类型默认值描述
asElementType'a'渲染的元素标签,默认渲染为 `a` 标签
hrefstring'#'链接地址

Common Props

属性名类型默认值描述
childrenReactNode-内容
classNamestring-自定义类名
...restHTMLAttributes-透传原生元素属性(如 `onClick`、`style` 等)

Type Definitions

AlertVariant

警告框情景颜色变体类型

export type AlertVariant =
  'danger' | 'dark' | 'info' | 'light' | 'primary' | 'secondary' | 'success' | 'warning';

AlertProps

警告框组件属性接口

export interface AlertProps extends HTMLAttributes<HTMLDivElement> {
  children?: ReactNode;
  className?: string;
  closeLabel?: string;
  closeVariant?: CloseButtonVariant;
  dismissible?: boolean;
  onClose?: () => void;
  show?: boolean;
  variant?: AlertVariant;
}

AlertHeadingProps

警告框标题组件属性接口

export interface AlertHeadingProps extends HTMLAttributes<HTMLElement> {
  as?: ElementType;
  children?: ReactNode;
  className?: string;
}

AlertLinkProps

警告框链接组件属性接口

export interface AlertLinkProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
  as?: ElementType;
  children?: ReactNode;
  className?: string;
  href?: string;
}