From 2e4b3765379759f4afcfece2018e44cd2bf25ec7 Mon Sep 17 00:00:00 2001 From: maoyingjie Date: Sun, 25 Oct 2026 00:01:24 +0800 Subject: [PATCH] fix: complete submenu panel ARIA, focus and event passthrough - Submenu panel name priority is explicit aria-label > menu content channel > this instance's trigger; an explicit label drops the id reference so it cannot be shadowed. - openAutoFocus and animationend chain with the caller's handlers like keydown and interact-outside, so caller callbacks run and can cancel. - The panel loop falls back to the menu-level shouldFocusWrap and is always passed as a boolean: an undefined -> true update is dropped by the base prop chain, so the menu policy never applied. - Add a jsdom CSSOM polyfill for CSS.escape via vitest setupFiles; the animationend test no longer raises an unhandled error. --- docs/migrations/B01-b474a63-review.md | 44 +++++++++++ docs/migrations/B01-complete-review-2.md | 4 + docs/migrations/B01-menu-dropdown.md | 13 ++++ docs/verification.md | 10 +++ .../components/dropdown/DropdownPopover.vue | 4 +- .../dropdown/DropdownSubmenuTrigger.vue | 78 ++++++++++++++----- tests/b01-complete-review.probe.test.ts | 46 +++++++++++ tests/setup/cssom.ts | 38 +++++++++ vitest.config.ts | 6 +- 9 files changed, 223 insertions(+), 20 deletions(-) create mode 100644 docs/migrations/B01-b474a63-review.md create mode 100644 tests/setup/cssom.ts diff --git a/docs/migrations/B01-b474a63-review.md b/docs/migrations/B01-b474a63-review.md new file mode 100644 index 0000000..db32588 --- /dev/null +++ b/docs/migrations/B01-b474a63-review.md @@ -0,0 +1,44 @@ +# B01 b474a63 复核 + +2026-10-24;feat/b01-menu-dropdown / b474a63。检查代码及本地复跑,不依赖交接中“全套通过”的表述。结论:原7项失败均修好;当前仍有原R9子面板透传的3个分支未完成,及1项测试环境异常,暂不合并。本轮未修改实现、未提交/推送。 + +## 通过部分 + +- 原整批20项探针全部通过,R4关闭策略、R6根事件合并、R8独立Menu名称及R9上一轮data/style/禁止Esc探针对应修复有效。 +- 浏览器5181默认“复制链接”点击后menu数量0、trigger aria-expanded=false;没有复现默认不关闭回归。截图 `/private/tmp/b01-default-close-verified.png`。 +- 包装组件真实Props消费与typecheck通过。 +- 不要求为三级中间层焦点新增自动浏览器断言作为本轮门槛;现有浏览器序列观察和未自动化说明保留,不扩大SSR/水合/Nuxt范围。 + +## 剩余实现问题:均属原R9,不新增组件/能力范围 + +### 1. [P2] 子面板直接指定的ARIA被覆盖 + +DropdownSubmenuTrigger虽然先spread domProps,随后又无条件写 `aria-label: contentAttrs.label.value` / `aria-describedby: contentAttrs.describedBy.value`。子DropdownMenu没有指定这些值时为undefined,嵌套DropdownPopover传入的显式ARIA被删除。 + +回归:`openConfiguredSubmenu({'aria-label':'Direct submenu name','aria-describedby':'explicit-description'})`,实际aria-label=null。应同时保留Menu内容通道和面板调用方的明确属性,遵循约定优先级,并保证aria-labelledby不会意外压过显式名称;不要用undefined覆盖真实调用值。 + +### 2. [P2] 子面板openAutoFocus/animationend仍覆盖用户事件 + +SubContent的domProps中已有调用方onOpenAutoFocus,但后面重新赋值内部函数;onAnimationend也有同样覆盖结构。新增openAutoFocus回调探针调用次数为0(期望1)。根Popover已修的chain合并应等价应用于子面板,保留Vue事件数组与取消语义;不再只针对keydown单点修复。 + +### 3. [P2] 子菜单shouldFocusWrap没有传到底层 + +子DropdownMenu把shouldFocusWrap写进contentAttrs.loop;SubContent只收到panelProps.loop(即嵌套Popover显式loop),没有回退读取contentAttrs.loop,导致菜单级循环配置失效。此次b474a63修改时移除了原loop回退表达式。 + +新回归:子菜单shouldFocusWrap=true,焦点在First按ArrowUp,期望到Last,实际仍First。恢复明确优先级:面板显式loop→菜单shouldFocusWrap→底层默认;补双向边界及false不循环,别用声明支持代替行为。 + +以上3个分支均已加入 `tests/b01-complete-review.probe.test.ts`,现23项中原20项通过、新3项失败。请保持原断言,不删除或改成接受当前行为。 + +## 测试环境异常(与产品运行错误分开) + +在既有 `root popover merges caller handlers for escape, outside, focus and animation` 用例派发animationend后,Reka Presence调用CSS.escape,而jsdom环境未提供CSS,抛未捕获TypeError。单独跑B01测试时65项断言通过但命令仍因1个Error失败;完整回归同样出现,不能称完全通过。 + +修复测试环境的CSS.escape能力/合成AnimationEvent夹具,保持测试可单独运行;不要关闭未捕获错误检查、吞Reka异常或为了测试修改产品行为。这不是新增SSR要求。 + +## 最终复跑记录 + +- `pnpm test`:72文件,71通过/1失败;931项,928通过/3失败,另1个未捕获Error(上述jsdom CSS.escape)。 +- `pnpm typecheck`通过,`git diff --check`通过。 +- 本次没有重做逐像素/读屏/物理触屏或新一轮tarball安装,不把它们描述为已验收。 + +返修范围仅上述3个子面板分支和测试夹具。修后跑整批23项、B01其他回归及完整测试,并确认无Unhandled Errors;保留已完成的Portal/关闭/事件取消/名称/类型修复。不创建下一组件任务,不合main、不推送。 diff --git a/docs/migrations/B01-complete-review-2.md b/docs/migrations/B01-complete-review-2.md index 30c3c04..699b8b7 100644 --- a/docs/migrations/B01-complete-review-2.md +++ b/docs/migrations/B01-complete-review-2.md @@ -42,3 +42,7 @@ - 不新增SSR/水合/Nuxt验收,不把未重做的完整读屏/触屏/逐像素当作通过。 修复后按原整批清单复验受影响的默认/显式关闭策略、根/子事件及参数、独立Menu名称,并保持层级/Portal/Modal已通过路径。继续当前任务分支,交回复核,不合main、不推送。 + +## b474a63 后续复核 + +原7项已修复通过。当前仅原R9子面板ARIA/事件/loop分支及测试环境异常未完成,详见 [B01-b474a63-review.md](./B01-b474a63-review.md)。三级中间焦点未固化自动断言不作为新增门槛。 diff --git a/docs/migrations/B01-menu-dropdown.md b/docs/migrations/B01-menu-dropdown.md index 935bbef..b763c16 100644 --- a/docs/migrations/B01-menu-dropdown.md +++ b/docs/migrations/B01-menu-dropdown.md @@ -118,3 +118,16 @@ default、with-icons、with-descriptions、with-disabled-items、with-sections | R8 | 独立 `Menu` 自持内容属性通道,名称/描述落到真正的 `role="menu"` 节点 | 探针「standalone Menu keeps its own accessible name」 | 回归:**72 文件 / 928 项**通过;typecheck、Prettier、`git diff --check` 通过;独立 tarball 消费重跑通过;浏览器复验默认动作关闭、三级子菜单逐层 Esc 与 Portal/Modal 既有路径。 + +## b474a63 复核返修(2026-10-24) + +按 [b474a63 复核记录](B01-b474a63-review.md) 收口 R9 子面板透传的 3 个分支与测试环境异常,探针 **23/23**。 + +| 项 | 修复 | 证据 | +| ------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- | +| 子面板 ARIA 被覆盖 | 名称优先级:面板显式 `aria-label` > Menu 内容通道 > 实例触发器 id;有 `aria-label` 时移除 `aria-labelledby`,避免 id 引用压过显式名称 | 探针「submenu keeps a directly supplied panel accessible name」 | +| 子面板 `openAutoFocus` 被覆盖 | 与 `onKeydown`/`onInteractOutside`/`onAnimationend` 一样改为「调用方 + 内部」数组链,调用方先执行并按 `defaultPrevented` 取消 | 探针「submenu preserves a caller's openAutoFocus callback」 | +| 子菜单 `shouldFocusWrap` 未生效 | 面板 `loop` 恢复回退到菜单级 `contentAttrs.loop`;并统一传布尔值——实测 `undefined → true` 的更新会被底座属性链丢弃,`false → true` 才生效 | 探针「submenu respects the menu's shouldFocusWrap navigation」 | +| 测试环境 `CSS.escape` 缺失 | 新增 `tests/setup/cssom.ts`,仅在缺失时补 `CSS.escape` 并注册到 `vitest.config.ts` 的 `setupFiles`;不关错误检查、不改产品行为 | 单跑与全量均无 Unhandled Errors | + +回归:**72 文件 / 931 项**通过、无未捕获错误;typecheck、Prettier、`git diff --check` 通过。 diff --git a/docs/verification.md b/docs/verification.md index d57a02c..fcaf238 100644 --- a/docs/verification.md +++ b/docs/verification.md @@ -971,3 +971,13 @@ Button/PopoverTrigger/Link/ToggleButtonGroup 源码及几何复核后,移除 - **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。 + +### B01 b474a63 复核返修(同日) + +按 [b474a63 复核记录](migrations/B01-b474a63-review.md) 处理 R9 子面板透传的 3 个分支与 1 项测试环境异常;探针 **23/23**。 + +- **子面板 ARIA**:名称优先级为「面板显式 `aria-label` → Menu 内容通道 → 本实例触发器 id」,显式名称存在时移除 `aria-labelledby`,不再被 id 引用压过。探针里 `Direct submenu name` 与 `explicit-description` 均保留。 +- **子面板事件合并**:`onOpenAutoFocus`、`onAnimationend` 与 `onKeydown`、`onInteractOutside` 一样改为「调用方 + 内部」数组链,调用方先执行并按 `defaultPrevented` 取消;`openAutoFocus` 调用次数回归通过。 +- **子菜单 `shouldFocusWrap`**:面板 `loop` 回退到菜单级配置,并统一传布尔值——实测底座属性链会丢弃 `undefined → true` 的更新(`false → true` 生效),因此不能传 `undefined`。双向首尾循环回归通过。 +- **测试环境**:jsdom 缺少 `CSS`,Reka Presence 在 `animationend` 时会调用 `CSS.escape` 抛未捕获错误。新增 `tests/setup/cssom.ts` 仅在缺失时补齐该能力并注册到 `setupFiles`,未关闭错误检查、未吞异常、未改产品行为;单跑与全量都不再有 Unhandled Errors。 +- 回归:**72 文件 / 931 项**通过(无未捕获错误);typecheck、Prettier、`git diff --check` 通过;未重做逐像素/读屏/物理触屏,仍按未验收记录。 diff --git a/packages/vue/src/components/dropdown/DropdownPopover.vue b/packages/vue/src/components/dropdown/DropdownPopover.vue index 412a9d1..ba1b142 100644 --- a/packages/vue/src/components/dropdown/DropdownPopover.vue +++ b/packages/vue/src/components/dropdown/DropdownPopover.vue @@ -219,7 +219,9 @@ export default defineComponent({ avoidCollisions: props.avoidCollisions ?? props.shouldFlip, collisionPadding: props.collisionPadding ?? props.containerPadding, - loop: props.loop ?? contentAttrs?.loop.value, + // Boolean on purpose: undefined -> true updates are dropped by + // the base prop chain. + loop: props.loop ?? contentAttrs?.loop.value ?? false, onAnimationend: chain( "onAnimationend", onAnimationEnd, diff --git a/packages/vue/src/components/dropdown/DropdownSubmenuTrigger.vue b/packages/vue/src/components/dropdown/DropdownSubmenuTrigger.vue index 67ed2cc..4210930 100644 --- a/packages/vue/src/components/dropdown/DropdownSubmenuTrigger.vue +++ b/packages/vue/src/components/dropdown/DropdownSubmenuTrigger.vue @@ -137,19 +137,28 @@ export default defineComponent({ shouldCloseOutside?: (event: Event) => boolean; } = { dismissDisabled: false }; + // Accessible-name decision for the current render; the repair pass reads it. + const panelAria: { label?: string; labelledBy?: string } = {}; + /** * 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 - * dangles. Point it at the real trigger element of this instance. + * dangles. Priority: explicit aria-label (wins over any id reference) > + * caller/Menu `aria-labelledby` > this instance's trigger element. */ - function repairLabel(explicit?: string) { + function repairLabel() { const content = contentElement.value; - const trigger = elementFromValue(triggerRef.value); - if (!content || !trigger) return; - if (explicit) { - content.setAttribute("aria-labelledby", explicit); + if (!content) return; + if (panelAria.label) { + content.removeAttribute("aria-labelledby"); return; } + if (panelAria.labelledBy) { + content.setAttribute("aria-labelledby", panelAria.labelledBy); + return; + } + const trigger = elementFromValue(triggerRef.value); + if (!trigger) return; if (!trigger.id) trigger.id = fallbackId; content.setAttribute("aria-labelledby", trigger.id); } @@ -240,7 +249,12 @@ export default defineComponent({ collisionPadding: (popoverProps.collisionPadding as number | undefined) ?? (popoverProps.containerPadding as number | undefined), - loop: popoverProps.loop as boolean | undefined, + // Always a boolean: an undefined -> true transition is dropped by the + // base prop chain, so the menu-level policy would never apply. + loop: + (popoverProps.loop as boolean | undefined) ?? + contentAttrs.loop.value ?? + false, }; const side = (popoverProps.side as string | undefined) ?? position.side; const align = @@ -255,7 +269,40 @@ export default defineComponent({ ...asArray(popoverProps.onInteractOutside as Handler), handleInteractOutside as (event: Event) => void, ]; - const labelledBy = contentAttrs.labelledBy.value; + const focusChain = [ + ...asArray(popoverProps.onOpenAutoFocus as Handler), + (event: Event) => { + if (event.defaultPrevented) return; + if (contentAttrs.autoFocus.value === false) event.preventDefault(); + }, + ]; + const animationChain = [ + ...asArray(popoverProps.onAnimationend as Handler), + onAnimationEnd as (event: Event) => void, + ]; + + // Explicit caller values win over the Menu content channel; a label + // always beats an id reference so it cannot be shadowed silently. + const callerLabel = popoverProps["aria-label"] as string | undefined; + const callerLabelledBy = popoverProps["aria-labelledby"] as + string | undefined; + const callerDescribedBy = popoverProps["aria-describedby"] as + string | undefined; + const effectiveLabel = callerLabel ?? contentAttrs.label.value; + const effectiveLabelledBy = + callerLabelledBy ?? contentAttrs.labelledBy.value; + const describedBy = callerDescribedBy ?? contentAttrs.describedBy.value; + panelAria.label = effectiveLabel; + panelAria.labelledBy = effectiveLabelledBy; + const ariaProps: Record = {}; + if (effectiveLabel) { + ariaProps["aria-label"] = effectiveLabel; + // An id reference would win over the explicit label; drop it. + ariaProps["aria-labelledby"] = undefined; + } else if (effectiveLabelledBy) { + ariaProps["aria-labelledby"] = effectiveLabelledBy; + } + if (describedBy) ariaProps["aria-describedby"] = describedBy; return h( RekaDropdownMenuSub as never, @@ -302,8 +349,7 @@ export default defineComponent({ ...panelProps, side, align, - "aria-label": contentAttrs.label.value, - "aria-describedby": contentAttrs.describedBy.value, + ...ariaProps, class: [ dropdownCtx?.slots.value.popover(), popoverProps.class, @@ -314,21 +360,17 @@ export default defineComponent({ "data-exiting": exiting.value || undefined, onKeydown: keydownChain, onInteractOutside: interactChain, - onAnimationend: onAnimationEnd, - onOpenAutoFocus: (event: Event) => { - if (event.defaultPrevented) return; - if (contentAttrs.autoFocus.value === false) - event.preventDefault(); - }, + onAnimationend: animationChain, + onOpenAutoFocus: focusChain, onVnodeMounted: (vnode) => { contentElement.value = menuSurfaceFromVNode(vnode); - void nextTick(() => repairLabel(labelledBy)); + void nextTick(repairLabel); }, onVnodeUpdated: (vnode) => { const element = menuSurfaceFromVNode(vnode); if (!element) return; contentElement.value = element; - void nextTick(() => repairLabel(labelledBy)); + void nextTick(repairLabel); }, onVnodeUnmounted: () => { contentElement.value = null; diff --git a/tests/b01-complete-review.probe.test.ts b/tests/b01-complete-review.probe.test.ts index e0c67d3..b1f970a 100644 --- a/tests/b01-complete-review.probe.test.ts +++ b/tests/b01-complete-review.probe.test.ts @@ -341,3 +341,49 @@ it("standalone Menu keeps its own accessible name", async () => { document.querySelector('[role="menu"]')?.getAttribute("aria-label"), ).toBe("Standalone actions"); }); + +it("submenu keeps a directly supplied panel accessible name", async () => { + const panel = await openConfiguredSubmenu({ + "aria-label": "Direct submenu name", + "aria-describedby": "explicit-description", + }); + expect(panel.getAttribute("aria-label")).toBe("Direct submenu name"); + expect(panel.getAttribute("aria-describedby")).toBe("explicit-description"); +}); +it("submenu preserves a caller's openAutoFocus callback", async () => { + const callback = vi.fn(); + await openConfiguredSubmenu({ onOpenAutoFocus: callback }); + expect(callback).toHaveBeenCalledTimes(1); +}); +it("submenu respects the menu's shouldFocusWrap navigation", async () => { + await render({}, () => + h(DropdownSubmenuTrigger, {}, () => [ + h(DropdownItem, { id: "sub" }, () => [ + "Sub", + h(DropdownSubmenuIndicator), + ]), + h(DropdownPopover, {}, () => + h(DropdownMenu, { shouldFocusWrap: true }, () => [ + h(DropdownItem, { id: "first", textValue: "First" }, () => "First"), + h(DropdownItem, { id: "last", textValue: "Last" }, () => "Last"), + ]), + ), + ]), + ); + (document.querySelector('[data-key="sub"]') as HTMLElement).click(); + await flushPromises(); + const panel = document.querySelector("[data-submenu-content]")!; + const first = panel.querySelector('[data-key="first"]') as HTMLElement; + const last = panel.querySelector('[data-key="last"]') as HTMLElement; + first.focus(); + await flushPromises(); + first.dispatchEvent( + new KeyboardEvent("keydown", { + key: "ArrowUp", + bubbles: true, + cancelable: true, + }), + ); + await flushPromises(); + expect(document.activeElement).toBe(last); +}); diff --git a/tests/setup/cssom.ts b/tests/setup/cssom.ts new file mode 100644 index 0000000..b9cd8a2 --- /dev/null +++ b/tests/setup/cssom.ts @@ -0,0 +1,38 @@ +/** + * jsdom lacks parts of the CSSOM. Reka's Presence uses `CSS.escape` when it + * resolves exiting children, so tests that dispatch real `animationend` events + * need the same capability a browser provides. Only fills the gap; never + * overrides an existing implementation. + */ +type CssEscapeHost = { escape?: (value: string) => string }; +const cssGlobal = globalThis as unknown as { CSS?: CssEscapeHost }; + +if (!cssGlobal.CSS) cssGlobal.CSS = {}; + +if (typeof cssGlobal.CSS.escape !== "function") { + // Approximation of CSS.escape for selector fragments: hex-escape leading + // digits and any character outside the identifier set. + const escapeIdentifier = (value: string) => { + const input = String(value); + let output = ""; + for (let index = 0; index < input.length; index += 1) { + const char = input[index]; + const code = char.codePointAt(0) ?? 0; + const isFirst = index === 0; + const digits = code >= 0x30 && code <= 0x39; + const safe = + code >= 0x80 || + code === 0x2d || + code === 0x5f || + (code >= 0x30 && code <= 0x39) || + (code >= 0x41 && code <= 0x5a) || + (code >= 0x61 && code <= 0x7a); + if (code === 0) output += "\uFFFD"; + else if ((isFirst && digits) || !safe) + output += `\\${code.toString(16)} `; + else output += char; + } + return output; + }; + cssGlobal.CSS.escape = escapeIdentifier; +} diff --git a/vitest.config.ts b/vitest.config.ts index 4475356..a93840a 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -23,5 +23,9 @@ export default defineConfig({ ), }, }, - test: { environment: "jsdom", include: ["tests/**/*.test.ts"] }, + test: { + environment: "jsdom", + include: ["tests/**/*.test.ts"], + setupFiles: ["tests/setup/cssom.ts"], + }, });