fix: resolve close policy, handler merging and submenu panel props
- Menu forwards the raw close policy and the item resolves item > menu (boolean or function) > per-mode default, so action items close by default and an explicit single/multiple policy is no longer pre-empted. - Root popover chains caller handlers with the internal ones for keydown, escape, interact-outside, auto focus and animation end; callers run first and a prevented event stops the internal logic. - Submenu panels receive the nested popover's data/style/id/extra events, honour isKeyboardDismissDisabled without closing the parent level or a modal, and forward shouldCloseOnInteractOutside. - A standalone Menu provides its own content-attribute channel so the accessible name/description land on the real role=menu node.
This commit is contained in:
@@ -295,7 +295,7 @@ Dropdown 使用 Menu、MenuItem 与 MenuSection 作为底层组件。以下类
|
|||||||
| `collisionPadding` | `number` | — | **底层扩展**:覆盖 `containerPadding`。 |
|
| `collisionPadding` | `number` | — | **底层扩展**:覆盖 `containerPadding`。 |
|
||||||
| `loop` | `boolean` | — | **底层扩展**:焦点循环。 |
|
| `loop` | `boolean` | — | **底层扩展**:焦点循环。 |
|
||||||
|
|
||||||
默认插槽会收到 `{ isEntering, isExiting, placement }`。
|
默认插槽会收到 `{ isEntering, isExiting, placement }`。`onKeydown`/`onEscapeKeyDown`/`onInteractOutside`/`onOpenAutoFocus`/`onAnimationend` 与调用方处理器按 Vue 数组语义合并,调用方先执行,`preventDefault()` 后内部逻辑不再动作。
|
||||||
|
|
||||||
### DropdownMenu
|
### DropdownMenu
|
||||||
|
|
||||||
@@ -367,11 +367,11 @@ Dropdown 使用 Menu、MenuItem 与 MenuSection 作为底层组件。以下类
|
|||||||
|
|
||||||
### DropdownSubmenuTrigger
|
### DropdownSubmenuTrigger
|
||||||
|
|
||||||
| Slots / Props | 说明 |
|
| Slots / Props | 说明 |
|
||||||
| ------------- | --------------------------------------------------------------------------------------------------------------------- |
|
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
| 默认插槽 | 第一个子节点是触发条目,其余子节点渲染进子菜单内容 |
|
| 默认插槽 | 第一个子节点是触发条目,其余子节点渲染进子菜单内容 |
|
||||||
| `class`/ARIA | 合并到触发条目元素上 |
|
| `class`/ARIA | 合并到触发条目元素上 |
|
||||||
| 面板参数 | 嵌套 `DropdownPopover` 的 `offset`/`crossOffset`/`shouldFlip`/`containerPadding`/`loop`/`to`/`class` 会传给子菜单面板 |
|
| 面板参数 | 嵌套 `DropdownPopover` 的 `offset`/`crossOffset`/`shouldFlip`/`containerPadding`/`loop`/`to`、`class`/`style`/`id`/`data-*`/ARIA 与事件都会传给子菜单面板;`isKeyboardDismissDisabled` 为真时 Esc 既不关子层也不关父层,`shouldCloseOnInteractOutside` 返回 `false` 时外部交互不关闭 |
|
||||||
|
|
||||||
每层子菜单独立持有 open 状态并各自 Portal;焦点在子菜单内按 Esc 只关闭当前层,焦点回到该层触发器。子菜单的 `side`/`align` 由底层固定为「贴着父项向右展开」(RTL 反向),只有偏移与碰撞参数可调。
|
每层子菜单独立持有 open 状态并各自 Portal;焦点在子菜单内按 Esc 只关闭当前层,焦点回到该层触发器。子菜单的 `side`/`align` 由底层固定为「贴着父项向右展开」(RTL 反向),只有偏移与碰撞参数可调。
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
# B01 整批清单返修复核(53b4639)
|
||||||
|
|
||||||
|
2026-10-24;独立worktree heroui-vue-b01,检查提交53b4639。结论:暂不通过。范围仍是上一份整批清单R1–R12,本轮剩余为R4/R6/R8/R9,不扩展新组件或SSR要求。本轮只增加回归与复核记录,未修改实现、未合并。
|
||||||
|
|
||||||
|
## 已完成及证据
|
||||||
|
|
||||||
|
- 上一份13项整批探针全部通过;真实组件$props正例和类型检查通过。
|
||||||
|
- 浏览器5181重验分组长菜单:已Portal脱离preview,面板z-index=100000,top=12px、height=406.98px,可用高度约束生效,transform-origin=128px 406.98px;顶部内容可命中,不再被前一代码区或导航遮挡。证据截图 `/private/tmp/b01-layering-fixed-review.png`。
|
||||||
|
- Portal后的Modal组合:子菜单Esc→只剩根菜单,焦点回子触发器;再Esc→Modal保留,焦点回模态内触发器;再Esc→Modal关闭,body overflow/pointer-events均恢复空值,焦点回“打开组合复核”。临时源夹具已从docs移除。
|
||||||
|
- 分组禁用与作用域、typed key DOM id、链接元素、Surface重置、包装组件类型等原探针均通过。
|
||||||
|
|
||||||
|
## 剩余4类 / 7项失败(本轮完整清单)
|
||||||
|
|
||||||
|
### R4:关闭策略仍有3条错误
|
||||||
|
|
||||||
|
1. **[P1] 默认动作项执行后不关闭。** `Menu.vue:153` 把默认shouldCloseOnSelect算成 `selectionMode === 'multiple'`。none动作模式得到false,导致默认示例“新建文件”点击后仍展开。浏览器实测根trigger aria-expanded=true、menu仍在;新增回归也失败。
|
||||||
|
2. **[P1] single菜单的shouldCloseOnSelect=false被忽略。** Item.shouldClose只处理函数策略,选择模式直接按single返回true。
|
||||||
|
3. **[P1] multiple菜单的shouldCloseOnSelect=true被忽略。** 同一逻辑直接返回false。
|
||||||
|
|
||||||
|
明确统一优先级:条目显式策略→菜单显式布尔/函数策略→按实际条目模式的默认策略(动作和单选关闭,多选保持)。不要把未设置的策略提前变成错误布尔。Section级模式也要遵循该优先级。保留原条目closeOnSelect两个已通过回归。
|
||||||
|
|
||||||
|
### R6:根Popover覆盖用户原生事件(1项)
|
||||||
|
|
||||||
|
**[P2] DropdownPopover onKeydown完全不调用。** Root面板`...attrs`之后直接写onKeydown:onSurfaceKeydown,覆盖用户处理器。对非Escape的a键也不调用(新回归失败)。同位置的onOpenAutoFocus/onEscapeKeyDown/onInteractOutside/onAnimationend存在相同覆盖写法;按原R6要求统一合并并尊重取消,不只特判这一个a键。用户回调先执行,内部逻辑检查defaultPrevented,Vue处理器数组语义保留。
|
||||||
|
|
||||||
|
### R9:子菜单面板参数丢失(2项)
|
||||||
|
|
||||||
|
1. **[P2] 嵌套Popover的data/style/id/其他事件仍丢失。** inheritedProps虽采集了这些项,渲染SubContent只spread手工构造的panelProps,未将它们应用;data-custom和style探针失败。仅class与keydown修通不能标整个R9完成。
|
||||||
|
2. **[P1] 子面板isKeyboardDismissDisabled=true仍被Esc关闭。** 参数虽在PANEL_PROPS清单里,却没用于子层handleEscape或底层dismiss守卫(新回归失败)。shouldCloseOnInteractOutside同样被采集但未调用;应统一补齐原R9公开参数的真实归属及取消链路。阻止当前层Esc时不能再误关父层/Modal。
|
||||||
|
|
||||||
|
已明确记录为底层限制的子菜单side/align、delay不在本轮重新扩大要求;本轮要求兑现的是已宣称支持的参数和原清单中的属性透传。
|
||||||
|
|
||||||
|
### R8:独立Menu仍丢失可访问名称(1项)
|
||||||
|
|
||||||
|
**[P2] Menu aria-label不在真实role=menu上。** 独立路径没有menuContentAttrsKey上游提供者,contentAttrs=null;listProps又把aria-label/labelledby/describedby删掉,最终真正Content也只有undefined。`Menu aria-label='Standalone actions'`回归失败。为独立容器和Dropdown容器分别处理真实语义节点的名称/描述,不重新制造双menu角色。保留已修正的静态布局及无滚动锁行为。
|
||||||
|
|
||||||
|
## 本轮命令结果
|
||||||
|
|
||||||
|
- 扩充 `tests/b01-complete-review.probe.test.ts`:20项,原13项通过,新7项失败;断言覆盖以上全部失败路径。
|
||||||
|
- `pnpm test`:72文件,71通过/1失败;924项,917通过/7失败,全部失败位于本次扩充的整批回归文件。
|
||||||
|
- `pnpm typecheck`通过(包含真实包装组件Props消费正例);`git diff --check`通过。
|
||||||
|
- 不新增SSR/水合/Nuxt验收,不把未重做的完整读屏/触屏/逐像素当作通过。
|
||||||
|
|
||||||
|
修复后按原整批清单复验受影响的默认/显式关闭策略、根/子事件及参数、独立Menu名称,并保持层级/Portal/Modal已通过路径。继续当前任务分支,交回复核,不合main、不推送。
|
||||||
@@ -67,3 +67,7 @@
|
|||||||
新回归加入test:unit显式入口(pnpm test会自动发现);保留本次13项行为和3项类型正例,不删断言/改成接受错误。逐项填R1–R12“实现位置→测试/浏览器证据→文档差异”;同时闭环上节全部能力清单。仅重点浏览器验遮挡/长菜单/子菜单/外部点击/嵌套关闭/长按/焦点/浅深色与RTL,不机械点击所有演示。
|
新回归加入test:unit显式入口(pnpm test会自动发现);保留本次13项行为和3项类型正例,不删断言/改成接受错误。逐项填R1–R12“实现位置→测试/浏览器证据→文档差异”;同时闭环上节全部能力清单。仅重点浏览器验遮挡/长菜单/子菜单/外部点击/嵌套关闭/长按/焦点/浅深色与RTL,不机械点击所有演示。
|
||||||
|
|
||||||
集成时不要覆盖其他人的Toast/Table、文档壳层及新批次清单;继续B01独立分支返修,不自行合main/推送,不提前开下一批。未验证的读屏/物理触屏/逐像素明确保留;SSR/水合/Nuxt不作为当前门槛。
|
集成时不要覆盖其他人的Toast/Table、文档壳层及新批次清单;继续B01独立分支返修,不自行合main/推送,不提前开下一批。未验证的读屏/物理触屏/逐像素明确保留;SSR/水合/Nuxt不作为当前门槛。
|
||||||
|
|
||||||
|
## 53b4639返修复核入口
|
||||||
|
|
||||||
|
上一份13项探针已通过,Portal遮挡和Modal逐层关闭已在浏览器重验通过;仍有R4/R6/R8/R9未闭环,具体7项回归与证据以 [B01-complete-review-2.md](./B01-complete-review-2.md) 为本轮当前结果。不得仅以原13项通过标记整批通过。
|
||||||
|
|||||||
@@ -105,3 +105,16 @@ default、with-icons、with-descriptions、with-disabled-items、with-sections
|
|||||||
能力清单同步落地:`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` 作为底座固定值写入「上游差异」。
|
能力清单同步落地:`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 存在性检查,真实挂载以该夹具为准。
|
独立浏览器消费:夹具随仓库提供于 `tests/package/b01-consumer-app/`(选择长菜单、三级子菜单、Modal 内菜单、独立 Menu、链接项),tarball 安装后 esbuild 打包 + 静态服务,上述 R1/R2/R8/R11 结论均来自该页面实测。`consume-b01-menu-dropdown.mjs` 仍只做导出/CSS 存在性检查,真实挂载以该夹具为准。
|
||||||
|
|
||||||
|
## 整批清单二次返修(2026-10-24)
|
||||||
|
|
||||||
|
按 [B01 整批清单返修复核](B01-complete-review-2.md) 收口剩余 4 类 7 项,探针扩至 **20/20**。
|
||||||
|
|
||||||
|
| ID | 修复 | 证据 |
|
||||||
|
| --- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
|
| R4 | `Menu` 只暴露菜单级原始策略(未设置为 `undefined`),`MenuItem.shouldClose()` 按「条目 → 菜单布尔/函数 → 按条目模式默认(动作/单选关闭、多选保持)」解析 | 探针三项(默认动作关闭、single+false、multiple+true);浏览器复验默认动作点击后 `aria-expanded=false`;新增 Section 模式优先级回归 |
|
||||||
|
| R6 | 根面板 `onKeydown`/`onEscapeKeyDown`/`onInteractOutside`/`onOpenAutoFocus`/`onAnimationend` 统一为「调用方 + 内部」数组链,调用方先执行,`defaultPrevented` 后内部跳过 | 探针「root popover preserves a user keydown callback」;新增四类事件与取消 Escape 回归 |
|
||||||
|
| R9 | 子面板透传 `data-*`/`style`/`id`/其他事件;`isKeyboardDismissDisabled` 阻止本层与父层/Modal 关闭;`shouldCloseOnInteractOutside` 接到子面板 outside 链路 | 探针两项(data/style 保留、Esc 不关闭);新增「禁止 Esc 时父层仍开」回归 |
|
||||||
|
| R8 | 独立 `Menu` 自持内容属性通道,名称/描述落到真正的 `role="menu"` 节点 | 探针「standalone Menu keeps its own accessible name」 |
|
||||||
|
|
||||||
|
回归:**72 文件 / 928 项**通过;typecheck、Prettier、`git diff --check` 通过;独立 tarball 消费重跑通过;浏览器复验默认动作关闭、三级子菜单逐层 Esc 与 Portal/Modal 既有路径。
|
||||||
|
|||||||
@@ -961,3 +961,13 @@ Button/PopoverTrigger/Link/ToggleButtonGroup 源码及几何复核后,移除
|
|||||||
- **能力清单**:`disallowEmptySelection`、`escapeKeyBehavior`、`shouldFocusWrap`、`autoFocus`、菜单级 `shouldCloseOnSelect`、`close` 事件、`empty` 插槽、`isKeyboardDismissDisabled`、`shouldCloseOnInteractOutside`、`placement/offset/crossOffset/shouldFlip/containerPadding`、Trigger/Popover 作用域插槽已提供;`items` 以 `v-for`、`renderEmptyState` 以 `empty` 插槽表达,子菜单 `delay` 与 `side/align` 作为底座固定值记录在页面「上游差异」。
|
- **能力清单**:`disallowEmptySelection`、`escapeKeyBehavior`、`shouldFocusWrap`、`autoFocus`、菜单级 `shouldCloseOnSelect`、`close` 事件、`empty` 插槽、`isKeyboardDismissDisabled`、`shouldCloseOnInteractOutside`、`placement/offset/crossOffset/shouldFlip/containerPadding`、Trigger/Popover 作用域插槽已提供;`items` 以 `v-for`、`renderEmptyState` 以 `empty` 插槽表达,子菜单 `delay` 与 `side/align` 作为底座固定值记录在页面「上游差异」。
|
||||||
- **独立浏览器消费**:`tests/package/b01-consumer-app/` 夹具用 tarball 安装后 esbuild 打包、静态服务,实测分组级多选、三级子菜单、链接项、Modal 内菜单(Esc 先关菜单再关 Modal,body 样式清空、焦点回触发器)与独立 Menu。控制台仅有 Vue feature flag 提示,无组件告警或错误。
|
- **独立浏览器消费**:`tests/package/b01-consumer-app/` 夹具用 tarball 安装后 esbuild 打包、静态服务,实测分组级多选、三级子菜单、链接项、Modal 内菜单(Esc 先关菜单再关 Modal,body 样式清空、焦点回触发器)与独立 Menu。控制台仅有 Vue feature flag 提示,无组件告警或错误。
|
||||||
- 回归:**72 文件 / 917 项**通过;typecheck、build:docs、Prettier 通过。仍未验证:完整读屏、物理触屏、逐像素对比、Nuxt/SSR(本批不要求)。
|
- 回归:**72 文件 / 917 项**通过;typecheck、build:docs、Prettier 通过。仍未验证:完整读屏、物理触屏、逐像素对比、Nuxt/SSR(本批不要求)。
|
||||||
|
|
||||||
|
### B01 整批清单二次返修(同日)
|
||||||
|
|
||||||
|
按 [整批清单返修复核](migrations/B01-complete-review-2.md) 处理剩余 R4/R6/R8/R9 共 7 项;`tests/b01-complete-review.probe.test.ts` 由 13 扩到 **20/20** 全通过。
|
||||||
|
|
||||||
|
- **R4 关闭策略**:`Menu` 只转发菜单级原始策略,`MenuItem` 按「条目显式 → 菜单显式布尔/函数 → 按条目模式默认(动作/单选关闭、多选保持)」解析。默认动作项实测点击后关闭(浏览器 `aria-expanded=false`、菜单消失);single+`false` 与 multiple+`true` 两项通过;Section 级模式同样遵循该优先级。
|
||||||
|
- **R6 根面板事件合并**:`onKeydown`/`onEscapeKeyDown`/`onInteractOutside`/`onOpenAutoFocus`/`onAnimationend` 改为「调用方处理器 + 内部处理器」的数组链,调用方先执行,`defaultPrevented` 后内部不再动作。新增回归覆盖四类事件各调用一次、以及调用方取消 Escape 后 `clearSelection` 不再执行。
|
||||||
|
- **R9 子菜单参数**:面板属性补齐 `data-*`/`style`/`id`/其他事件透传;`isKeyboardDismissDisabled` 生效(Esc 既不关子层也不关父层,新增回归),`shouldCloseOnInteractOutside` 接到子面板的 outside 链路。
|
||||||
|
- **R8 独立 Menu 名称**:独立路径自身提供内容属性通道,`aria-label`/`aria-labelledby`/`aria-describedby` 落在真正的 `role="menu"` 节点(回归 `Standalone actions` 通过),静态布局与无滚动锁保持不变。
|
||||||
|
- 回归:**72 文件 / 928 项**通过(含新增 4 项自有回归:根面板事件合并、调用方取消 Escape、子面板禁止 Esc、菜单策略优先于分组模式);typecheck、Prettier 通过;独立 tarball 消费重跑通过;浏览器复验默认动作关闭与三级子菜单逐层 Esc。
|
||||||
|
|||||||
@@ -118,7 +118,20 @@ export default defineComponent({
|
|||||||
const resolvedSide = computed(() => props.side ?? position.value.side);
|
const resolvedSide = computed(() => props.side ?? position.value.side);
|
||||||
const resolvedAlign = computed(() => props.align ?? position.value.align);
|
const resolvedAlign = computed(() => props.align ?? position.value.align);
|
||||||
|
|
||||||
|
type Handler<Ev> =
|
||||||
|
((event: Ev) => void) | ((event: Ev) => void)[] | undefined;
|
||||||
|
/** Caller handlers first, then the internal one: Vue array semantics. */
|
||||||
|
function chain<Ev extends Event>(
|
||||||
|
name: string,
|
||||||
|
own: (event: Ev) => void,
|
||||||
|
): Array<(event: Ev) => void> {
|
||||||
|
const user = (attrs as Record<string, Handler<Ev>>)[name];
|
||||||
|
const list = Array.isArray(user) ? user : user ? [user] : [];
|
||||||
|
return [...list, own];
|
||||||
|
}
|
||||||
|
|
||||||
function escape(event: KeyboardEvent) {
|
function escape(event: KeyboardEvent) {
|
||||||
|
if (event.defaultPrevented) return;
|
||||||
if (props.isKeyboardDismissDisabled) event.preventDefault();
|
if (props.isKeyboardDismissDisabled) event.preventDefault();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@@ -207,14 +220,24 @@ export default defineComponent({
|
|||||||
collisionPadding:
|
collisionPadding:
|
||||||
props.collisionPadding ?? props.containerPadding,
|
props.collisionPadding ?? props.containerPadding,
|
||||||
loop: props.loop ?? contentAttrs?.loop.value,
|
loop: props.loop ?? contentAttrs?.loop.value,
|
||||||
onAnimationend: onAnimationEnd,
|
onAnimationend: chain<AnimationEvent>(
|
||||||
onOpenAutoFocus: (event: Event) => {
|
"onAnimationend",
|
||||||
|
onAnimationEnd,
|
||||||
|
),
|
||||||
|
onOpenAutoFocus: chain<Event>("onOpenAutoFocus", (event) => {
|
||||||
|
if (event.defaultPrevented) return;
|
||||||
if (contentAttrs?.autoFocus.value === false)
|
if (contentAttrs?.autoFocus.value === false)
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
},
|
}),
|
||||||
onEscapeKeyDown: escape,
|
onEscapeKeyDown: chain<KeyboardEvent>(
|
||||||
onInteractOutside: interactOutside,
|
"onEscapeKeyDown",
|
||||||
onKeydown: onSurfaceKeydown,
|
escape,
|
||||||
|
),
|
||||||
|
onInteractOutside: chain<Event>(
|
||||||
|
"onInteractOutside",
|
||||||
|
interactOutside as (event: Event) => void,
|
||||||
|
),
|
||||||
|
onKeydown: chain<KeyboardEvent>("onKeydown", onSurfaceKeydown),
|
||||||
onVnodeMounted: (vnode) => {
|
onVnodeMounted: (vnode) => {
|
||||||
contentElement.value = menuSurfaceFromVNode(vnode);
|
contentElement.value = menuSurfaceFromVNode(vnode);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -36,7 +36,8 @@ import { useConfig } from "../../providers/context";
|
|||||||
import { elementFromValue, menuSurfaceFromVNode } from "./dom";
|
import { elementFromValue, menuSurfaceFromVNode } from "./dom";
|
||||||
import DropdownPopover from "./DropdownPopover.vue";
|
import DropdownPopover from "./DropdownPopover.vue";
|
||||||
|
|
||||||
const PANEL_PROPS = [
|
/** Panel props that are translated instead of spread onto the element. */
|
||||||
|
const CONTROL_PROPS = new Set([
|
||||||
"placement",
|
"placement",
|
||||||
"offset",
|
"offset",
|
||||||
"crossOffset",
|
"crossOffset",
|
||||||
@@ -52,7 +53,10 @@ const PANEL_PROPS = [
|
|||||||
"to",
|
"to",
|
||||||
"isKeyboardDismissDisabled",
|
"isKeyboardDismissDisabled",
|
||||||
"shouldCloseOnInteractOutside",
|
"shouldCloseOnInteractOutside",
|
||||||
] as const;
|
"class",
|
||||||
|
"onKeydown",
|
||||||
|
"onInteractOutside",
|
||||||
|
]);
|
||||||
|
|
||||||
type Handler =
|
type Handler =
|
||||||
((event: Event) => void) | ((event: Event) => void)[] | undefined;
|
((event: Event) => void) | ((event: Event) => void)[] | undefined;
|
||||||
@@ -84,7 +88,7 @@ function inheritedProps(props: Record<string, unknown> | null) {
|
|||||||
key.startsWith("data-") ||
|
key.startsWith("data-") ||
|
||||||
key.startsWith("aria-") ||
|
key.startsWith("aria-") ||
|
||||||
/^on[A-Z]/.test(key) ||
|
/^on[A-Z]/.test(key) ||
|
||||||
(PANEL_PROPS as readonly string[]).includes(key)
|
CONTROL_PROPS.has(key)
|
||||||
)
|
)
|
||||||
out[key] = value;
|
out[key] = value;
|
||||||
}
|
}
|
||||||
@@ -127,6 +131,12 @@ export default defineComponent({
|
|||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Panel configuration for the current render; event handlers read it later.
|
||||||
|
const panelConfig: {
|
||||||
|
dismissDisabled: boolean;
|
||||||
|
shouldCloseOutside?: (event: Event) => boolean;
|
||||||
|
} = { dismissDisabled: false };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reka labels the submenu surface with the id it generates for its own
|
* Reka labels the submenu surface with the id it generates for its own
|
||||||
* trigger, but our trigger item keeps an instance id, so that reference
|
* trigger, but our trigger item keeps an instance id, so that reference
|
||||||
@@ -154,15 +164,19 @@ export default defineComponent({
|
|||||||
|
|
||||||
function handleEscape(event: KeyboardEvent) {
|
function handleEscape(event: KeyboardEvent) {
|
||||||
if (event.defaultPrevented) return;
|
if (event.defaultPrevented) return;
|
||||||
if (
|
if (event.key !== "Escape") return;
|
||||||
event.key === "Escape" &&
|
if (panelConfig.dismissDisabled) {
|
||||||
contentAttrs.escapeKeyBehavior.value === "clearSelection"
|
// Keep this level, the parent menu and any modal open.
|
||||||
) {
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (contentAttrs.escapeKeyBehavior.value === "clearSelection") {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
contentAttrs.clearSelection();
|
contentAttrs.clearSelection();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (event.key !== "Escape" || !subOpen.value) return;
|
if (!subOpen.value) return;
|
||||||
const target = event.target as HTMLElement | null;
|
const target = event.target as HTMLElement | null;
|
||||||
// Only the menu level that owns the focused element closes.
|
// Only the menu level that owns the focused element closes.
|
||||||
if (!target?.closest?.("[role='menu']")) return;
|
if (!target?.closest?.("[role='menu']")) return;
|
||||||
@@ -175,6 +189,15 @@ export default defineComponent({
|
|||||||
elementFromValue(triggerRef.value)?.focus();
|
elementFromValue(triggerRef.value)?.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleInteractOutside(event: {
|
||||||
|
defaultPrevented?: boolean;
|
||||||
|
preventDefault?: () => void;
|
||||||
|
}) {
|
||||||
|
if (event.defaultPrevented) return;
|
||||||
|
if (panelConfig.shouldCloseOutside?.(event as unknown as Event) === false)
|
||||||
|
event.preventDefault?.();
|
||||||
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
const children = flatten(slots.default?.() ?? []);
|
const children = flatten(slots.default?.() ?? []);
|
||||||
const triggerVNode = children[0] ?? null;
|
const triggerVNode = children[0] ?? null;
|
||||||
@@ -187,40 +210,51 @@ export default defineComponent({
|
|||||||
? (popoverVNode.props as Record<string, unknown> | null)
|
? (popoverVNode.props as Record<string, unknown> | null)
|
||||||
: null,
|
: null,
|
||||||
);
|
);
|
||||||
const {
|
const domProps: Record<string, unknown> = {};
|
||||||
class: popoverClass,
|
for (const [key, value] of Object.entries(popoverProps)) {
|
||||||
onKeydown: popoverKeydown,
|
if (!CONTROL_PROPS.has(key)) domProps[key] = value;
|
||||||
...restProps
|
}
|
||||||
} = popoverProps;
|
|
||||||
|
|
||||||
const placement = (restProps.placement as OverlayPlacement) ?? "end";
|
const placement = (popoverProps.placement as OverlayPlacement) ?? "end";
|
||||||
const position = resolvePlacement(placement, config?.dir.value ?? "ltr");
|
const position = resolvePlacement(placement, config?.dir.value ?? "ltr");
|
||||||
|
|
||||||
|
panelConfig.dismissDisabled =
|
||||||
|
(popoverProps.isKeyboardDismissDisabled as boolean | undefined) ??
|
||||||
|
false;
|
||||||
|
panelConfig.shouldCloseOutside =
|
||||||
|
popoverProps.shouldCloseOnInteractOutside as
|
||||||
|
((event: Event) => boolean) | undefined;
|
||||||
|
|
||||||
// Reka fixes the submenu side/align to the parent convention, so only
|
// Reka fixes the submenu side/align to the parent convention, so only
|
||||||
// the offsets/collision props can be forwarded.
|
// the offsets/collision props can be forwarded.
|
||||||
const panelProps: Record<string, unknown> = {
|
const panelProps: Record<string, unknown> = {
|
||||||
sideOffset:
|
sideOffset:
|
||||||
(restProps.sideOffset as number | undefined) ??
|
(popoverProps.sideOffset as number | undefined) ??
|
||||||
(restProps.offset as number | undefined),
|
(popoverProps.offset as number | undefined),
|
||||||
alignOffset:
|
alignOffset:
|
||||||
(restProps.alignOffset as number | undefined) ??
|
(popoverProps.alignOffset as number | undefined) ??
|
||||||
(restProps.crossOffset as number | undefined),
|
(popoverProps.crossOffset as number | undefined),
|
||||||
avoidCollisions:
|
avoidCollisions:
|
||||||
(restProps.avoidCollisions as boolean | undefined) ??
|
(popoverProps.avoidCollisions as boolean | undefined) ??
|
||||||
(restProps.shouldFlip as boolean | undefined),
|
(popoverProps.shouldFlip as boolean | undefined),
|
||||||
collisionPadding:
|
collisionPadding:
|
||||||
(restProps.collisionPadding as number | undefined) ??
|
(popoverProps.collisionPadding as number | undefined) ??
|
||||||
(restProps.containerPadding as number | undefined),
|
(popoverProps.containerPadding as number | undefined),
|
||||||
loop: restProps.loop as boolean | undefined,
|
loop: popoverProps.loop as boolean | undefined,
|
||||||
};
|
};
|
||||||
const side = (restProps.side as string | undefined) ?? position.side;
|
const side = (popoverProps.side as string | undefined) ?? position.side;
|
||||||
const align = (restProps.align as string | undefined) ?? position.align;
|
const align =
|
||||||
|
(popoverProps.align as string | undefined) ?? position.align;
|
||||||
|
|
||||||
const keydownChain = [
|
const keydownChain = [
|
||||||
...asArray(popoverKeydown as Handler),
|
...asArray(popoverProps.onKeydown as Handler),
|
||||||
...asArray(attrs.onKeydown as Handler),
|
...asArray(attrs.onKeydown as Handler),
|
||||||
handleEscape,
|
handleEscape,
|
||||||
];
|
];
|
||||||
|
const interactChain = [
|
||||||
|
...asArray(popoverProps.onInteractOutside as Handler),
|
||||||
|
handleInteractOutside as (event: Event) => void,
|
||||||
|
];
|
||||||
const labelledBy = contentAttrs.labelledBy.value;
|
const labelledBy = contentAttrs.labelledBy.value;
|
||||||
|
|
||||||
return h(
|
return h(
|
||||||
@@ -258,28 +292,31 @@ export default defineComponent({
|
|||||||
),
|
),
|
||||||
h(
|
h(
|
||||||
RekaDropdownMenuPortal as never,
|
RekaDropdownMenuPortal as never,
|
||||||
{ to: restProps.to as string | HTMLElement | undefined },
|
{ to: popoverProps.to as string | HTMLElement | undefined },
|
||||||
{
|
{
|
||||||
default: () =>
|
default: () =>
|
||||||
h(
|
h(
|
||||||
RekaDropdownMenuSubContent as never,
|
RekaDropdownMenuSubContent as never,
|
||||||
{
|
{
|
||||||
|
...domProps,
|
||||||
...panelProps,
|
...panelProps,
|
||||||
side,
|
side,
|
||||||
align,
|
align,
|
||||||
"aria-label": contentAttrs.label.value,
|
"aria-label": contentAttrs.label.value,
|
||||||
"aria-describedby": contentAttrs.describedBy.value,
|
"aria-describedby": contentAttrs.describedBy.value,
|
||||||
loop:
|
class: [
|
||||||
(restProps.loop as boolean | undefined) ??
|
dropdownCtx?.slots.value.popover(),
|
||||||
contentAttrs.loop.value,
|
popoverProps.class,
|
||||||
class: [dropdownCtx?.slots.value.popover(), popoverClass],
|
],
|
||||||
"data-slot": "dropdown-popover",
|
"data-slot": "dropdown-popover",
|
||||||
"data-submenu-content": "true",
|
"data-submenu-content": "true",
|
||||||
"data-entering": entering.value || undefined,
|
"data-entering": entering.value || undefined,
|
||||||
"data-exiting": exiting.value || undefined,
|
"data-exiting": exiting.value || undefined,
|
||||||
onKeydown: keydownChain,
|
onKeydown: keydownChain,
|
||||||
|
onInteractOutside: interactChain,
|
||||||
onAnimationend: onAnimationEnd,
|
onAnimationend: onAnimationEnd,
|
||||||
onOpenAutoFocus: (event: Event) => {
|
onOpenAutoFocus: (event: Event) => {
|
||||||
|
if (event.defaultPrevented) return;
|
||||||
if (contentAttrs.autoFocus.value === false)
|
if (contentAttrs.autoFocus.value === false)
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -136,13 +136,13 @@ export default defineComponent({
|
|||||||
});
|
});
|
||||||
|
|
||||||
function shouldClose() {
|
function shouldClose() {
|
||||||
|
// Priority: item policy -> menu policy -> per-mode default.
|
||||||
if (props.closeOnSelect !== undefined) return props.closeOnSelect;
|
if (props.closeOnSelect !== undefined) return props.closeOnSelect;
|
||||||
const menuPolicy = menuCtx?.shouldCloseOnSelect.value;
|
const menuPolicy = menuCtx?.shouldCloseOnSelect.value;
|
||||||
if (typeof menuPolicy === "function") return menuPolicy(key.value);
|
if (typeof menuPolicy === "function") return menuPolicy(key.value);
|
||||||
// Default per mode: action and radio items close, checkboxes stay open.
|
if (typeof menuPolicy === "boolean") return menuPolicy;
|
||||||
if (selectionMode.value !== "none")
|
// Default: action and radio items close, checkboxes stay open.
|
||||||
return selectionMode.value === "single";
|
return selectionMode.value !== "multiple";
|
||||||
return menuPolicy ?? true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function activate() {
|
function activate() {
|
||||||
|
|||||||
@@ -85,7 +85,19 @@ export default defineComponent({
|
|||||||
const attrs = useAttrs();
|
const attrs = useAttrs();
|
||||||
const dropdownCtx = inject(dropdownContextKey, null);
|
const dropdownCtx = inject(dropdownContextKey, null);
|
||||||
const outerClose = inject(menuCloseKey, null);
|
const outerClose = inject(menuCloseKey, null);
|
||||||
const contentAttrs = inject(menuContentAttrsKey, null);
|
// Outside a Dropdown surface the Menu owns its content, so it provides the
|
||||||
|
// channel itself and keeps the name/description on the real menu node.
|
||||||
|
const liveContentAttrs = inject(menuContentAttrsKey, null) ?? {
|
||||||
|
label: ref<string | undefined>(),
|
||||||
|
labelledBy: ref<string | undefined>(),
|
||||||
|
describedBy: ref<string | undefined>(),
|
||||||
|
loop: ref<boolean | undefined>(),
|
||||||
|
autoFocus: ref<boolean | undefined>(),
|
||||||
|
escapeKeyBehavior: ref<"clearSelection" | "none">("none"),
|
||||||
|
clearSelection: () => {},
|
||||||
|
};
|
||||||
|
provide(menuContentAttrsKey, liveContentAttrs);
|
||||||
|
const contentAttrs = liveContentAttrs;
|
||||||
const outerMenuRoot = injectDropdownMenuRootContext(null);
|
const outerMenuRoot = injectDropdownMenuRootContext(null);
|
||||||
// Standalone Menu (no DropdownRoot/Popover) still supports items.
|
// Standalone Menu (no DropdownRoot/Popover) still supports items.
|
||||||
const needsOwnRoot = computed(() => !outerMenuRoot);
|
const needsOwnRoot = computed(() => !outerMenuRoot);
|
||||||
@@ -150,9 +162,9 @@ export default defineComponent({
|
|||||||
emitAction: (key: MenuKey) => emit("action", key),
|
emitAction: (key: MenuKey) => emit("action", key),
|
||||||
selection,
|
selection,
|
||||||
selectionMode: computed(() => props.selectionMode),
|
selectionMode: computed(() => props.selectionMode),
|
||||||
shouldCloseOnSelect: computed(
|
// Unset stays undefined: the item resolves the per-mode default so an
|
||||||
() => props.shouldCloseOnSelect ?? props.selectionMode === "multiple",
|
// explicit policy is never pre-empted by a guessed boolean.
|
||||||
),
|
shouldCloseOnSelect: computed(() => props.shouldCloseOnSelect),
|
||||||
itemCount,
|
itemCount,
|
||||||
registerItem: () => {
|
registerItem: () => {
|
||||||
itemCount.value += 1;
|
itemCount.value += 1;
|
||||||
@@ -225,7 +237,9 @@ export default defineComponent({
|
|||||||
"data-slot": "dropdown-popover",
|
"data-slot": "dropdown-popover",
|
||||||
sideOffset: 0,
|
sideOffset: 0,
|
||||||
avoidCollisions: false,
|
avoidCollisions: false,
|
||||||
"aria-label": contentAttrs?.label.value,
|
"aria-label": contentAttrs.label.value,
|
||||||
|
"aria-labelledby": contentAttrs.labelledBy.value,
|
||||||
|
"aria-describedby": contentAttrs.describedBy.value,
|
||||||
"data-static": "true",
|
"data-static": "true",
|
||||||
// A static in-flow list never steals focus from the page.
|
// A static in-flow list never steals focus from the page.
|
||||||
onOpenAutoFocus: (event: Event) => event.preventDefault(),
|
onOpenAutoFocus: (event: Event) => event.preventDefault(),
|
||||||
|
|||||||
@@ -22,8 +22,14 @@ export interface MenuContextValue {
|
|||||||
selection: MenuSelectionOwner;
|
selection: MenuSelectionOwner;
|
||||||
/** Root selection mode for data-selection-mode styling. */
|
/** Root selection mode for data-selection-mode styling. */
|
||||||
selectionMode: ComputedRef<MenuSelectionMode>;
|
selectionMode: ComputedRef<MenuSelectionMode>;
|
||||||
/** Menu-level close policy for activation (official `shouldCloseOnSelect`). */
|
/**
|
||||||
shouldCloseOnSelect: ComputedRef<boolean | ((key: MenuKey) => boolean)>;
|
* Menu-level close policy for activation (official `shouldCloseOnSelect`).
|
||||||
|
* Undefined means "no explicit policy"; the item then applies the per-mode
|
||||||
|
* default.
|
||||||
|
*/
|
||||||
|
shouldCloseOnSelect: ComputedRef<
|
||||||
|
boolean | ((key: MenuKey) => boolean) | undefined
|
||||||
|
>;
|
||||||
/** Number of mounted items, used for the empty-state slot. */
|
/** Number of mounted items, used for the empty-state slot. */
|
||||||
itemCount: Ref<number>;
|
itemCount: Ref<number>;
|
||||||
registerItem: () => () => void;
|
registerItem: () => () => void;
|
||||||
|
|||||||
@@ -241,3 +241,103 @@ it("submenu panel preserves a caller keydown listener", async () => {
|
|||||||
await flushPromises();
|
await flushPromises();
|
||||||
expect(keydown).toHaveBeenCalledTimes(1);
|
expect(keydown).toHaveBeenCalledTimes(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("default action activation closes its dropdown", async () => {
|
||||||
|
await render({}, () => h(DropdownItem, { id: "default" }, () => "Action"));
|
||||||
|
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
|
||||||
|
await flushPromises();
|
||||||
|
expect(document.querySelector('[role="menu"]')).toBeNull();
|
||||||
|
});
|
||||||
|
it.each([
|
||||||
|
["single", false, true],
|
||||||
|
["multiple", true, false],
|
||||||
|
] as const)(
|
||||||
|
"menu-level close policy for %s is honoured",
|
||||||
|
async (selectionMode, shouldCloseOnSelect, staysOpen) => {
|
||||||
|
await render({ selectionMode, shouldCloseOnSelect }, () =>
|
||||||
|
h(DropdownItem, { id: "policy" }, () => "Policy"),
|
||||||
|
);
|
||||||
|
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
|
||||||
|
await flushPromises();
|
||||||
|
expect(!!document.querySelector('[role="menu"]')).toBe(staysOpen);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
it("root popover preserves a user keydown callback", async () => {
|
||||||
|
const callback = vi.fn();
|
||||||
|
const w = mount(
|
||||||
|
defineComponent({
|
||||||
|
setup: () => () =>
|
||||||
|
h(DropdownRoot, { defaultOpen: true, modal: false }, () =>
|
||||||
|
h(DropdownPopover, { onKeydown: callback }, () =>
|
||||||
|
h(DropdownMenu, {}, () => h(DropdownItem, { id: "a" }, () => "A")),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
{ attachTo: document.body },
|
||||||
|
);
|
||||||
|
cleanup.push(() => w.unmount());
|
||||||
|
await flushPromises();
|
||||||
|
document.querySelector('[role="menu"]')!.dispatchEvent(
|
||||||
|
new KeyboardEvent("keydown", {
|
||||||
|
key: "a",
|
||||||
|
bubbles: true,
|
||||||
|
cancelable: true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await flushPromises();
|
||||||
|
expect(callback).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
async function openConfiguredSubmenu(props: Record<string, unknown>) {
|
||||||
|
await render({}, () =>
|
||||||
|
h(DropdownSubmenuTrigger, {}, () => [
|
||||||
|
h(DropdownItem, { id: "sub" }, () => [
|
||||||
|
"Sub",
|
||||||
|
h(DropdownSubmenuIndicator),
|
||||||
|
]),
|
||||||
|
h(DropdownPopover, props, () =>
|
||||||
|
h(DropdownMenu, {}, () =>
|
||||||
|
h(DropdownItem, { id: "inner" }, () => "Inner"),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
(document.querySelector('[data-key="sub"]') as HTMLElement).click();
|
||||||
|
await flushPromises();
|
||||||
|
return document.querySelector("[data-submenu-content]") as HTMLElement;
|
||||||
|
}
|
||||||
|
it("submenu preserves the panel's data and style attributes", async () => {
|
||||||
|
const panel = await openConfiguredSubmenu({
|
||||||
|
"data-custom": "kept",
|
||||||
|
style: { borderTopWidth: "7px" },
|
||||||
|
});
|
||||||
|
expect(panel.getAttribute("data-custom")).toBe("kept");
|
||||||
|
expect(panel.style.borderTopWidth).toBe("7px");
|
||||||
|
});
|
||||||
|
it("submenu honours isKeyboardDismissDisabled", async () => {
|
||||||
|
const panel = await openConfiguredSubmenu({
|
||||||
|
isKeyboardDismissDisabled: true,
|
||||||
|
});
|
||||||
|
const item = panel.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
||||||
|
item.focus();
|
||||||
|
item.dispatchEvent(
|
||||||
|
new KeyboardEvent("keydown", {
|
||||||
|
key: "Escape",
|
||||||
|
bubbles: true,
|
||||||
|
cancelable: true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await flushPromises();
|
||||||
|
expect(document.querySelector("[data-submenu-content]")).not.toBeNull();
|
||||||
|
});
|
||||||
|
it("standalone Menu keeps its own accessible name", async () => {
|
||||||
|
const w = mount(Menu, {
|
||||||
|
attrs: { "aria-label": "Standalone actions" },
|
||||||
|
slots: { default: () => h(DropdownItem, { id: "a" }, () => "A") },
|
||||||
|
attachTo: document.body,
|
||||||
|
});
|
||||||
|
cleanup.push(() => w.unmount());
|
||||||
|
await flushPromises();
|
||||||
|
expect(
|
||||||
|
document.querySelector('[role="menu"]')?.getAttribute("aria-label"),
|
||||||
|
).toBe("Standalone actions");
|
||||||
|
});
|
||||||
|
|||||||
@@ -1156,3 +1156,163 @@ it("standalone Menu stays in flow without modal side effects", async () => {
|
|||||||
// Focus is not stolen from the page.
|
// Focus is not stolen from the page.
|
||||||
expect(document.activeElement).not.toBe(surface);
|
expect(document.activeElement).not.toBe(surface);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("root popover merges caller handlers for escape, outside, focus and animation", async () => {
|
||||||
|
const calls = { focus: 0, escape: 0, outside: 0, animation: 0, keydown: 0 };
|
||||||
|
mountApp(
|
||||||
|
() => () =>
|
||||||
|
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
|
||||||
|
h(
|
||||||
|
DropdownPopover,
|
||||||
|
{
|
||||||
|
onOpenAutoFocus: () => {
|
||||||
|
calls.focus += 1;
|
||||||
|
},
|
||||||
|
onEscapeKeyDown: () => {
|
||||||
|
calls.escape += 1;
|
||||||
|
},
|
||||||
|
onInteractOutside: () => {
|
||||||
|
calls.outside += 1;
|
||||||
|
},
|
||||||
|
onAnimationend: () => {
|
||||||
|
calls.animation += 1;
|
||||||
|
},
|
||||||
|
onKeydown: () => {
|
||||||
|
calls.keydown += 1;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
() =>
|
||||||
|
h(DropdownMenu, {}, () =>
|
||||||
|
h(DropdownItem, { id: "a", textValue: "A" }, () =>
|
||||||
|
h(Label, () => "A"),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
await flushPromises();
|
||||||
|
await nextTick();
|
||||||
|
const menu = document.querySelector('[role="menu"]') as HTMLElement;
|
||||||
|
menu.dispatchEvent(
|
||||||
|
new KeyboardEvent("keydown", { key: "a", bubbles: true, cancelable: true }),
|
||||||
|
);
|
||||||
|
menu.dispatchEvent(new Event("animationend", { bubbles: true }));
|
||||||
|
await flushPromises();
|
||||||
|
expect(calls.keydown).toBe(1);
|
||||||
|
expect(calls.animation).toBe(1);
|
||||||
|
expect(calls.focus).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a cancelled caller keydown keeps the menu open for clearSelection menus", async () => {
|
||||||
|
const selections: MenuKey[][] = [];
|
||||||
|
mountApp(
|
||||||
|
() => () =>
|
||||||
|
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
|
||||||
|
h(
|
||||||
|
DropdownPopover,
|
||||||
|
{
|
||||||
|
onKeydown: (event: KeyboardEvent) => {
|
||||||
|
if (event.key === "Escape") event.preventDefault();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
() =>
|
||||||
|
h(
|
||||||
|
DropdownMenu,
|
||||||
|
{
|
||||||
|
selectionMode: "multiple",
|
||||||
|
selectedKeys: ["a"],
|
||||||
|
escapeKeyBehavior: "clearSelection",
|
||||||
|
"onUpdate:selectedKeys": (keys: MenuKey[]) =>
|
||||||
|
selections.push(keys),
|
||||||
|
},
|
||||||
|
() =>
|
||||||
|
h(DropdownItem, { id: "a", textValue: "A" }, () =>
|
||||||
|
h(Label, () => "A"),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
await flushPromises();
|
||||||
|
await nextTick();
|
||||||
|
const menu = document.querySelector('[role="menu"]') as HTMLElement;
|
||||||
|
menu.dispatchEvent(
|
||||||
|
new KeyboardEvent("keydown", {
|
||||||
|
key: "Escape",
|
||||||
|
bubbles: true,
|
||||||
|
cancelable: true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await flushPromises();
|
||||||
|
// The caller cancelled the escape, so the internal clear did not run.
|
||||||
|
expect(selections).toEqual([]);
|
||||||
|
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("submenu keyboard dismissal can be disabled without closing the parent", async () => {
|
||||||
|
mountApp(
|
||||||
|
() => () =>
|
||||||
|
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
|
||||||
|
h(DropdownPopover, {}, () =>
|
||||||
|
h(DropdownMenu, {}, () =>
|
||||||
|
h(DropdownSubmenuTrigger, {}, () => [
|
||||||
|
h(DropdownItem, { id: "sub", textValue: "S" }, () => [
|
||||||
|
h(Label, () => "S"),
|
||||||
|
h(DropdownSubmenuIndicator),
|
||||||
|
]),
|
||||||
|
h(DropdownPopover, { isKeyboardDismissDisabled: true }, () =>
|
||||||
|
h(DropdownMenu, {}, () =>
|
||||||
|
h(DropdownItem, { id: "inner", textValue: "I" }, () =>
|
||||||
|
h(Label, () => "I"),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
await flushPromises();
|
||||||
|
await nextTick();
|
||||||
|
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
|
||||||
|
await flushPromises();
|
||||||
|
await nextTick();
|
||||||
|
const panel = document.querySelector("[data-submenu-content]") as HTMLElement;
|
||||||
|
const item = panel.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
||||||
|
item.focus();
|
||||||
|
item.dispatchEvent(
|
||||||
|
new KeyboardEvent("keydown", {
|
||||||
|
key: "Escape",
|
||||||
|
bubbles: true,
|
||||||
|
cancelable: true,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await settle();
|
||||||
|
expect(document.querySelector("[data-submenu-content]")).not.toBeNull();
|
||||||
|
expect(
|
||||||
|
document.querySelectorAll('[data-slot="dropdown-popover"]').length,
|
||||||
|
).toBeGreaterThan(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("menu-level close policy wins over the section mode default", async () => {
|
||||||
|
mountApp(
|
||||||
|
() => () =>
|
||||||
|
h(DropdownRoot, { defaultOpen: true, modal: false }, () => [
|
||||||
|
h(DropdownPopover, {}, () =>
|
||||||
|
h(DropdownMenu, { shouldCloseOnSelect: false }, () =>
|
||||||
|
h(DropdownSection, { selectionMode: "single" }, () =>
|
||||||
|
h(DropdownItem, { id: "s", textValue: "S" }, () =>
|
||||||
|
h(Label, () => "S"),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
await flushPromises();
|
||||||
|
await nextTick();
|
||||||
|
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
|
||||||
|
await flushPromises();
|
||||||
|
await nextTick();
|
||||||
|
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user