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.
This commit is contained in:
maoyingjie
2026-10-24 22:56:44 +08:00
parent 043a5418f3
commit 53b4639b0e
25 changed files with 1721 additions and 203 deletions
+82 -54
View File
@@ -252,6 +252,8 @@ Dropdown 使用 Menu、MenuItem 与 MenuSection 作为底层组件。以下类
所有部件接受 `class`、`style`、ARIA 与原生事件(`DropdownSubmenuTrigger` 会把它们合并到它的条目元素上)。选择集合使用 Vue 数组模型(`MenuKey[]`),不使用 React `Set`/`Selection`。标记「Vue 扩展」的属性在官方 HeroUI 中没有对应项。
弹层默认通过 Portal 挂载到 `document.body`(可用 `to` 指定目标),层级为 `--z-index-overlay`,因此不会被祖先的 `overflow`、`contain` 或 `isolation` 裁切;面板高度受底层可用的浮动高度约束并自动滚动。
### DropdownRoot
| Prop | 类型 | 默认值 | 描述 |
@@ -264,7 +266,7 @@ Dropdown 使用 Menu、MenuItem 与 MenuSection 作为底层组件。以下类
| `dir` | `'ltr' \| 'rtl'` | — | **Vue 扩展**:文本方向 |
| `longPressDuration` | `number` | `500` | **Vue 扩展**:长按时长 ms;移动超过 10px 取消 |
长按模式下 Enter/Space 仍可键盘打开;`pointerup`/`pointercancel`/`pointerleave`/卸载会清理计时。
长按模式下 Enter/Space 仍可键盘打开,并且真实触发器上的 `keydown` 处理器先执行,可 `preventDefault()` 取消打开;`pointerup`/`pointercancel`/`pointerleave`/卸载会清理计时。
### DropdownTrigger
@@ -273,54 +275,74 @@ Dropdown 使用 Menu、MenuItem 与 MenuSection 作为底层组件。以下类
| `asChild` | `boolean` | `true` | 将触发合并到唯一子节点(默认行为) |
| `as` | `string` | — | 自定义触发元素标签 |
页面里的 `Button` props 写在内部的 `Button` 上。子节点若不是原生可聚焦元素(例如 Avatar),组件会补 `role="button"` 与 `tabindex="0"`,保持官方「触发器可键盘操作」的行为。
默认插槽会收到 `{ isOpen }`。页面里的 `Button` props 写在内部的 `Button` 上。子节点若不是原生可聚焦元素(例如 Avatar),组件会补 `role="button"` 与 `tabindex="0"`,保持官方「触发器可键盘操作」的行为。
### DropdownPopover
| Prop | 类型 | 默认值 | 描述 |
| ------------------ | --------- | ------ | ------------------------------------- |
| `side` / `align` | `string` | — | 相对触发器的位置。官方用 `placement` |
| `sideOffset` | `number` | — | 侧边偏移。官方名 `offset` |
| `alignOffset` | `number` | — | 交叉轴偏移。官方名 `crossOffset` |
| `avoidCollisions` | `boolean` | — | 碰撞翻转。官方名 `shouldFlip` |
| `collisionPadding` | `number` | — | 边界内边距。官方名 `containerPadding` |
| `loop` | `boolean` | — | **Vue 扩展**:焦点循环 |
| Prop | 类型 | 默认值 | 描述 |
| ------------------------------ | ----------------------- | ---------- | ---------------------------------------------------- |
| `placement` | `OverlayPlacement` | `'bottom'` | 相对于触发器的位置(复用本库 Popover 的取值)。 |
| `offset` | `number` | `0` | 与触发器的距离。 |
| `crossOffset` | `number` | `0` | 交叉轴偏移。 |
| `shouldFlip` | `boolean` | `true` | 空间不足时翻转。 |
| `containerPadding` | `number` | `12` | 与视口边缘保留的距离。 |
| `isKeyboardDismissDisabled` | `boolean` | `false` | 禁用 Escape 关闭。 |
| `shouldCloseOnInteractOutside` | `(event) => boolean` | — | 返回 `false` 时外部交互不关闭菜单。 |
| `to` | `string \| HTMLElement` | — | Portal 目标,默认 `document.body`。 |
| `side` / `align` | `string` | — | **底层扩展**:直接指定浮动方向,优先于 `placement`。 |
| `sideOffset` / `alignOffset` | `number` | — | **底层扩展**:覆盖 `offset` / `crossOffset`。 |
| `avoidCollisions` | `boolean` | — | **底层扩展**:覆盖 `shouldFlip`。 |
| `collisionPadding` | `number` | — | **底层扩展**:覆盖 `containerPadding`。 |
| `loop` | `boolean` | — | **底层扩展**:焦点循环。 |
默认插槽会收到 `{ isEntering, isExiting, placement }`。
### DropdownMenu
| Prop | 类型 | 默认值 | 描述 |
| ----------------------------------------- | ---------------------------------- | -------- | ------------------------------------------ |
| `selectionMode` | `'none' \| 'single' \| 'multiple'` | `'none'` | 是否启用单选、多选或不启用选择。 |
| `selectedKeys` | `MenuKey[]` | — | 当前选中的 key(受控)。 |
| `defaultSelectedKeys` | `MenuKey[]` | `[]` | 初始选中的 key(非受控)。 |
| `disabledKeys` | `MenuKey[]` | `[]` | 禁用项的 key。 |
| `update:selectedKeys` / `selectionChange` | 事件 `(keys: MenuKey[])` | — | 选中变化时触发。官方名 `onSelectionChange` |
| `action` | 事件 `(key: MenuKey)` | — | 激活菜单项时触发。官方名 `onAction` |
| Prop | 类型 | 默认值 | 描述 |
| ----------------------------------------- | ---------------------------------- | -------- | ---------------------------------------------------- |
| `selectionMode` | `'none' \| 'single' \| 'multiple'` | `'none'` | 是否启用单选、多选或不启用选择。 |
| `selectedKeys` | `MenuKey[]` | — | 当前选中的 key(受控)。 |
| `defaultSelectedKeys` | `MenuKey[]` | `[]` | 初始选中的 key(非受控)。 |
| `disabledKeys` | `MenuKey[]` | `[]` | 禁用项的 key。 |
| `disallowEmptySelection` | `boolean` | `false` | 禁止用户清空已有选择。 |
| `shouldCloseOnSelect` | `boolean \| (key) => boolean` | — | 菜单级关闭策略;默认动作项与单选关闭、多选保持打开。 |
| `shouldFocusWrap` | `boolean` | — | 键盘导航在首尾循环。 |
| `autoFocus` | `boolean` | `true` | 打开时聚焦菜单。 |
| `escapeKeyBehavior` | `'clearSelection' \| 'none'` | `'none'` | `clearSelection` 时 Escape 清空选择且不关闭菜单。 |
| `update:selectedKeys` / `selectionChange` | 事件 `(keys: MenuKey[])` | — | 选中变化时触发。官方名 `onSelectionChange` |
| `action` | 事件 `(key: MenuKey)` | — | 激活菜单项时触发。官方名 `onAction` |
| `close` | 事件 `()` | — | 该层菜单关闭时触发。官方名 `onClose` |
默认插槽渲染条目;`empty` 插槽在该菜单没有条目时渲染(对应官方 `renderEmptyState`)。数据驱动的 `items` 用 `v-for` 表达。
### DropdownSection
| Prop | 类型 | 默认值 | 描述 |
| ----------------------------------------- | ------------------------ | ------ | ------------------------------------------ |
| `selectionMode` | `'single' \| 'multiple'` | — | 该分组内菜单项的选择模式;省略则继承菜单。 |
| `selectedKeys` | `MenuKey[]` | — | 当前选中的 key(受控)。 |
| `defaultSelectedKeys` | `MenuKey[]` | `[]` | 初始选中的 key(非受控)。 |
| `disabledKeys` | `MenuKey[]` | `[]` | 禁用项的 key。 |
| `update:selectedKeys` / `selectionChange` | 事件 `(keys: MenuKey[])` | — | 选中变化时触发。官方名 `onSelectionChange` |
| Prop | 类型 | 默认值 | 描述 |
| ----------------------------------------- | ------------------------ | ------- | -------------------------------------------- |
| `selectionMode` | `'single' \| 'multiple'` | — | 该分组内菜单项的选择模式;省略则继承菜单。 |
| `selectedKeys` | `MenuKey[]` | — | 当前选中的 key(受控)。 |
| `defaultSelectedKeys` | `MenuKey[]` | `[]` | 初始选中的 key(非受控)。 |
| `disabledKeys` | `MenuKey[]` | `[]` | 禁用项的 key,与菜单级 `disabledKeys` 叠加。 |
| `disallowEmptySelection` | `boolean` | `false` | 禁止用户清空该分组的选择。 |
| `update:selectedKeys` / `selectionChange` | 事件 `(keys: MenuKey[])` | — | 选中变化时触发。官方名 `onSelectionChange` |
分组内嵌套的 `Header` 会自动成为分组的可访问名称来源(`aria-labelledby`)。
分组内嵌套的 `Header` 会自动成为分组的可访问名称来源(`aria-labelledby`)。分组内再嵌套的 `Menu` 会重置选择作用域,不会继承外层分组的 owner。
### DropdownItem
| Prop | 类型 | 默认值 | 描述 |
| --------------- | ----------------------- | ----------- | ---------------------------------------------------------------- |
| `id` | `MenuKey` | — | 菜单项唯一标识,同时作为元素 `id`。 |
| `textValue` | `string` | — | 用于首字母导航的文本内容。 |
| `variant` | `'default' \| 'danger'` | `'default'` | 菜单项视觉变体。 |
| `isDisabled` | `boolean` | `false` | 禁用该项。 |
| `closeOnSelect` | `boolean` | — | `false` 时动作项激活后保持菜单打开。官方名 `shouldCloseOnSelect` |
| `select` | 事件 `(key: MenuKey)` | — | **Vue 扩展**:条目激活回调 |
| Prop | 类型 | 默认值 | 描述 |
| ------------------------------------------------------------- | ----------------------- | ----------- | ------------------------------------------------------------------------- |
| `id` | `MenuKey` | — | 业务 key(选择与动作使用)。DOM id 为实例级,与业务 key 无关。 |
| `textValue` | `string` | — | 用于首字母导航的文本内容。 |
| `variant` | `'default' \| 'danger'` | `'default'` | 菜单项视觉变体。 |
| `isDisabled` | `boolean` | `false` | 禁用该项。 |
| `closeOnSelect` | `boolean` | — | 覆盖关闭策略;`false` 保持打开,`true` 关闭。官方名 `shouldCloseOnSelect` |
| `href` | `string` | — | 渲染为链接型菜单项(真实 `<a>`)。 |
| `target` / `rel` / `download` / `hrefLang` / `referrerPolicy` | — | — | 透传到链接元素。 |
| `select` | 事件 `(key: MenuKey)` | — | **Vue 扩展**:条目激活回调 |
默认插槽会收到 `{ isSelected, isFocused, isDisabled, isPressed }`,对应官方的 Render Props。
默认插槽会收到 `{ isSelected, isFocused, isDisabled, isPressed }`,对应官方的 Render Props。禁用链接不会导航;`href` 项的原生导航不受 `click` 处理器影响(除非调用 `preventDefault()`)。
### DropdownItemIndicator
@@ -338,19 +360,20 @@ Dropdown 使用 Menu、MenuItem 与 MenuSection 作为底层组件。以下类
### DropdownSubmenuIndicator
| 内容 | 说明 |
| ---- | ---------------------------------------- |
| 默认 | 官方内置的 chevron-right 图标 |
| 插槽 | 自定义指示器内容;只有带子菜单的项才渲染 |
| 内容 | 说明 |
| ---- | ------------------------------------ |
| 默认 | 官方内置的 chevron-right 图标 |
| 插槽 | 自定义指示器内容;带子菜单的项才渲染 |
### DropdownSubmenuTrigger
| Slots / Props | 说明 |
| ------------- | -------------------------------------------------- |
| 默认插槽 | 第一个子节点是触发条目,其余子节点渲染进子菜单内容 |
| `class`/ARIA | 合并到触发条目元素上 |
| Slots / Props | 说明 |
| ------------- | --------------------------------------------------------------------------------------------------------------------- |
| 默认插槽 | 第一个子节点是触发条目,其余子节点渲染进子菜单内容 |
| `class`/ARIA | 合并到触发条目元素上 |
| 面板参数 | 嵌套 `DropdownPopover` 的 `offset`/`crossOffset`/`shouldFlip`/`containerPadding`/`loop`/`to`/`class` 会传给子菜单面板 |
每层子菜单独立持有 open 状态;焦点在子菜单内按 Esc 只关闭当前层,焦点回到该层触发器。
每层子菜单独立持有 open 状态并各自 Portal;焦点在子菜单内按 Esc 只关闭当前层,焦点回到该层触发器。子菜单的 `side`/`align` 由底层固定为「贴着父项向右展开」(RTL 反向),只有偏移与碰撞参数可调。
### Render Props
@@ -363,27 +386,32 @@ Dropdown 使用 Menu、MenuItem 与 MenuSection 作为底层组件。以下类
| `isDisabled` | `boolean` | 该项是否禁用。 |
| `isPressed` | `boolean` | 该项是否处于按下状态。 |
### 独立 Menu
`Menu` 可以在 `DropdownPopover` 之外直接使用:渲染为静态内联列表(`data-slot="menu"`),保留 `role="menu"`、条目语义、键盘导航与选择;不会锁定页面滚动、不会拦截外部指针事件、不会抢焦点,也不做浮动定位。
## 无障碍
Dropdown 组件实现 ARIA 菜单模式,并提供:
- 完整键盘导航(方向键、Home/End、首字母导航)
- 屏幕阅读器对操作与选中变化的播报
- 合理的焦点管理(关闭后焦点回到触发器)
- 禁用态支持
- 完整键盘导航(方向键、Home/End、首字母导航,`shouldFocusWrap` 控制首尾循环)
- 屏幕阅读器对操作与选中变化的播报:`aria-label`/`aria-labelledby`/`aria-describedby` 落在真正的 `role="menu"` 节点上
- 合理的焦点管理(关闭后焦点回到触发器,Esc 逐层关闭)
- 禁用态支持(`[data-disabled]` + `aria-disabled`)
- 长按交互支持
- 子菜单导航,Esc 逐层关闭
- 子菜单导航
菜单使用 `role="menu"` / `menuitem`(选择项为 `menuitemradio` / `menuitemcheckbox`);禁用项跳过,不进入 Tab 顺序。
菜单使用 `role="menu"` / `menuitem`(选择项为 `menuitemradio` / `menuitemcheckbox`),每个弹层只有一个 `role="menu"`;禁用项跳过,不进入 Tab 顺序。
## 上游差异
- 部件导出为具名组件(`DropdownRoot` / `DropdownTrigger` / …),触发器需要显式包裹,官方是把 `Button` 直接放在 `Dropdown` 下。
- `Selection`/`Set` 改为 `MenuKey[]`;`onAction`/`onSelectionChange` 改为 Vue 事件;`isOpen`/`onOpenChange` 改为 `open` + `v-model:open`。
- `placement` 改为 `side`/`align`(Reka 定位模型),`offset`/`crossOffset`/`shouldFlip`/`containerPadding` 分别改为 `sideOffset`/`alignOffset`/`avoidCollisions`/`collisionPadding`。
- 官方支持的链接型菜单项(`href`/`target`)未实现:底层 Reka 菜单项没有锚点路径。
- 子菜单悬停展开延迟由底层固定为 100ms,未暴露官方 `delay`(默认 200ms)。
- `Selection`/`Set` 改为 `MenuKey[]`;`onAction`/`onSelectionChange`/`onClose` 改为 Vue 事件;`isOpen`/`onOpenChange` 改为 `open` + `v-model:open`。
- 官方 `items` + 渲染函数的数据驱动集合用 `v-for` 表达;`renderEmptyState` 对应 `empty` 插槽。不提供 React Collection 对象模型。
- 子菜单悬停展开延迟由底层固定为 100ms,未暴露官方 `delay`(默认 200ms);子菜单面板的 `side`/`align` 由底层固定,只有偏移与碰撞参数可调。
- `DropdownTrigger` 的作用域插槽只提供 `{ isOpen }`,`DropdownPopover` 只提供 `{ isEntering, isExiting, placement }`;官方 Button/Popover 的完整 render props 未全部覆盖。
- `DropdownRoot` 额外渲染一层 `.hv-dropdown` 包裹元素(官方该类没有 DOM 宿主),用于承载根级 class 与方向。
- 条目 `id` 是业务 key,DOM id 为实例级;官方把 key 直接作为 DOM id(Vue 端避免数字 `0` 与字符串 `"0"` 冲突)。
- 条目 `onClick` 中 `preventDefault()` 会取消动作、选择与关闭;`stopImmediatePropagation()` 会终止后续处理器,均为原生事件语义。
## 相关组件