Skip to content

fix(Menu): 在异步焦点处理中保留事件目标 - #2930

Merged
zernonia merged 1 commit into
unovue:v2from
qqqqqf-q:fix/menu-focus-current-target
Sep 11, 2026
Merged

zernonia merged 1 commit into
unovue:v2from
qqqqqf-q:fix/menu-focus-current-target

Conversation

@qqqqqf-q

@qqqqqf-q qqqqqf-q commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

🔗 关联 issue

Closes #2929

❓ 变更类型

  • 🐞 Bug 修复

📚 说明

子菜单通过悬停打开后,继续在同一个触发项内移动鼠标,可以使它意外关闭。MenuItemImpl 在 pointermove 内调用 .focus();异步 focus 处理器恢复时再读取 event.currentTarget,在此路径中会得到 null,并覆盖当前高亮项。

本次在 await nextTick() 前保存该元素,随后使用保存的目标更新高亮。保留原有的异步处理、defaultPrevented 和 disabled 判断;生产代码仅修改这一处处理器。

新增回归测试复用现有 MenuWithSubmenu fixture,覆盖「悬停打开 → 再次在同一触发项内 pointermove → 子菜单保持打开」。该断言在未修复的上游代码上失败,修复后通过。

同时为 Menu 子菜单测试和 DropdownMenuFilter 测试补齐组件卸载。Filter 测试原先仅清空 body,没有卸载组件,导致共享的键盘输入状态跨测试残留:受影响用例单独运行通过,连续运行失败。补齐卸载后,原有断言均通过,没有修改 Filter 的生产逻辑或测试预期。

验证

  • pnpm --filter reka-ui exec vitest run --coverage --maxWorkers=4:97 个测试文件、2042 项测试通过。
  • pnpm --filter reka-ui build:类型检查和构建通过。
  • pnpm --filter reka-ui size:包体积检查通过。
  • 对三个修改文件执行 ESLint,以及 git diff --check:通过。
  • Chromium 152:触发项内移动、移向子菜单两条路径,各重复 5 次。上游原版各关闭 5/5 次,修复版各关闭 0/5 次;比较的构建产物仅 MenuItemImpl 的 ESM/CJS 文件不同。
  • 浏览器自检覆盖鼠标进入子项并返回、移离子菜单方向后切换父菜单项、ArrowRight / ArrowLeft、Escape 恢复焦点、选择子项和点击外部关闭。

独立复现和环境详情见关联 issue。本次未验证其他浏览器。

📝 Checklist

  • 已关联包含最小复现的 issue。
  • 已检查文档影响:无公开 API 变更,无需更新 API 文档。

⚠️ No human QA — this PR has not been verified by a human yet. Remove this line once a human confirms the happy path.

Summary by CodeRabbit

  • Bug Fixes

    • Improved submenu behavior so it remains open when the pointer moves within its focused trigger.
    • Improved focus handling when navigating menu items, ensuring the correct item remains highlighted.
  • Tests

    • Added coverage for submenu focus and open-state behavior.
    • Improved test cleanup between cases for menus and filtered dropdown menus.

@coderabbitai

coderabbitai Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: aea043ca-0135-47af-bed1-d7a88a37606e

📥 Commits

Reviewing files that changed from the base of the PR and between 0a81bff and f20810c.

📒 Files selected for processing (3)
  • packages/core/src/DropdownMenu/DropdownMenuFilter.test.ts
  • packages/core/src/Menu/Menu.test.ts
  • packages/core/src/Menu/MenuItemImpl.vue

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Walkthrough

Walkthrough

The focus handler now captures the focused menu item before asynchronous processing. Tests verify that submenu pointer movement preserves the open state and that mounted test wrappers and timers are cleaned up.

Changes

Menu focus stability

Layer / File(s) Summary
Capture the focused menu item
packages/core/src/Menu/MenuItemImpl.vue
The focus handler stores event.currentTarget before nextTick() and assigns the captured element to highlightedElement.
Validate submenu pointer movement
packages/core/src/Menu/Menu.test.ts, packages/core/src/DropdownMenu/DropdownMenuFilter.test.ts
The submenu test uses fake timers and verifies that repeated pointer movement keeps the submenu expanded. Test hooks unmount wrappers and restore real timers after each test.

Priority: ➖ Normal — Schedule the menu focus fix because it resolves a medium-severity submenu bug that can cause unexpected closing and flicker during pointer movement.

Estimated code review effort: 2 (Simple) | ~10 minutes

Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to f2081

This change prevents an open submenu from closing while the pointer continues moving within its trigger, with regression coverage for the affected interaction. No merge-blocking risk remains.

Suggested reviewers: benjamincanac

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 … Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了主要修复:在异步焦点处理中保留事件目标。
Linked Issues check ✅ Passed 变更满足 Issue #2929 的要求。代码在 await nextTick() 前保存焦点事件目标,并保留原有判断。新增回归测试验证子菜单在触发项内再次移动指针后仍保持打开。
Out of Scope Changes check ✅ Passed 所有变更均与 Issue #2929 相关。新增的测试卸载和计时器清理用于隔离测试状态,没有发现无关的生产代码或公共 API 变更。
Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Some tools did not complete. Review the errors below.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@zernonia
zernonia merged commit 9a07946 into unovue:v2 Sep 11, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: 子菜单打开后在同一触发项内移动鼠标会意外关闭

2 participants