Skip to content

fix: cancel pending autoFocus raf when dropdown closes - #276

Open
mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/cancel-autofocus-raf-on-close
Open

mokshupadhyay wants to merge 1 commit into
react-component:masterfrom
mokshupadhyay:fix/cancel-autofocus-raf-on-close

Conversation

@mokshupadhyay

@mokshupadhyay mokshupadhyay commented Dec 1, 2026 •

Copy link
Copy Markdown

With autoFocus, useAccessibility schedules the focus of the menu with raf(() => focusMenu(...), 3), which runs three animation frames after the dropdown opens. The cleanup of that effect removed the keydown listener but did not cancel the pending raf. So if the dropdown closed before those frames passed (for example it is opened and closed quickly, or open is toggled by the parent), the scheduled callback still ran afterwards and moved focus into the menu that was no longer open, away from the trigger.

The effect now keeps the id returned by raf() and calls raf.cancel(rafId) in its cleanup, so a close before the callback fires cancels the focus. Behaviour when the dropdown stays open is unchanged (the menu is still focused after the three frames).

Test: cancels pending autoFocus when closed before raf fires in tests/useAccessibility.test.tsx renders the hook with open: true and autoFocus, re-renders with open: false, runs all timers and expects focus to stay on the trigger. It fails on master (focus ends up elsewhere, on the menu) and passes now. npx rc-test passes (6 suites, 32 tests), tsc --noEmit and ESLint on the hook are clean, and Prettier is clean on the changed files.

Summary by CodeRabbit

  • Bug 修复

    • 关闭自动聚焦状态时,会取消尚未执行的焦点任务,避免焦点意外移动。
  • 测试

    • 新增测试,验证关闭自动聚焦后焦点仍保留在触发元素上。

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

自动聚焦效果现在保存动画帧任务 ID,并在清理时取消已排定的任务。新增测试验证自动聚焦后关闭时,触发元素仍保持焦点。

Changes

自动聚焦任务清理

Layer / File(s) Summary
保存并取消自动聚焦任务
src/hooks/useAccessibility.ts, tests/useAccessibility.test.tsx
效果保存 raf 返回的任务 ID,并在清理时取消已排定的任务。测试使用假计时器验证关闭后焦点仍在触发元素上。

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

Change: Bug fix

Suggested reviewers: lkxdsb

Merge Risk: Minimal · up to 48d67

This change stops a pending auto-focus callback from moving focus to a closed dropdown menu. Behavior while the dropdown stays open is unchanged. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix: cancel pending autoFocus raf when dropdown closes”准确描述主要变更:下拉菜单关闭时取消待执行的 autoFocus raf 回调。raw_summary 显示 useAccessibility.ts 保存 raf ID,并在清理时调用 raf.cancel;测试文件新增对应用例。标题与变更集一致,且具体。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

兔子轻轻按下开关,
焦点安稳留在原处。
帧任务收好再取消,
测试钟声滴答作答。
萝卜摇摇庆祝收尾。

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a2ed220d-f00d-486e-b9be-f343e3d93123
Commits

Reviewing files that changed from the base of the PR and between b4ea62f and 48d67ee.

Files selected for processing: 2

  • src/hooks/useAccessibility.ts
  • tests/useAccessibility.test.tsx

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

This branch has not been deployed

No deployments
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.

1 participant