Files
heroui-vue/apps/docs/components/modal.md
T
maoyingjie 145a208687 docs: close documentation facts, structure and related-component mapping
HD-04 scope, against heroui ce90ea3 cn sources:

- stale/fictional content: input.md no longer claims TextField is
  unmigrated; color-field.md no longer claims ColorPicker is private;
  header.md drops the fictional Header.Root subcomponent and the stale
  'menus not migrated' wording (MenuSection label registration is now
  documented from HeaderRoot source); radio-group.md drops three copied
  CheckboxGroup wordings (variant override is Radio's, scalar emits,
  native single-value submit); input-otp.md drops the fictional
  data-complete state and lists the real missing upstream props
- toggle-button disabled row now matches the real CSS hooks
  (:disabled / [aria-disabled], with data-disabled as a marker only)
- part API / CSS classes / interactive states restored per pinned
  sources: button (full class lists + real variable-based global CSS),
  accordion (per-part tables + variant/state lists), card (per-part
  tables + variant list), switch (SwitchGroup snippet + Switch/
  SwitchGroup class lists + states), color-field Group (class list +
  states), input-otp (variant notes, global CSS, class/state lists);
  input-group gains the two upstream usage chapters
- RelatedComponents restored from the pinned componentRelationships
  (first-3, filtered against componentsMap): 64 pages rewritten,
  list-box/slider sections added, 7 empty-mapping pages' fabricated
  entries removed; RelatedShowcases left unmigrated with its real data
  inventory recorded (2 cases, showcase-site cards)
- defaults unified on implementation evidence: granularity (inferred
  day/minute) and visibleYears (full min-max span, no "or 20" fallback);
  HD-03 hand-offs resolved (trigger focus-visible noted for HD-06,
  width guards kept)
- new demos: header-collection-grouping (MenuSection/ListBoxSection
  label wiring), empty-state-collection (collection empty slot);
  empty-state page deduplicated with a boundary compatibility note
- ComponentPreview minHeight/isBgSolid restored per the 30 upstream
  marks; tabs-overflow max-w-full kept after measuring internal scroll
- tests: +3 header registration/demo cases, +1 empty-state collection
  case; full suite 110 files / 1630 tests, typecheck and build:docs
  pass
2026-10-28 22:32:10 +08:00

12 KiB
Raw Blame History

title, description, component, group
title description component group
Modal 模态框 用于聚焦用户交互与重要内容的对话框遮罩层。 modal 组件

基于固定 HeroUI 3.2.6(ce90ea3)迁移。命名组件、v-model、作用域插槽与 DOM attrs 为 Vue 适配;预览、复制与高亮来自同一份 SFC。

用法

import { Modal } from "@heroui-vue/components";

组件结构

Vue 使用具名组件导出。根组件也可将第一个 Button 作为触发器;明确组合时使用 ModalTrigger。

<script setup lang="ts">
import {
  Modal,
  ModalTrigger,
  ModalBackdrop,
  ModalContainer,
  ModalDialog,
  ModalCloseTrigger,
  ModalHeader,
  ModalHeading,
  ModalBody,
  ModalFooter,
  ModalClose,
  Button,
} from "@heroui-vue/components";
</script>
<template>
  <Modal>
    <ModalTrigger as-child><Button>Open Modal</Button></ModalTrigger>
    <ModalBackdrop>
      <ModalContainer>
        <ModalDialog>
          <ModalCloseTrigger />
          <ModalHeader><ModalHeading>标题</ModalHeading></ModalHeader>
          <ModalBody>内容</ModalBody>
          <ModalFooter>
            <ModalClose as-child><Button>关闭</Button></ModalClose>
          </ModalFooter>
        </ModalDialog>
      </ModalContainer>
    </ModalBackdrop>
  </Modal>
</template>

示例

尺寸

位置

滚动行为

受控状态

搭配表单

自定义触发器

遮罩变体

自定义遮罩

关闭行为

关闭方式

自定义动画

自定义 Portal

自定义样式

Tailwind CSS

全局 CSS

若要自定义组件类,可使用 @layer components 指令。了解更多。

@layer components {
  .hv-modal__backdrop {
    @apply bg-gradient-to-br from-black/50 to-black/70;
  }

  .hv-modal__dialog {
    @apply rounded-2xl border border-white/10 shadow-2xl;
  }

  .hv-modal__header {
    @apply text-center;
  }

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

样式参考

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

CSS 类

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

基础类

  • .hv-modal__trigger — 打开 Modal 的触发元素
  • .hv-modal__backdrop — Modal 背后的遮罩层
  • .hv-modal__container — 支持 placement 的定位包裹层
  • .hv-modal__dialog — Modal 内容容器
  • .hv-modal__header — 标题与图标区域
  • .hv-modal__body — 主内容区域
  • .hv-modal__footer — 操作区域
  • .hv-modal__close-trigger — 关闭按钮元素

遮罩变体

  • .hv-modal__backdrop--opaque — 不透明有色遮罩(默认)
  • .hv-modal__backdrop--blur — 带玻璃效果的模糊遮罩
  • .hv-modal__backdrop--transparent — 透明遮罩(无叠加层)

滚动变体

  • .hv-modal__container--scroll-outside — 允许整个 Modal 滚动
  • .hv-modal__dialog--scroll-inside — 限制 Modal 高度,由 body 区域滚动
  • .hv-modal__body--scroll-inside — 仅 body 区域可滚动
  • .hv-modal__body--scroll-outside — 允许整页滚动

交互状态

组件支持以下交互状态:

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

API 参考

Modal

Prop 类型 默认值 描述
默认插槽 VNode / 插槽 - 触发器与容器元素

ModalTrigger

Prop 类型 默认值 描述
默认插槽 VNode / 插槽 - 自定义触发内容
class string / object / array - CSS 类

ModalBackdrop

Prop 类型 默认值 描述
variant "opaque" | "blur" | "transparent" "opaque" 遮罩叠加样式
isDismissable boolean true 点击遮罩是否关闭
isKeyboardDismissDisabled boolean false 是否禁用 ESC 关闭
isOpen boolean - 受控打开状态
@open-change (isOpen: boolean) => void - 打开状态变化处理函数
class string / object / array - 遮罩 CSS 类
to HTMLElement - 自定义 portal 容器

ModalContainer

Prop 类型 默认值 描述
placement "auto" | "center" | "top" | "bottom" "auto" Modal 在屏幕上的位置
scroll "inside" | "outside" "inside" 滚动行为
size "xs" | "sm" | "md" | "lg" | "cover" | "full" "md" Modal 尺寸变体
class string / object / array - 容器 CSS 类

ModalDialog

Prop 类型 默认值 描述
默认插槽 作用域插槽 { close } - 内容或渲染函数
class string / object / array - CSS 类
role string "dialog" ARIA role
aria-label string - 无障碍标签
aria-labelledby string - 标签元素的 id
aria-describedby string - 描述元素的 id

ModalHeader

Prop 类型 默认值 描述
默认插槽 VNode / 插槽 - 头部内容
class string / object / array - CSS 类

ModalBody

Prop 类型 默认值 描述
默认插槽 VNode / 插槽 - 正文内容
class string / object / array - CSS 类

ModalFooter

Prop 类型 默认值 描述
默认插槽 VNode / 插槽 - 底部内容
class string / object / array - CSS 类

ModalCloseTrigger

Prop 类型 默认值 描述
默认插槽 VNode / 插槽 - 自定义关闭按钮
class string / object / array - CSS 类

useOverlayState Hook

import { useOverlayState } from "@heroui-vue/components";

const state = useOverlayState({
  defaultOpen: false,
  onOpenChange: console.log,
});
state.isOpen.value; // 当前状态(模板中的顶层 ref 自动解包)
state.open();
state.close();
state.toggle();
state.setOpen(true);

无障碍

实现 WAI-ARIA Dialog 模式:

  • 焦点陷阱:焦点锁定在 Modal 内
  • 键盘:ESC 关闭(启用时)、Tab 在元素间循环
  • 屏幕阅读器:正确的 ARIA 属性
  • 滚动锁定:打开时禁用 body 滚动

Vue API 与组合约定

Root 的 open / isOpen 支持受控值,defaultOpen 默认 false;推荐 v-model:open,事件为 update:open、openChange(模板 @open-change)。父级拒绝请求时保持父级状态,初始值只初始化一次。第一个 Button 可自动作为触发器;自定义内容用显式 Trigger,单元素组合可设 as-child。Trigger 的 class/style、ARIA 与 DOM 监听落在实际触发元素,disabled、isDisabled 与加载状态会阻止激活。

React 的 DOM render 回调改为 as 加原生 attrs,例如 as="div" data-custom="foo";class 函数改为 Vue 的字符串/对象/数组绑定。Enter/Space 对自定义按钮根只激活一次,调用者 preventDefault 可取消。Portal 默认 body,可由 ConfigProvider.teleportTo 或 Content/Backdrop 的 to 覆盖;目标应置于所需主题作用域中。SSR Portal 于客户端挂载后插入,尚未承诺 Nuxt 服务端收集。

状态与关闭

Modal 还接受 state: OverlayState,其优先级高于 open / isOpen;复用 useOverlayState()。ModalBackdrop 可脱离 Modal 单独使用 v-model:open,满足外部按钮控制场景。根已存在时由根统一管理状态。ModalContainer 的 size / placement / scroll 与基线相同,Body 继承滚动模式。

React 的 slot="close" 改为 <ModalClose as-child><Button>关闭</Button></ModalClose>;也可通过 ModalDialog 的 v-slot="{ close }" 自行关闭。ModalCloseTrigger 默认复用 CloseButton 的图标、可访问名称与变体。Header/Body/Footer/Icon/Heading 支持 as 与原生 attrs。

ModalDialog 的 ARIA/DOM 事件落在实际 role=dialog 的节点。isDismissable 与 isKeyboardDismissDisabled 位于 Backdrop,分别控制外部交互与 Escape;显式关闭不受它们影响。openAutoFocus、closeAutoFocus、escapeKeyDown、pointerDownOutside、focusOutside、interactOutside 以可取消事件透传。

Vue 补充部件

部件 属性 / 插槽 默认值 说明
ModalHeading as / id / class h2 / 自动唯一 ID 真实标题关联,显式 ID 可动态更新
ModalIcon as / class / 默认插槽 div 复用基线 40px 图标容器
ModalClose as / asChild / isDisabled button / false / false 显式替代 React close slot;保留取消与禁用
ModalCloseTrigger variant / isDisabled / disabled / aria-label default / false / false / Close 复用 CloseButton 的同名契约,默认插槽可自定义图标
Modal open / isOpen / defaultOpen / state undefined / undefined / false / undefined state 优先,推荐 v-model:open;update:open 和 openChange 的参数均为 boolean

相关组件

Button 按钮 · Tooltip 工具提示 · Select 选择器