Files
heroui-vue/docs/migrations/B01-menu-dropdown.md
T
maoyingjie 043a5418f3 fix: align Dropdown/Menu with the HeroUI baseline after fidelity audit
- menu-item.css keys disabled styling off the presence attribute Reka emits.
- Dropdown surfaces drive the ported enter/exit animations and use data-side
  for placement-specific motion.
- One role=menu per popup; items render their key as DOM id; submenu surfaces
  point aria-labelledby at the real trigger; MenuSection names its group from a
  nested Header.
- Custom (asChild) triggers stay keyboard focusable; library Button/Avatar keep
  their own layout instead of the custom-trigger rules.
- Items emit data-focused/data-hovered/data-pressed, expose the upstream render
  props to slots, and merge caller classes through tailwind-variants.
- Submenu triggers declare hasSubmenu explicitly and forward class/ARIA; the
  default submenu indicator uses the baseline chevron artwork.
- Demos restore Kbd slot="keyboard" (27x), IconSquarePlus, and remove the added
  comment, stray space and duplicated indicator SVGs.
- Document the page after the official structure and API tables, including
  renamed props, Vue extensions and remaining gaps.
2026-10-24 22:14:10 +08:00

85 lines
9.2 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
日期:2026-10-24。独立 worktree `heroui-vue-b01`,分支 `feat/b01-menu-dropdown`,起点 `origin/main` `1c4ecc3`。固定上游 HeroUI 3.2.6 / `ce90ea3`。本批不要求 SSR/水合/Nuxt。
## 源公开契约
- 部件:Menu/MenuRoot、MenuItem/Indicator/SubmenuIndicator、MenuSection;DropdownRoot/Trigger/Popover/Menu/Section/Item/ItemIndicator/SubmenuIndicator/SubmenuTrigger。
- 变体:menuItem `variant: default|danger`;ItemIndicator `type: checkmark|dot`。
- 选择:`selectionMode: none|single|multiple`、`selectedKeys`/`onSelectionChange`、Section 级选择、`disabledKeys`、`onAction`。
- 样式:`menu.css` / `menu-item.css` / `menu-section.css` / `dropdown.css` + tv,hv- 前缀移植。
- 文档:中文 `dropdown.mdx` **17 预览**;Menu/Item/Section **无独立 Web 文档页**(native menu.mdx 属 heroui-native,不作为契约)。
## 实现映射
| 上游 | Vue |
| ------------------------ | ---------------------------------------------------------------------------------- |
| RAC MenuTrigger + Button | `DropdownRoot`(Reka `DropdownMenuRoot`)+ `DropdownTrigger` / 演示内 Button |
| RAC Popover + Menu | `DropdownPopover`(Reka `DropdownMenuContent`)+ `DropdownMenu`(选择/动作上下文) |
| RAC MenuItem | `MenuItem`(Reka `DropdownMenuItem`)+ 自有选择 owner |
| RAC MenuSection | `MenuSection`(Reka `DropdownMenuGroup`)+ 分组级选择 |
| SubmenuTrigger | `DropdownSubmenuTrigger`(Reka `DropdownMenuSub` + `SubTrigger`) |
| Selection Set | `MenuKey[]` 数组模型 + 受控/非受控 + 提案更新 |
选择所有权:Menu 级 owner;Section 提供 `selectionMode`/`selectedKeys` 时覆盖为分组 owner。动作项 `selectionMode=none` 时 `@action` + 默认关闭;multiple 保持展开;single 关闭。
## 预览清单(17)
default、with-icons、with-descriptions、with-disabled-items、with-sections、with-multiple-selection、controlled、controlled-open-state、with-single-selection、single-with-custom-indicator、with-section-level-selection、with-keyboard-shortcuts、with-submenus、with-custom-submenu-indicator、custom-trigger、long-press-trigger、custom-styles。
## 验证
- `tests/b01-menu-dropdown.test.ts`:动作单次触发、多选切换、分组级选择、禁用标记、指示器 checkmark、子菜单、长按(真实触发器 8 项)、Esc 逐层(焦点在子项 / 焦点在子触发器两条路径)。
- 完整 `pnpm test`、`typecheck`、独立产物见验收节。
- 浏览器重点:见 verification(子菜单/RTL/Esc/长按)。
## 上游差异
- 具名导出替代对象部件;`MenuKey[]` 替代 `Set`/`Selection`;Vue 事件替代 onAction/onSelectionChange。
- 长按触发由 `DropdownRoot trigger="longPress"` + `longPressDuration` 提供(Reka 无 longPress 触发模式),交互契约对齐上游演示:短点击不开、按住到阈值打开、释放不反向关闭、键盘备用。
- Item render props 使用作用域插槽状态,不机械搬 RAC render 函数。
- Reka 2.11 主入口导出 `DropdownMenu*` 而非独立 `MenuItem`/`MenuGroup`;本库 MenuItem 基于 `DropdownMenuItem`。
- Reka `MenuSubContent` 的 Escape 默认关闭整栈(与上游/APG 的逐层关闭不同),本族在子菜单层自行拦截实现逐层关闭,不改共享 overlay 内核。
## 未验证
完整读屏、物理触屏、Nuxt、逐像素、自动视觉对比。长按“按住到阈值”的计时路径只有 jsdom + 假时钟证据(本运行时浏览器后端无 press-and-hold 输入)。
## 集成增量
- `packages/vue/src/index.ts`:Menu/Dropdown 命名导出 + 变体/类型。
- `packages/styles`:menu*.css、dropdown.css、menu/dropdown variants。
- `apps/docs/.vitepress/theme/navigation.ts`:Dropdown 条目(collections)。
- `docs/compatibility.md` / `upstream.md`:B01 条目。
- `package.json` test:unit:`tests/b01-menu-dropdown.test.ts`。
## 首轮复核返修(2026-10-24)
按 [B01-review](./B01-review.md) 修复,`tests/b01-review.probe.test.ts` **4/4**;原测试增至 8 项全过。
1. **选择 ARIA/样式**:`menuitemradio`/`menuitemcheckbox` + `aria-checked` + `data-selected`;dot 指示器选中样式由源 CSS `[data-selected]` 路径命中。
2. **事件取消**:条目自行编排 click(用户 handler 先跑);`preventDefault` 取消动作/选择/关闭,并 `stopImmediatePropagation` 避免 Reka 二次激活。
3. **子菜单单节点**:`SubTrigger asChild` 合并进 Item,不再嵌套两个 `menuitem`。
4. **独立 Menu**:无外层 Reka 上下文时自带 `DropdownMenuRoot`+`Content`,公开 `Menu > MenuItem` 可挂载。
5. **长按**:`DropdownRoot trigger="longPress"` + `longPressDuration`(默认 500ms、10px 阈值、键盘备用、卸载清理);演示改用组件 API。
6. **类型**:包根导出 `*Props`;`tests/package/b01-menu-dropdown-contract.ts` NodeNext/bundler 正反例通过。
7. **独立消费**:`/tmp/heroui-b01-consumer` tarball 安装后 `consume-b01-menu-dropdown.mjs` ESM+CSS 通过。
## 第二轮复核返修(2026-10-24)
按 [B01-review-2](./B01-review-2.md) 修复;`tests/b01-review.probe.test.ts` **7/7**(上一轮 4 项保持通过,新增 3 项由失败转通过)。
1. **长按的触发权归真实触发器**:`DropdownRoot` 不再包裹整段内容,只提供 `triggerMode`/`longPressDuration`/`open`/`setOpen` 上下文;`DropdownTrigger` 在 `longPress` 模式自行渲染一层 `display: contents` 守卫,用 **capture 阶段**接管 pointer/click/keydown,并以 `event.preventDefault()` 触发 Reka 自带的 `if (event.defaultPrevented) return` 守卫取消其打开/切换。短点击不开、release 后的 click 不回关、Enter/Space 只切换一次都由真实 `DropdownTrigger > Button` 上的事件决定,不依赖晚到的冒泡拦截。回归覆盖阈值前后、移动阈值、`pointercancel`、非主键/非主指针、卸载清理。
2. **点击链交回 Vue DOM 事件层**:`MenuItem` 不再手工遍历用户处理器数组。用户的 `onClick`(函数或数组)与内部激活动作合并成一个 `onClick` 数组下发给同一元素,Reka 的 `handleSelect` 由 `onSelect` 里的 `preventDefault` 中和,因此 `preventDefault`、capture、`stopImmediatePropagation` 都是 Vue/浏览器原生语义;`stopImmediatePropagation` 后第二个处理器与激活都不再执行。
3. **Esc 逐层关闭**:`DropdownSubmenuTrigger` 自己持有子菜单 open,在子内容与子触发器上拦截 Escape(`preventDefault` + `stopPropagation`,只作用于本层,不改共享 overlay 内核,也不加全局 document 监听),关闭当前层并把握焦还给子触发器;父层保留,第二次 Escape 才关闭根菜单。焦点在子菜单项内(键盘)与焦点在子触发器上(指针打开)两条路径都有回归,浏览器实测一致。
4. **回归与产物**:原 `b01-menu-dropdown` 增至 **18/18**(两条旧长按测试原先只在 Root 包裹层派发事件,已改为操作真实触发器;另加键盘激活与键盘取消两项),两文件合计 **25/25**;完整 **71 文件 / 888 项**;typecheck、build:docs、Prettier、`git diff --check` 通过。独立 tarball 重新打包后 ESM+CSS 与 bundler/NodeNext 类型契约仍通过。
5. **浏览器(5181,真实点击/按键)**:长按示例真实短点击后不打开;真实 Enter 打开且不被二次切换关闭;Space 一次开一次关;Esc 关闭后焦点回触发器。`with-submenus` 焦点在子菜单项上按 Esc → 只关子菜单、根菜单保持、焦点回「其他」,再按 Esc → 关根菜单、焦点回「分享」,console warn/error 为 0。
**仍存限制(诚实记录)**:本运行时浏览器后端没有 press-and-hold 输入,“按住达到阈值才打开”的计时路径只有 jsdom + 假时钟证据;完整读屏、物理触屏、Nuxt、逐像素与自动视觉对比未验证。
## 忠实度审计(2026-10-24)
按官方 `dropdown.mdx` 与 17 个官方演示逐条对照,问题清单、证据与逐项状态见 [B01 忠实度审计](B01-fidelity-audit.md)。本轮修掉 25 项(禁用态、进出动画、单 `role="menu"`、条目 id 与子菜单 `aria-labelledby`、自定义触发器可聚焦、触发器 inline-flex 冲突、`hasSubmenu` 来源、Section 标题关联、Render Props、`data-focused` 等交互状态、class 合并、子菜单指示器图标、演示 27 处 `Kbd slot="keyboard"`、官方图标与文档页重写),并保留 6 项须记录的差异:链接型菜单项不支持、子菜单悬停延迟固定 100ms、无 `disallowEmptySelection` 等官方 props、长按键盘语义、`DropdownRoot` 包裹元素、`Selection`→`MenuKey[]` 适配。
回归:`tests/b01-menu-dropdown.test.ts` **27/27**、`tests/b01-review.probe.test.ts` **7/7**,完整 **71 文件 / 897 项**;typecheck、build:docs、Prettier 通过;浏览器 5181 实测禁用态、进出动画、图标居中、自定义触发器键盘打开、`data-focused` 背景、单 `role="menu"` 与 `kbd[slot="keyboard"]`。