feat: manage focus for accessible click/context popups
Summary
Implements focus behavior for floating popups as discussed for Ant Design Popover/Popconfirm-style usage (ant-design#57333): move focus into content on open, return it to the trigger on close, and keep Tab navigation inside the layer while open. Escape behavior is unchanged (existing Portal onEsc).
Default behavior
- On when open is driven only by click, contextMenu, and/or focus (no hover in show actions).
- Off when hover is a show action (including ['hover', 'click']), so hover tooltips do not steal focus.
- focusPopup prop overrides the default explicitly.
Summary by CodeRabbit
- 新功能
- 新增可配置自动焦点管理(focusPopup):打开时将焦点移至弹窗根或首个可聚焦元素,关闭时在合适情况下恢复至触发元素;触发器与提供者层级已连通该选项,默认在 click/contextMenu/focus 生效,hover 禁用。
- 添加弹窗内 Tab/Shift+Tab 焦点环与聚焦辅助行为,确保在弹窗内循环聚焦并处理无可聚焦元素的情况。
- 测试
- 新增针对聚焦行为与焦点工具函数的详细测试覆盖(打开聚焦、关闭还原、Tab 抑制与边界场景)。
[!NOTE]
Reviews paused
It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the
reviews.auto_review.auto_pause_after_reviewed_commitssetting.Use the following commands to manage reviews:
@coderabbitai resumeto resume automatic reviews.@coderabbitai reviewto trigger a single review.Use the checkboxes below for quick actions:
- [ ] ▶️ Resume reviews
- [ ] 🔍 Trigger review
Walkthrough
在 Trigger/Popup 链路中新增可选聚焦管理配置 focusPopup:打开时聚焦根节点或第一个可聚焦子元素,关闭时在条件满足下恢复触发器焦点,并在弹窗内启用 Tab 键环状捕获;相关工具、接口与测试一并添加。
Changes
| Cohort / File(s) | Summary |
|---|---|
焦点工具 src/focusUtils.ts |
新增焦点辅助模块:计算首/末可聚焦元素、聚焦弹窗根或首个可聚焦子元素、处理 Tab 键环状捕获;导出 getTabbableEdges、focusPopupRootOrFirst、handlePopupTabTrap。 |
Popup 组件 src/Popup/index.tsx |
新增 focusPopup?: boolean prop;添加 rootRef 与 prevOpen 跟踪;使用 useLayoutEffect 在 closed→open 时聚焦(或首个可聚焦子元素),closed←open 时在条件满足下恢复 trigger 焦点;注册 onKeyDownCapture 调用 handlePopupTabTrap;将 !show 早退移动以保证钩子执行。 |
UniqueProvider & 上下文 src/UniqueProvider/index.tsx, src/context.ts |
在 UniqueShowOptions 添加 focusPopup?: boolean,并将 mergedOptions.focusPopup 透传给渲染的 Popup。 |
Trigger 接口与合并逻辑 src/index.tsx |
在导出 TriggerProps 添加 focusPopup?: boolean;引入 mergedFocusPopup(基于 show action 的默认规则并考虑 hover),将其注入 UniqueProvider/Popup;移除重复的 show/hide action 重新计算。 |
测试 tests/focus.test.tsx, tests/focusUtils.test.tsx |
新增聚焦管理与工具函数测试:覆盖自动聚焦、hover 例外、关闭时恢复焦点、Tab 捕获行为、shadow DOM/slot 场景及不可聚焦元素过滤,并对布局相关 DOM API 做全局 stub。 |
依赖 package.json |
新增运行时依赖 focusable-selectors@^0.8.4。 |
Sequence Diagram(s)
sequenceDiagram
participant Trigger
participant UniqueProvider
participant Popup
participant Document
Trigger->>UniqueProvider: 请求打开(包含 focusPopup 选项)
UniqueProvider->>Popup: 渲染并传入 focusPopup
Popup->>Popup: 挂载 rootRef,记录 prevOpen
Popup->>Document: useLayoutEffect(closed→open) 调用 focusPopupRootOrFirst(root)
Popup->>Popup: 根节点注册 onKeyDownCapture -> handlePopupTabTrap
Popup->>Document: 关闭时(open→closed)若 active 在 popup 内且 trigger 仍 connected,则聚焦 trigger
Estimated code review effort
🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs
- react-component/trigger#511: 修改
src/index.tsx的 TriggerProps / generateTrigger 合并逻辑,与本次对focusPopup的合并与默认规则改动相关。 - react-component/trigger#568: 涉及
UniqueProvider与上下文传递改动,与本次将focusPopup透传至Popup的路径直接相关。 - react-component/trigger#493: 修改
Popup接口及事件处理,与本次对Popup聚焦与键盘处理的更改触及相同组件接口。
Suggested reviewers
- afc163
Poem
🐇 我是小兔子轻轻跃,
弹窗门里灯火热,
Tab 键绕圈不迷路,
关窗归来找回窝,
焦点安稳甜又乐 ✨
🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | ⚠️ Warning | Docstring coverage is 27.27% which is insufficient. The required threshold is 80.00%. | Write docstrings for the functions missing them to satisfy the coverage threshold. |
✅ Passed checks (2 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | 标题准确概括了主要功能变更:为可访问的点击/右键菜单弹出框实现焦点管理,与整个变更集的核心目标完全相关。 |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing Touches
🧪 Generate unit tests (beta)
- [ ] Create PR with unit tests
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.
Comment @coderabbitai help to get the list of available commands and usage tips.
@meet-student Please review
@meet-student had a chance to review?
@afc163 @meet-student please review
@KittyGiraudel Thanks for your review, I just updated PR. Please review again
Codecov Report
:x: Patch coverage is 81.20301% with 25 lines in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 95.28%. Comparing base (59b659d) to head (01a93c6).
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/focusUtils.ts | 74.22% | 25 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## master #613 +/- ##
==========================================
- Coverage 97.26% 95.28% -1.99%
==========================================
Files 17 18 +1
Lines 952 1082 +130
Branches 274 330 +56
==========================================
+ Hits 926 1031 +105
- Misses 26 51 +25
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
| Diff | Package | Supply Chain Security |
Vulnerability | Quality | Maintenance | License |
|---|---|---|---|---|---|---|
| focusable-selectors@0.8.4 |
@zombieJ @afc163 Could you please review?
@zombieJ @afc163 Ping.
@afc163 @zombieJ @meet-student @QDyanbing Could you please review?
@afc163 Could you please take a look?
@claytonlin1110 Could you please resolve conflicts?
@claytonlin1110 Could you please resolve conflicts?
Conflicting!Rebase please.