JGuang / HeroUI

AlertDialog 警告对话框

用于关键确认的模态对话框,需要用户关注并执行明确操作。

引入

import { AlertDialog } from "@heroui/react";

用法

HeroUI 示例

组件结构

导入 AlertDialog 组件后,可通过点语法访问各个子部分。

import {AlertDialog, Button} from "@heroui/react";

export default () => (
  <AlertDialog>
    <Button>Open Alert Dialog</Button>
    <AlertDialog.Backdrop>
      <AlertDialog.Container>
        <AlertDialog.Dialog>
          <AlertDialog.CloseTrigger /> {/* Optional: Close button */}
          <AlertDialog.Header>
            <AlertDialog.Icon /> {/* Optional: Status icon */}
            <AlertDialog.Heading />
          </AlertDialog.Header>
          <AlertDialog.Body />
          <AlertDialog.Footer />
        </AlertDialog.Dialog>
      </AlertDialog.Container>
    </AlertDialog.Backdrop>
  </AlertDialog>
);

状态

HeroUI 示例

位置

HeroUI 示例

背景变体

HeroUI 示例

尺寸

HeroUI 示例

自定义图标

HeroUI 示例

自定义背景

HeroUI 示例

关闭行为

HeroUI 示例

关闭方式

HeroUI 示例

受控状态

HeroUI 示例

自定义触发器

HeroUI 示例

自定义动画

HeroUI 示例

自定义 Portal

HeroUI 示例

样式

传入 Tailwind CSS 类

import {AlertDialog, Button} from "@heroui/react";

function CustomAlertDialog() {
  return (
    <AlertDialog>
      <Button variant="danger">Delete</Button>
      <AlertDialog.Backdrop className="bg-red-950/90">
        <AlertDialog.Container className="items-start pt-20">
          <AlertDialog.Dialog className="border-2 border-red-500 sm:max-w-[400px]">
            <AlertDialog.CloseTrigger />
            <AlertDialog.Header>
              <AlertDialog.Icon status="danger" />
              <AlertDialog.Heading>Custom Styled Alert</AlertDialog.Heading>
            </AlertDialog.Header>
            <AlertDialog.Body>
              <p>This alert dialog has custom styling applied via Tailwind classes</p>
            </AlertDialog.Body>
            <AlertDialog.Footer>
              <Button slot="close" variant="tertiary">
                Cancel
              </Button>
              <Button slot="close" variant="danger">
                Delete
              </Button>
            </AlertDialog.Footer>
          </AlertDialog.Dialog>
        </AlertDialog.Container>
      </AlertDialog.Backdrop>
    </AlertDialog>
  );
}

自定义组件类

要自定义 AlertDialog 的组件类,可使用 @layer components 指令。

了解更多.

@layer components {
  .alert-dialog__backdrop {
    @apply bg-gradient-to-br from-black/60 to-black/80;
  }

  .alert-dialog__dialog {
    @apply rounded-2xl border border-red-500/20 shadow-2xl;
  }

  .alert-dialog__header {
    @apply gap-4;
  }

  .alert-dialog__icon {
    @apply size-16;
  }

  .alert-dialog__close-trigger {
    @apply rounded-full bg-white/10 hover:bg-white/20;
  }
}

HeroUI 遵循 BEM 方法论,确保组件变体与状态可复用且易于自定义。

CSS 类

AlertDialog 使用以下 CSS 类(查看源码样式):

基础类

  • .alert-dialog__trigger - 打开对话框的触发元素
  • .alert-dialog__backdrop - 对话框背后的遮罩层
  • .alert-dialog__container - 支持位置配置的包裹层
  • .alert-dialog__dialog - 对话框内容容器
  • .alert-dialog__header - 图标与标题区域
  • .alert-dialog__heading - 标题文本样式
  • .alert-dialog__body - 主内容区域
  • .alert-dialog__footer - 操作按钮区域
  • .alert-dialog__icon - 带状态色的图标容器
  • .alert-dialog__close-trigger - 关闭按钮元素

背景变体

  • .alert-dialog__backdrop--opaque - 不透明有色背景(默认)
  • .alert-dialog__backdrop--blur - 带玻璃效果的模糊背景
  • .alert-dialog__backdrop--transparent - 透明背景(无遮罩)

状态变体(图标)

  • .alert-dialog__icon--default - 默认灰色状态
  • .alert-dialog__icon--accent - 强调蓝色状态
  • .alert-dialog__icon--success - 成功绿色状态
  • .alert-dialog__icon--warning - 警告橙色状态
  • .alert-dialog__icon--danger - 危险红色状态

交互状态

组件支持以下交互状态:

  • 聚焦::focus-visible 或 [data-focus-visible="true"] — 应用于触发器、对话框与关闭按钮
  • 悬停::hover 或 [data-hovered="true"] — 应用于关闭按钮悬停时
  • 激活::active 或 [data-pressed="true"] — 应用于关闭按钮按下时
  • 进入:[data-entering] — 应用于对话框打开动画期间
  • 离开:[data-exiting] — 应用于对话框关闭动画期间
  • 位置:[data-placement="*"] — 根据对话框位置应用(auto、top、center、bottom)

API 参考

AlertDialog

Prop类型默认值描述
childrenReactNode-触发器与容器元素

AlertDialog.Trigger

Prop类型默认值描述
childrenReactNode-自定义触发器内容
classNamestring-CSS 类

AlertDialog.Backdrop

Prop类型默认值描述
variant"opaque" | "blur" | "transparent""opaque"背景遮罩样式
isDismissablebooleanfalse点击背景是否关闭
isKeyboardDismissDisabledbooleantrue是否禁用 ESC 关闭
isOpenboolean-受控的打开状态
onOpenChange(isOpen: boolean) => void-打开状态变化的事件处理函数
classNamestring | (values) => string-背景的 CSS 类
UNSTABLE_portalContainerHTMLElement-自定义 portal 容器

AlertDialog.Container

Prop类型默认值描述
placement"auto" | "center" | "top" | "bottom""auto"对话框在屏幕上的位置
size"xs" | "sm" | "md" | "lg" | "cover""md"AlertDialog 尺寸变体
classNamestring | (values) => string-容器的 CSS 类

AlertDialog.Dialog

Prop类型默认值描述
childrenReactNode | ({close}) => ReactNode-内容或渲染函数
classNamestring-CSS 类
rolestring"alertdialog"ARIA role
aria-labelstring-无障碍标签
aria-labelledbystring-标签元素的 id
aria-describedbystring-描述元素的 id

AlertDialog.Header

Prop类型默认值描述
childrenReactNode-头部内容(通常为 Icon 与 Heading)
classNamestring-CSS 类

AlertDialog.Heading

Prop类型默认值描述
childrenReactNode-标题文本
classNamestring-CSS 类

AlertDialog.Body

Prop类型默认值描述
childrenReactNode-正文内容
classNamestring-CSS 类

AlertDialog.Footer

Prop类型默认值描述
childrenReactNode-底部内容(通常为操作按钮)
classNamestring-CSS 类

AlertDialog.Icon

Prop类型默认值描述
childrenReactNode-自定义图标元素
status"default" | "accent" | "success" | "warning" | "danger""danger"状态颜色变体
classNamestring-CSS 类

AlertDialog.CloseTrigger

Prop类型默认值描述
childrenReactNode-自定义关闭按钮
classNamestring | (values) => string-CSS 类

useOverlayState Hook

import {useOverlayState} from "@heroui/react";

const state = useOverlayState({
  defaultOpen: false,
  onOpenChange: (isOpen) => console.log(isOpen),
});

state.isOpen; // Current state
state.open(); // Open dialog
state.close(); // Close dialog
state.toggle(); // Toggle state
state.setOpen(); // Set state directly

无障碍

实现 WAI-ARIA AlertDialog 模式:

  • 焦点陷阱:焦点限制在 AlertDialog 内
  • 键盘:ESC 关闭(若启用)、Tab 在可聚焦元素间循环
  • 屏幕阅读器:role="alertdialog" 等合适的 ARIA 属性
  • 滚动锁定:打开时禁用 body 滚动
  • 需要明确操作:默认需要用户明确操作(不通过点击背景/ESC 轻易关闭)

本地来源: vendor/zh/react/components/(overlays)/alert-dialog.mdx

文档根据 HeroUI v3 固定提交生成,已调整内部链接与交互预览以适配 JGuang。

On this page