feat: accessibility improvements
Implements 2/3 of the proposed changes here: https://github.com/ant-design/ant-design/issues/53203
I did not implement the panel role change because there is already some logic around setting the panel role to "tabpanel" when in accordion mode, and the panel role = "region" is optional for a11y purposes.
- Adds optional
headingLevelprop which will wrap the collapse header in a header element - Adds a default id generated by uuid to the collapse which will be used to create ids for the child components in order to link the header and panel.
- Adds optional
idprop which will override the default id mentioned above
Summary by CodeRabbit
-
Style
- 优化折叠组件头部样式,确保标准与禁用状态下显示统一。
-
新功能
- 引入自动生成唯一标识符功能,支持自定义 ID,提升组件稳定性与无障碍体验。
- 增加了设置标题层级的选项,使显示层次更灵活友好。
- 折叠面板头部增加 ARIA 角色与属性,增强无障碍支持。
-
Tests
- 扩展了测试覆盖,验证 ID 与标题层级功能的正确性,从而提升了整体可靠性。
The latest updates on your projects. Learn more about Vercel for Git ↗︎
| Name | Status | Preview | Comments | Updated (UTC) |
|---|---|---|---|---|
| collapse | ✅ Ready (Inspect) | Visit Preview | 💬 Add feedback | Jun 27, 2025 1:51pm |
Walkthrough
此次变更主要包含对 Collapse 组件及其子组件(Panel、PanelContent)的增强改造,同时对样式文件和测试用例进行了相应更新。CSS 中扩展了 header 的选择器以支持包裹结构;组件中新增了通过 useId 自动生成 id 的逻辑,并引入了 headingLevel 属性用于辅助设置无障碍相关属性;接口定义中添加了新的 HeadingLevelType 类型;测试用例也更新以覆盖新增 id 与 headingLevel 的功能。
Changes
| 文件(s) | 变更摘要 |
|---|---|
| assets/index.less | 扩展 header 选择器,新增 @{prefixCls}-header-wrapper > .@{prefixCls}-header 样式,统一应用常规及禁用状态下的样式。 |
| src/Collapse.tsx src/Panel.tsx src/PanelContent.tsx src/hooks/…/useItems.tsx |
在组件中新增 id 自动生成(基于 useId)和 headingLevel 属性,更新属性传递及无障碍相关的 aria 属性设置。 |
| src/interface.ts | 新增 HeadingLevelType 类型(1~6),并在 CollapseProps 与 CollapsePanelProps 中添加 headingLevel 和(CollapseProps 的)id 属性。 |
| tests/index.spec.tsx | 更新测试用例,新增针对 id 与 headingLevel 的测试,并重构部分测试逻辑以提升可读性。 |
Sequence Diagram(s)
sequenceDiagram
participant U as 用户
participant C as Collapse组件
participant H as useItems钩子
participant P as Panel组件
participant W as HeaderWrapper
U->>C: 请求渲染 Collapse
C->>C: 检查是否提供 id(无则使用 useId 生成)
C->>H: 传递 id 与 headingLevel
H->>P: 将 id(可能附加索引)和 headingLevel 传给各 Panel
P->>W: 根据 headingLevel 包装 header
W-->>P: 返回处理后的 header
Possibly related PRs
- react-component/collapse#353: 该 PR 同样涉及 CollapsePanel 组件头部的无障碍属性调整,主 PR 增加了包裹 div 和 headingLevel,而该 PR 调整了 header 的 role 属性,两者均为提升头部无障碍体验相关。
- react-component/collapse#354: 该 PR 也对 CollapsePanel 组件的无障碍属性和事件处理进行了调整,虽聚焦点不同,但与本次改动在代码层面存在关联。
Poem
我是一只快乐的小兔子,
在代码森林里欢快奔跑,
ID 与标题层级轻轻织就,
无障碍世界更加美妙,
修改微小如花开一瞬,
让每行代码闪耀着光芒!
🥕 跟我一同庆祝这新希望!
[!WARNING] There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.
🔧 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.
src/PanelContent.tsx
Oops! Something went wrong! :(
ESLint: 8.57.1
ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.
The config "prettier" was referenced from the config file in "/.eslintrc.js".
If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.
src/Panel.tsx
Oops! Something went wrong! :(
ESLint: 8.57.1
ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.
The config "prettier" was referenced from the config file in "/.eslintrc.js".
If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.
src/hooks/useItems.tsx
Oops! Something went wrong! :(
ESLint: 8.57.1
ESLint couldn't find the config "prettier" to extend from. Please check that the name of the config is correct.
The config "prettier" was referenced from the config file in "/.eslintrc.js".
If you still have problems, please stop by https://eslint.org/chat/help to chat with the team.
📜 Recent review details
Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro
📥 Commits
Reviewing files that changed from the base of the PR and between 536080213cee862272ebd230d2455356aac1fbcb and dbad27ac00a326fc56d28fa349d6299debbf99be.
📒 Files selected for processing (3)
src/Panel.tsx(3 hunks)src/PanelContent.tsx(3 hunks)src/hooks/useItems.tsx(5 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
- src/PanelContent.tsx
- src/hooks/useItems.tsx
- src/Panel.tsx
✨ Finishing Touches
- [ ] 📝 Generate Docstrings
🧪 Generate Unit Tests
- [ ] Create PR with Unit Tests
- [ ] Post Copyable Unit Tests in Comment
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.
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>, please review it.Explain this complex logic.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai explain this code block.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read src/utils.ts and explain its main purpose.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Support
Need help? Create a ticket on our support page for assistance with any issues or questions.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR.@coderabbitai auto-generate unit teststo generate unit tests for this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
@yoyo837 I removed the uuid package, ready for re-review.
Trace #362
@jon-cullison is attempting to deploy a commit to the React Component Team on Vercel.
A member of the Team first needs to authorize it.
Would be conflicted with https://github.com/react-component/collapse/pull/362
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 99.16%. Comparing base (
129f11f) to head (dbad27a). Report is 1 commits behind head on master.
Additional details and impacted files
@@ Coverage Diff @@
## master #363 +/- ##
==========================================
+ Coverage 99.15% 99.16% +0.01%
==========================================
Files 5 5
Lines 118 120 +2
Branches 43 44 +1
==========================================
+ Hits 117 119 +2
Misses 1 1
: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.