You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: cancel pending autoFocus raf when dropdown closes - #276
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.
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: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
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.
Hi @yoyo837, a friendly ping: this is ready for review whenever you have time. Happy to adjust anything. Thanks!
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
autoFocus,useAccessibilityschedules the focus of the menu withraf(() => focusMenu(...), 3), which runs three animation frames after the dropdown opens. The cleanup of that effect removed thekeydownlistener but did not cancel the pendingraf. So if the dropdown closed before those frames passed (for example it is opened and closed quickly, oropenis 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 callsraf.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 firesintests/useAccessibility.test.tsxrenders the hook withopen: trueandautoFocus, re-renders withopen: false, runs all timers and expects focus to stay on the trigger. It fails onmaster(focus ends up elsewhere, on the menu) and passes now.npx rc-testpasses (6 suites, 32 tests),tsc --noEmitand ESLint on the hook are clean, and Prettier is clean on the changed files.Summary by CodeRabbit
Bug 修复
测试