Files
heroui-vue/docs/migrations/B01-fidelity-audit.md
T
maoyingjie 53b4639b0e fix: portal dropdown surfaces and close the review capability gaps
- Root and submenu surfaces render through the dropdown portal with the overlay
  z-index, so clipping/isolation ancestors no longer cut the menu; panel height
  is capped by the floating available height and the animation origin maps the
  real transform origin.
- Standalone Menu is a static in-flow list: no scroll lock, no outside pointer
  blocking, no floating placement and no focus stealing.
- Selection ownership is isolated per level: section disabled keys add to the
  menu list, and a nested Menu resets the section owner.
- closeOnSelect resolves item > menu policy > per-mode default for selection
  items; actions and radio items close, checkboxes stay open.
- Business keys no longer become DOM ids (instance ids + data-key), and submenu
  surfaces label themselves from the real trigger.
- Long-press keydown runs the caller's handlers first and only opens when not
  cancelled; submenu panels merge caller keydown with the internal Escape.
- Link items render a real anchor, aria labels land on the role=menu node,
  submenu panels forward placement/offset/collision props and caller attrs, and
  the popover provides the default Surface variant.
- Wrapper parts declare real props/emits, plus the missing official capability
  props (disallowEmptySelection, escapeKeyBehavior, shouldFocusWrap, autoFocus,
  shouldCloseOnSelect, close, empty slot, isKeyboardDismissDisabled,
  shouldCloseOnInteractOutside, placement/offset family, scoped slots).
- Ship an independent consumer fixture for browser-level verification.
2026-10-24 22:56:44 +08:00

113 lines
26 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.
# B01 忠实度审计:Dropdown / Menu 家族
2026-10-24。基线 HeroUI 3.2.6 / `ce90ea3`(只读 `../heroui`)。对照对象:`apps/docs/components/dropdown.md`、`apps/docs/demos/dropdown-*.vue`、`packages/vue/src/components/{dropdown,menu,menu-item,menu-section}`、`packages/styles/components/{dropdown,menu,menu-item,menu-section}.css`。
对照依据:官方 `apps/docs/content/docs/cn/react/components/(collections)/dropdown.mdx`(380 行)、官方 17 个演示 `apps/docs/src/demos/cn/dropdown/*.tsx`、上游 `packages/react/src/components/{dropdown,menu,menu-item,menu-section}`、`packages/styles/components/*.css`、`components-registry.ts`。DOM 与计算样式结论来自 jsdom 实渲染与浏览器 5181 实测。
状态列:`已修` / `未修(原因)` / `保留(设计选择)`。
## A. 组件实现:不是复刻的地方
| ID | 问题 | 证据 | 状态 |
| --- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- |
| A1 | 禁用项没有禁用样式:移植 CSS 用 `[data-disabled="true"]`,Reka 只写空串 `data-disabled=""`,选择器永不命中,`with-disabled-items` 的删除行不置灰 | `menu-item.css:81`;`reka-ui/Menu/MenuItemImpl.vue:85`;dist 为 `.hv-menu-item[data-disabled=true]`;DOM 实测 `data-disabled=""`;测试 `tests/b01-menu-dropdown.test.ts` 断言放宽故漏检 | 已修 |
| A2 | 进入/退出动画、placement 位移全部失效:CSS 依赖 `[data-entering]`/`[data-exiting]`/`[data-placement]`,Dropdown 链路从不发这些属性 | `dropdown.css:54-84`;DOM 实测只有 `data-state`/`data-side`/`data-align`;适配层只存在于 Popover/Tooltip/Drawer/Toast | 已修 |
| A3 | 每个弹出层出现两层 `role="menu"`(Reka content 本身已是 menu,`Menu` 又给内层加了 role) | `Menu.vue:112`;实测 `querySelectorAll('[role="menu"]').length === 2`;官方只有一个 menu | 已修 |
| A4 | 条目没有 DOM `id`;子菜单 content 的 `aria-labelledby` 指向不存在的节点 | `MenuItem.vue` 只把 `id` 当选择 key;实测条目 `id=null`、子菜单 `aria-labelledby=reka-menu-sub-trigger-v-2` → MISSING | 已修 |
| A5 | 链接型菜单项无法实现:`href` 落到 `div[role=menuitem]`,没有 `<a>` | Reka `MenuItem` 无链接路径;官方 RAC `MenuItem` 支持 `href/target` | 未修(能力缺失) |
| A6 | 自定义触发器变成不可聚焦 `<span>`;官方是 `<button>`,可键盘操作 | 实测 `<span role=null tabindex=null aria-haspopup=menu>`;官方 `Dropdown.Trigger` = RAC `Button` | 已修 |
| A7 | 触发器被加 `hv-dropdown__trigger`,`display:inline-block` 覆盖 Button 的 `inline-flex`,图标触发器偏移 | dist 顺序 36211(`.hv-button`) → 270480(`.hv-dropdown__trigger`);浏览器实测计算 `display:block`、图标中心偏左 12px;Modal/Popover/Tooltip 都有 `.X__trigger.hv-button` 兼容规则,Dropdown 缺 | 已修 |
| A8 | 子菜单能力依赖「是否渲染指示器组件」:不带 `<DropdownSubmenuIndicator>` 的子菜单项被当普通动作项,点击关闭整栈 | `MenuItemSubmenuIndicator.vue:37`;`MenuItem.vue:84` | 已修 |
| A9 | Section 标题与分组没有关联:`div[role=group]` 无 `aria-labelledby`,`Header` 无 id/注册 | `MenuSection.vue:119-129`;`Header.vue` | 已修 |
| A10 | 菜单项没有官方 Render Props(`isSelected/isFocused/isDisabled/isPressed`);指示器只有 `isSelected`,缺 `isIndeterminate` | 官方 `dropdown.mdx:331-363`;`MenuItem.vue` 直接透传 slot;`MenuItemIndicator.vue:62` | 已修 |
| A11 | 不发 `data-focused`/`data-hovered`/`data-pressed`/`data-focus-visible`:`custom-styles` 演示的 `data-[focused=true]` 工具类永不生效,`menu-item.css` 对应分支为死代码 | 官方演示 `custom-styles.tsx:10,17,24`;实测条目无 `data-focused`;Reka 只发 `data-highlighted`,而移植 CSS 没有该选择器 | 已修 |
| A12 | 子菜单项/子菜单 Popover 丢弃 `class`/`aria-*` | `DropdownSubmenuTrigger.vue:42-43`(`inheritAttrs:false` + `useAttrs()` 未使用);`DropdownPopover.vue:51-56` Fragment 分支;实测两者均丢 | 已修 |
| A13 | 用户 class 不经过 twMerge,冲突工具类压不掉基础类 | `Menu.vue:107`、`MenuItem.vue:125`、`MenuSection.vue:124` 用 `join(" ")`;官方走 tailwind-variants | 已修 |
| A14 | 默认子菜单指示器是自绘描边 SVG,官方是 `IconChevronRight`(16 视框实心) | `MenuItemSubmenuIndicator.vue:12-28`;官方 `menu-item.tsx:145` | 已修 |
| A15 | 独立 `Menu` 丢了 `data-slot="menu"`;`data-slot="menu-section"` 又是新增 | 官方 `menu.tsx:26` 用 `data-slot="menu"`;`Menu.vue` 恒为 `dropdown-menu` | 已修 |
| A16 | `MenuSection.as` 声明但渲染不使用(死参数) | `MenuSection.vue:27` vs `:119-129` | 已修 |
| A17 | Section 只传 `selectedKeys` 时自动推断 `multiple`,官方无此推断 | `MenuSection.vue:63-67` | 已修 |
| A18 | 子菜单悬停开启延迟:Reka 固定 100ms,官方 `delay` 默认 200ms 可配 | 官方 `dropdown.mdx:352`;Reka `MenuSubTrigger` 100ms | 未修(底座固定值) |
| A19 | 长按键盘语义自定(Enter/Space 切换),RAC longPress 文档只写 Alt+↓;另加 `longPressDuration`、10px 移动取消等无上游对应项 | `DropdownTrigger.vue:71-76`;官方 `dropdown.mdx:259` 无时长参数 | 保留(复核要求) |
| A20 | `DropdownRoot` 自带一层 `div.hv-dropdown` + `data-trigger`,官方该类在无 DOM 宿主 | `DropdownRoot.vue:58-66`;官方 `dropdown.tsx:39-48` | 保留(结构差异) |
| A21 | 缺失的官方 props:`disallowEmptySelection`、`escapeKeyBehavior`、`shouldFocusWrap`、`autoFocus`、`onClose`、`items`/`renderEmptyState`、`shouldCloseOnSelect`(菜单级)、`isKeyboardDismissDisabled`、`shouldCloseOnInteractOutside`、Popover 全部 render props | 官方 `dropdown.mdx:282-321` 及各部件表 | 未修(公开 prop 缺口) |
## B. 17 个演示与官方源码的差异
| ID | 问题 | 证据 | 状态 |
| --- | ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------- |
| B1 | 每个演示多包一层 `<DropdownTrigger>`(官方是 `Dropdown` 直接子级 `Button`) | 17/17 演示;官方 `dropdown.tsx:39-47` | 保留(既定 API) |
| B2 | `Kbd slot="keyboard"` 全部丢失,共 27 处 | with-icons 4、with-descriptions 4、with-disabled-items 3、with-sections 3、with-keyboard-shortcuts 4、with-section-level-selection 9;Vue 端 0 处 | 已修 |
| B3 | 图标换成了不同概念的图标:`SquarePlus`→`IconPlus`(应 `IconSquarePlus`);`ArrowRightFromSquare`→`IconLogout`;`Persons`→`IconUsers` | 官方 `with-icons.tsx:15` 等;`@tabler/icons-vue` 已导出 `IconSquarePlus` | 部分已修 |
| B4 | `controlled-open-state` 多一个空格:`下拉菜单: 打开` | `dropdown-controlled-open-state.vue:17-20`;官方无空格 | 已修 |
| B5 | `single-with-custom-indicator` 把官方复用一个常量拆成 5 段内联 SVG 复制 | `dropdown-single-with-custom-indicator.vue:33-130`(136 行 vs 官方 71 行) | 已修 |
| B6 | `long-press-trigger` 多了一条官方没有的中文注释(演示即复制源) | `dropdown-long-press-trigger.vue:14` | 已修 |
| B7 | `with-multiple-selection` 用 `MenuKey[]` 取代 `Selection`/`Set`,且无 `"all"` 支持 | 已登记为适配;`Menu.vue:42-45` | 保留(已登记) |
| B8 | `custom-styles` 的 `data-[focused=true]` 死代码(见 A11) | 演示逐字抄官方,状态不发 | 已修 |
## C. 文档页 `apps/docs/components/dropdown.md`
| ID | 问题 | 证据 | 状态 |
| --- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------- | ---------------- |
| C1 | 组件结构示例不是官方骨架:缺 `DropdownTrigger`(照抄无法展开)、缺 `Separator`/`Section`+`Header`/`SubmenuTrigger`/`SubmenuIndicator`/`Description`/`Kbd slot="keyboard"`/`ItemIndicator` | `dropdown.md:29-40` vs 官方 `dropdown.mdx:23-59` | 已修 |
| C2 | 全局 CSS 示例自造:只给 2 个类、popover 用 `rounded-2xl bg-overlay shadow-lg`,与官方 4 类与实际 CSS 都不符 | `dropdown.md:114-123` vs 官方 `dropdown.mdx:164-182` | 已修 |
| C3 | 样式参考清单缩水并混入未实现状态(`data-entering`/`data-exiting`/`data-placement`/`data-hovered`/`data-pressed`/`data-disabled`),丢了官方分节与源码链接 | `dropdown.md:147-155` | 已修 |
| C4 | API 表不成体系:`DropdownSection` 只有一句话、`SubmenuIndicator`/`SubmenuTrigger` 无表、`ItemIndicator` 缺 render/slot 契约、整节 Render Props 缺失 | `dropdown.md:157-225` vs 官方 `dropdown.mdx:250-364` | 已修 |
| C5 | 改名列未对标:`isOpen`→`open`、`onOpenChange`→`update:open`、`placement`→`side`/`align`、`offset`→`sideOffset`、`crossOffset`→`alignOffset`、`containerPadding`→`collisionPadding`、`shouldFlip`→`avoidCollisions`、`onAction`→`action`、`onSelectionChange`→`selectionChange`、`shouldCloseOnSelect`→`closeOnSelect` | `dropdown.md:161-225` | 已修 |
| C6 | 自造项未标为扩展:`longPressDuration`、`closeOnSelect`、`modal`、`dir`、`loop`、Item 的 `isDisabled`、`select` 事件、`data-trigger`/`dropdown-root`/`dropdown-trigger-region`/`data-submenu-content` | `dropdown.md:163-213` | 已修 |
| C7 | 无障碍段改写,丢掉官方 5 条(键盘导航/播报/焦点/禁用/长按/子菜单),加了未验证的「隐藏/退出条目不进 Tab 顺序」 | `dropdown.md:227-229` vs 官方 `dropdown.mdx:365-376` | 已修 |
| C8 | 相关组件 4 条且顺序不同;官方 `slice(0,3)` = button/popover/separator | `dropdown.md:238-243`;`components-registry.ts:644` | 已修 |
| C9 | 顶部迁移注记与 `## 上游差异` 节为项目自加(确认是否作为统一约定保留) | `dropdown.md:8,231-236` | 保留(项目约定) |
## D. 核对一致、不需要改
- 17 个演示名称与顺序;全部 `id`/`textValue`/文案/`variant`/`aria-label`;`min-w-[256px]`、`ms-auto`、`size-3.5 text-muted`、`text-danger` 等工具类;`KbdAbbr` 键位映射与官方常量一致。
- 四个 CSS 文件是逐字移植(只差 `hv-` 前缀、注释与上游那条空的 `&:focus` 规则);`dropdownVariants`/`menuItemVariants` 与官方 1:1。
- 选择模型 `MenuKey[]`、事件名、具名导出为已登记的 Vue 适配。
## E. 无法核实
上游 checkout 未安装 `node_modules`,以下按官方文档表 + 上游 CSS 选择器反推,未做运行时验证:RAC 的 `data-focused`/每项 `data-selection-mode` 输出、`MenuSection` 的 `aria-labelledby` 实现方式、`trigger="longPress"` 的键盘语义、`MenuItemRenderProps` 是否含 `isIndeterminate`、`@gravity-ui/icons` 的默认属性。
## F. 本轮修复记录(2026-10-24)
同一分支 `feat/b01-menu-dropdown` 逐项修复,证据均为实渲染 DOM 与浏览器 5181 实测。
### 实现
- **A1**:`menu-item.css` 禁用态改为存在选择器 `&[data-disabled]`(Reka 写空串)。浏览器实测禁用行 `opacity: 0.5`、`cursor: not-allowed`,其余行 `opacity: 1`。
- **A2**:`DropdownPopover` 与子菜单内容接入共享 `useOverlayAnimation`,输出真实的 `data-entering`/`data-exiting`;`dropdown.css` 的 `[data-placement]` 改为 Reka 实际输出的 `[data-side]`。浏览器实测打开时 `data-entering="true"`、`animation: enter 0.15s`,动画结束后属性清除、`opacity: 1`;关闭时 `data-exiting="true"`、`animation: exit 0.1s`、`pointer-events: none`,随后节点卸载。
- **A3**:`Menu` 不再给内层列表加 `role="menu"`(Reka content 已是 menu)。实测每个弹层只有 1 个 `role="menu"`。
- **A4**:`MenuItem` 把 key 渲染为元素 `id`;`DropdownSubmenuTrigger` 在挂载/更新后把子菜单内容的 `aria-labelledby` 指回真实触发器(Reka 生成的 id 被用户 key 覆盖导致悬空)。实测子菜单 `aria-labelledby="share"` 可解析。
- **A6**:`DropdownTrigger` 在 `asChild` 目标不是原生可聚焦元素时补 `role="button"` 与 `tabindex="0"`。浏览器实测自定义触发器为 `<span role="button" tabindex="0" aria-haspopup="menu">`,Enter 可打开。
- **A7**:`dropdown.css` 增加 `.hv-dropdown__trigger.hv-button{display:inline-flex}` 与 `.hv-avatar{display:flex}` 兼容规则(与 Modal/Popover/Tooltip 既有做法一致)。实测纯图标触发器图标中心偏移从 `-12px` 变为 `0`。
- **A8**:`DropdownItem` 新增 `hasSubmenu` 显式下发,`DropdownSubmenuTrigger` 自动设置,不再依赖是否渲染指示器。新增回归:不带指示器的子菜单触发器点击不触发 action、不关闭根菜单。
- **A9**:`MenuSection` 提供标签注册上下文,`Header` 注册自身 id,分组输出 `aria-labelledby`。实测可解析到 `Header`。
- **A10**:`MenuItem` 默认插槽提供 `{ isSelected, isFocused, isDisabled, isPressed }`;`DropdownItemIndicator` 插槽提供 `{ isSelected, isIndeterminate, isFocused, isDisabled, isPressed }`。
- **A11**:条目输出 `data-focused`/`data-hovered`/`data-pressed`。浏览器实测 `custom-styles` 演示键盘聚焦行 `data-focused="true"` 且背景变为 `bg-default`,官方示例的 `data-[focused=true]` 不再是死代码。
- **A12**:`DropdownSubmenuTrigger` 把 `class`/ARIA 合并到触发条目;子菜单内 `DropdownPopover` 的 `class`/`data-*`/`aria-*` 继承到子菜单面板。实测 `hv-dropdown__popover nested-pop` 与 `data-x` 均落地。
- **A13**:`Menu`/`MenuItem`/`MenuItemIndicator`/`MenuItemSubmenuIndicator`/`MenuSection`/`DropdownTrigger`/`DropdownPopover` 的用户 class 改为经 tailwind-variants(含 twMerge)合并。
- **A14**:默认子菜单指示器改用官方 `IconChevronRight` 的路径(16 视框、实心),不再自绘 24 视框描边箭头。
- **A15/A16/A17**:独立 `Menu` 用 `data-slot="menu"`;删除 `MenuSection` 无效的 `as`;删除「只传 selectedKeys 就推断 multiple」的自造语义。
### 演示
- **B2**:6 个演示共 27 处 `Kbd slot="keyboard"` 全部还原(浏览器实测打开的菜单里有 4 个 `kbd[slot="keyboard"]`,类名为 `ms-auto hv-kbd hv-kbd--light`)。
- **B3**:`SquarePlus` 改用 `IconSquarePlus`(4 个演示)。
- **B4/B5/B6**:`controlled-open-state` 多余空格、`single-with-custom-indicator` 的 5 段重复 SVG(改为单一定义组件,136→82 行)、`long-press-trigger` 的自加中文注释。
### 文档
- **C1–C8**:`apps/docs/components/dropdown.md` 按官方 `dropdown.mdx` 分节重写:完整组件骨架(含 `DropdownTrigger`/`Separator`/`Section`+`Header`/`SubmenuTrigger`/`Description`/`Kbd slot="keyboard"`/`ItemIndicator`)、官方 4 个类的全局 CSS 示例、分节的样式参考(只列真实发出的状态)、每个部件一张 API 表并标注改名列与「Vue 扩展」、新增 Render Props 表、无障碍按官方条目、相关组件改为官方 3 项。
### 仍保留 / 未修
> 上表状态为忠实度审计当轮结果;`A5`(链接项)与 `A21` 中的多数能力已在随后的[整批复核返修](B01-complete-review.md)中补齐,以下为当前实际边界。
- **A5**:链接型菜单项已实现(`href` 渲染真实 `<a>`,保留 `target/rel` 等,禁用项阻止导航);剩余差异仅是 key 与 DOM id 分离。
- **A18**:子菜单悬停延迟由底层固定 100ms,未暴露官方 `delay`;子菜单面板 `side/align` 也由底层固定。
- **A19**:长按键盘用 Enter/Space(本批复核要求),与 RAC 文档的 Alt+↓ 不同;长按计时 500ms、10px 取消为自定实现。
- **A20**:`DropdownRoot` 保留 `.hv-dropdown` 包裹元素(承载根级 class/dir),官方该类没有 DOM 宿主。
- **A21**:`disallowEmptySelection`、`escapeKeyBehavior`、`shouldFocusWrap`、`autoFocus`、`close`、`renderEmptyState`(`empty` 插槽)、菜单级 `shouldCloseOnSelect`、`isKeyboardDismissDisabled`、`shouldCloseOnInteractOutside`、`placement` 系列已补齐;`items` 以 `v-for` 表达;Trigger/Popover 作用域插槽为官方 render props 的子集(`{isOpen}` / `{isEntering,isExiting,placement}`)。
- **B1/B7**:显式 `DropdownTrigger` 包裹触发器、`Selection`→`MenuKey[]`(含无 `"all"`)为既定 Vue API 适配,文档已登记。