53b4639b0e
- 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.
108 lines
18 KiB
Markdown
108 lines
18 KiB
Markdown
# 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"]`。
|
||
|
||
## 整批复核返修(2026-10-24)
|
||
|
||
按 [B01 整批复核与统一返修清单](B01-complete-review.md) 处理 R1–R12 与能力清单。新增探针 `tests/b01-complete-review.probe.test.ts` **13/13**、类型正例 `tests/package/b01-wrapper-props-review.ts` 通过。
|
||
|
||
| ID | 实现位置 | 测试 / 浏览器证据 | 文档 |
|
||
| --- | --------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------- |
|
||
| R1 | `DropdownPopover` / `DropdownSubmenuTrigger` 接入 `DropdownMenuPortal`(`to` 可配);`.hv-dropdown__popover` 增加 `z-index: var(--z-index-overlay)` | 单测:面板不在 `contain/isolation` 祖先内;浏览器:面板父节点为 popper 包装层且位于 body,rect 57–383(视口 709),首末项均可见,z-index 100000;Modal 内面板在 backdrop 之后挂载(DOM 顺序 4 > 3),Esc 先关菜单再关 Modal,关闭后 body 样式清空、焦点回触发器 | 「弹层默认通过 Portal 挂载…」段 + DropdownPopover `to` 行 |
|
||
| R2 | `Menu` 独立路径改为 `modal: false` + 非 Portal + `data-static` + 阻止 auto focus;`.hv-dropdown__popover[data-static]` 的包装层用静态定位 | 探针 + 单测;浏览器:内联菜单 `body` 无内联样式、包装层 `position: static`、焦点留在 body、`role=menuitem` | 「独立 Menu」小节 |
|
||
| R3 | `MenuSection` 的禁用列表与菜单级叠加;`Menu` 提供 `menuSectionContextKey = null` 重置作用域 | 探针两项 + 单项「Section 下新 Menu 不继承 owner」 | DropdownSection 表 + 「分组内再嵌套的 Menu 会重置选择作用域」 |
|
||
| R4 | `MenuItem.shouldClose()`:条目 `closeOnSelect` > 菜单 `shouldCloseOnSelect` > 按模式默认(多选保持打开) | 探针两项(single+false 保持、multiple+true 关闭) | DropdownItem `closeOnSelect` 与 DropdownMenu `shouldCloseOnSelect` 行 |
|
||
| R5 | `MenuItem` 用实例级 `useId()` 作 DOM id,业务 key 只进 `data-key`/选择;子菜单 `aria-labelledby` 直接指向本实例触发器 | 探针(0 与 "0" 不重复)+ 单测(id 唯一、`data-key` 保持业务 key) | DropdownItem `id` 行 + 上游差异条 |
|
||
| R6 | 长按键:Enter/Space 的用户 `keydown` 先执行(capture 链),内部在 `defaultPrevented` 时才跳过打开;基准触发器的 toggle 在本次事件内被挂起 | 探针「取消的 keydown 不打开」+ 单测「Enter/Space 只切换一次」 | DropdownRoot 长按段 |
|
||
| R7 | `MenuItem.href/target/rel/download/hrefLang/referrerPolicy` → 渲染真实 `<a>`;禁用项阻止导航 | 探针 + 单测(标签为 A、href/target 保留、action 仍触发、禁用时不导航) | DropdownItem 链接行 |
|
||
| R8 | `Menu` 把 `aria-label`/`aria-labelledby`/`aria-describedby` 通过内容通道写到真正的 `role="menu"` 节点 | 探针(`[role=menu]` 带 `File actions`)+ 浏览器(`水果操作`) | 无障碍段 + DropdownMenu 说明 |
|
||
| R9 | 子菜单面板接收嵌套 `DropdownPopover` 的 `offset/crossOffset/shouldFlip/containerPadding/loop/to/class/aria/事件`;用户 `keydown` 与内部 Escape 合并 | 探针(keydown 一次)+ 单测(panel class、keydown) | DropdownSubmenuTrigger「面板参数」行 |
|
||
| R10 | `DropdownPopover` 提供 `surfaceContextKey` 的 `default` 变体 | 探针(Surface secondary 下读回 default) | 无(行为与官方一致) |
|
||
| R11 | `origin` 改用 `--reka-dropdown-menu-content-transform-origin`;`max-height` 用 `--reka-dropdown-menu-content-available-height` | 浏览器:`transform-origin: 30px 0px`(真实浮动原点)、`max-height: 640px` 与 `overflow-y: auto` | 「面板高度受…约束」段 |
|
||
| R12 | `DropdownItem`/`DropdownMenu`/`DropdownSection` 补齐真实 props/emits,`DropdownSubmenuTriggerProps` 由组件推导 | `tests/package/b01-wrapper-props-review.ts` 在仓库与独立 tarball 中均通过(bundler + NodeNext) | 各部件 API 表 |
|
||
|
||
能力清单同步落地:`disallowEmptySelection`、`escapeKeyBehavior`、`shouldFocusWrap`、`autoFocus`、`shouldCloseOnSelect`(菜单级)、`close` 事件、`empty` 插槽、`isKeyboardDismissDisabled`、`shouldCloseOnInteractOutside`、`Trigger`/`Popover` 作用域插槽(`{isOpen}` / `{isEntering,isExiting,placement}`)、`placement/offset/crossOffset/shouldFlip/containerPadding`(复用本库 Popover 取值);`items` 用 `v-for`、`renderEmptyState` 用 `empty` 插槽表达;子菜单 `delay` 与子菜单 `side/align` 作为底座固定值写入「上游差异」。
|
||
|
||
独立浏览器消费:夹具随仓库提供于 `tests/package/b01-consumer-app/`(选择长菜单、三级子菜单、Modal 内菜单、独立 Menu、链接项),tarball 安装后 esbuild 打包 + 静态服务,上述 R1/R2/R8/R11 结论均来自该页面实测。`consume-b01-menu-dropdown.mjs` 仍只做导出/CSS 存在性检查,真实挂载以该夹具为准。
|