Files
heroui-vue/apps/docs/components/alert-dialog.md
T

301 lines
12 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: AlertDialog 警告对话框
description: 需要用户明确确认的警告对话框,默认禁止误关闭。
component: alert-dialog
group: 组件
---
> 基于固定 HeroUI 3.2.6(ce90ea3)迁移。命名组件、v-model、作用域插槽与 DOM attrs 为 Vue 适配;预览、复制与高亮来自同一份 SFC。
## 用法
```ts
import { AlertDialog } from "@heroui-vue/components";
```
<ComponentPreview name="alert-dialog-default" />
## 组件结构
```vue
<script setup lang="ts">
import {
AlertDialog,
AlertDialogBackdrop,
AlertDialogBody,
AlertDialogClose,
AlertDialogCloseTrigger,
AlertDialogContainer,
AlertDialogDialog,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogHeading,
AlertDialogIcon,
Button,
} from "@heroui-vue/components";
</script>
<template>
<AlertDialog>
<Button variant="danger">删除项目</Button>
<AlertDialogBackdrop>
<AlertDialogContainer>
<AlertDialogDialog>
<AlertDialogCloseTrigger />
<AlertDialogHeader>
<AlertDialogIcon status="danger" />
<AlertDialogHeading>要永久删除项目吗?</AlertDialogHeading>
</AlertDialogHeader>
<AlertDialogBody>此操作无法撤销。</AlertDialogBody>
<AlertDialogFooter>
<AlertDialogClose as-child>
<Button variant="tertiary">取消</Button>
</AlertDialogClose>
<AlertDialogClose as-child>
<Button variant="danger">删除</Button>
</AlertDialogClose>
</AlertDialogFooter>
</AlertDialogDialog>
</AlertDialogContainer>
</AlertDialogBackdrop>
</AlertDialog>
</template>
```
`Dialog` 渲染 `role="alertdialog"`。**默认 `isDismissable=false`、`isKeyboardDismissDisabled=true`**(与 Modal 相反),二者可独立配置。
## 示例
### 状态类型
<ComponentPreview name="alert-dialog-statuses" />
### 弹出位置
<ComponentPreview name="alert-dialog-placements" />
### 尺寸
<ComponentPreview name="alert-dialog-sizes" />
### 受控模式
<ComponentPreview name="alert-dialog-controlled" />
### 自定义图标
<ComponentPreview name="alert-dialog-custom-icon" />
### 自定义触发器
<ComponentPreview name="alert-dialog-custom-trigger" />
### 遮罩变体
<ComponentPreview name="alert-dialog-backdrop-variants" />
### 自定义遮罩
<ComponentPreview name="alert-dialog-custom-backdrop" />
### 关闭行为
<ComponentPreview name="alert-dialog-dismiss-behavior" />
### 关闭方式
<ComponentPreview name="alert-dialog-close-methods" />
### 自定义动画
<ComponentPreview name="alert-dialog-custom-animations" />
### 自定义 Portal
<ComponentPreview name="alert-dialog-custom-portal" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="alert-dialog-custom-styles" />
### 全局 CSS
若要自定义组件类,可使用 `@layer components` 指令:
```css
@layer components {
.hv-alert-dialog__backdrop {
@apply bg-gradient-to-br from-black/60 to-black/80;
}
.hv-alert-dialog__dialog {
@apply rounded-2xl border border-red-500/20 shadow-2xl;
}
.hv-alert-dialog__header {
@apply gap-4;
}
.hv-alert-dialog__icon {
@apply size-16;
}
.hv-alert-dialog__close-trigger {
@apply rounded-full bg-white/10 hover:bg-white/20;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论。类名前缀 `hv-`。
### CSS 类
#### 基础类
- `.hv-alert-dialog__trigger` - 打开警告对话框的触发元素
- `.hv-alert-dialog__backdrop` - 遮罩层
- `.hv-alert-dialog__container` - 支持 placement 的定位包裹层
- `.hv-alert-dialog__dialog` - 对话框内容容器
- `.hv-alert-dialog__header` - 图标与标题头部
- `.hv-alert-dialog__heading` - 标题文本
- `.hv-alert-dialog__body` - 主内容区域
- `.hv-alert-dialog__footer` - 操作按钮区域
- `.hv-alert-dialog__icon` - 状态色图标容器
- `.hv-alert-dialog__close-trigger` - 关闭按钮
#### 遮罩变体
- `.hv-alert-dialog__backdrop--opaque` / `--blur` / `--transparent`
#### 状态变体(图标)[!toc]
- `.hv-alert-dialog__icon--default` / `--accent` / `--success` / `--warning` / `--danger`
### 交互状态
- **Focus**:`:focus-visible` 或 `[data-focus-visible="true"]`
- **Hover**:`:hover` 或 `[data-hovered="true"]`
- **Active**:`:active` 或 `[data-pressed="true"]`
- **Entering**:`[data-entering]`
- **Exiting**:`[data-exiting]`
- **Placement**:`[data-placement="*"]`(auto、top、center、bottom)
## API 参考
### AlertDialog / AlertDialogRoot
| Prop | 类型 | 默认值 | 描述 |
| ---------------------------------------------- | -------------------------- | ------- | -------------------------------- |
| `open` / `isOpen` | `boolean` | - | 受控打开 |
| `defaultOpen` | `boolean` | `false` | 初始打开 |
| `state` | `OverlayState` | - | useOverlayState 实例 |
| `update:open` / `update:isOpen` / `openChange` | `(value: boolean) => void` | - | 状态事件 |
| 默认插槽 | `{ isOpen, close }` | - | 内容;首个 Button 自动作为触发器 |
### AlertDialogTrigger
| Prop | 类型 | 默认值 | 描述 |
| ------------ | --------------------------- | ------- | ---------------- |
| `as` | `string` | `'div'` | 渲染元素 |
| `asChild` | `boolean` | `false` | 合并到唯一子元素 |
| `isDisabled` | `boolean` | `false` | 禁用触发 |
| `class` | `string \| Array \| Object` | - | CSS 类 |
| 默认插槽 | - | - | 触发器内容 |
### AlertDialogBackdrop
| Prop | 类型 | 默认值 | 描述 |
| --------------------------------- | ------------------------------------- | ----------- | --------------------------------------------- |
| `variant` | `'opaque' \| 'blur' \| 'transparent'` | `'opaque'` | 遮罩样式 |
| `isDismissable` | `boolean` | **`false`** | 点击遮罩是否关闭 |
| `isKeyboardDismissDisabled` | `boolean` | **`true`** | 是否禁用 ESC |
| `open` / `isOpen` / `defaultOpen` | | | 无父 AlertDialog 时自建状态 |
| `state` | `OverlayState` | - | 受控状态 |
| `to` | `string \| HTMLElement` | body | 传送门容器(替代 `UNSTABLE_portalContainer`) |
| `update:open` / `openChange` | `(value: boolean) => void` | - | 状态事件 |
| 默认插槽 | `{ isOpen, close }` | - | Container 等 |
### AlertDialogContainer
| Prop | 类型 | 默认值 | 描述 |
| ----------- | ----------------------------------------- | -------- | --------- |
| `placement` | `'auto' \| 'center' \| 'top' \| 'bottom'` | `'auto'` | 位置 |
| `size` | `'xs' \| 'sm' \| 'md' \| 'lg' \| 'cover'` | `'md'` | 尺寸 |
| `as` | `string` | `'div'` | 渲染元素 |
| `class` | `string \| Array \| Object` | - | CSS 类 |
| 默认插槽 | `{ close }` | - | Dialog 等 |
### AlertDialogDialog
| Prop | 类型 | 默认值 | 描述 |
| ----------------------------------------------------- | --------------------------- | --------------- | ---------------- |
| `role` | `string` | `'alertdialog'` | ARIA role |
| `aria-label` / `aria-labelledby` / `aria-describedby` | `string` | 自动标题关联 | 无障碍名称与描述 |
| `as` | `string` | `'div'` | 渲染元素 |
| `id` | `string` | 自动 | 内容 id |
| `class` | `string \| Array \| Object` | - | CSS 类 |
| 默认插槽 | `{ close, isOpen }` | - | 面板内容 |
### AlertDialogHeader / Heading / Body / Footer
| 部件 | Prop | 描述 |
| ------- | -------------------------------- | ------------------ |
| Header | `class`、默认插槽 | 图标 + 标题容器 |
| Heading | `as`(默认 `h2`)、`id`、`class` | 注册为 dialog 标题 |
| Body | `class`、默认插槽 | 正文 |
| Footer | `class`、默认插槽 | 操作区 |
### AlertDialogIcon
| Prop | 类型 | 默认值 | 描述 |
| -------- | ------------------------------------------------------------- | ---------- | ------------------ |
| `status` | `'default' \| 'accent' \| 'success' \| 'warning' \| 'danger'` | `'danger'` | 状态配色与默认图标 |
| `as` | `string` | `'div'` | 渲染元素 |
| `class` | `string \| Array \| Object` | - | CSS 类 |
| 默认插槽 | - | 状态 SVG | 自定义图标 |
### AlertDialogCloseTrigger / AlertDialogClose
| 部件 | Prop | 描述 |
| ------------ | -------------------------------- | ------------------------------------------- |
| CloseTrigger | `variant`、`isDisabled`、`class` | X 按钮,点击关闭 |
| Close | `as`、`asChild`、`isDisabled` | 包裹任意控件以关闭;替代上游 `slot="close"` |
### useOverlayState
```ts
import { useOverlayState } from "@heroui-vue/components";
const state = useOverlayState({ defaultOpen: false });
state.isOpen.value;
state.open();
state.close();
state.toggle();
state.setOpen(true);
```
### 上游差异
| 上游 React | Vue |
| ----------------------------- | ------------------------------ |
| 对象子部件 `AlertDialog.Icon` | 具名导出 |
| `Button slot="close"` | `AlertDialogClose as-child` |
| Dialog render props | 作用域插槽 `{ close, isOpen }` |
| `UNSTABLE_portalContainer` | `to` |
| `onPress` | `@click` |
## 无障碍
- `role="alertdialog"`,标题经 `aria-labelledby` 关联真实 Heading
- 显式 `aria-labelledby` / `aria-label` 优先于自动计算
- 默认禁止 ESC 与外部点击误关闭
- 破坏性操作示例仅修改本地演示状态
## 相关组件
- [Modal 模态框](./modal)
- [Button 按钮](./button)