refactor: 重构collapse为details实现
关联antd https://github.com/ant-design/ant-design/issues/48974
Summary by CodeRabbit
-
新功能
- 折叠面板组件现采用原生 HTML
<details>和<summary>结构,提升可访问性与语义化。 - 针对支持的浏览器,折叠内容区域新增原生动画效果,展开与收起更流畅。
- 折叠面板组件现采用原生 HTML
-
样式
- 折叠面板内容区域新增动画和过渡效果,视觉体验优化。
-
优化
- 提升了折叠面板的渲染一致性和性能,部分浏览器下动画表现更佳。
- 点击折叠项时状态更新采用了过渡调度,提升响应体验。
- 简化了面板内容渲染逻辑,避免不必要的状态管理。
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 16, 2025 0:43am |
"""
Walkthrough
本次变更将折叠面板组件从基于 <div> 的实现迁移到更具语义的原生 <details>/<summary> 元素。相关类型定义、事件处理和样式均做了适配。内容动画根据浏览器支持情况选择原生或 JS 动画。部分状态逻辑也做了简化和优化。
Changes
| 文件/路径 | 变更摘要 |
|---|---|
| src/Collapse.tsx | 用 React.startTransition 包裹 setActiveKey,优化状态与 <details> 的同步,解决 React 状态与原生 open 属性的冲突。 |
| src/Panel.tsx | 根节点由 <div> 改为 <details>,header 用 <summary>,ref 类型和事件类型调整,点击事件阻止默认及冒泡,motionName 直接加到 <details>,保持动画逻辑。 |
| src/interface.ts | 类型定义从 HTMLDivElement 改为 HTMLDetailsElement,style 类型更精确,props 继承关系调整。 |
| assets/index.less | 针对 .rc-collapse-item 增加原生 <details> 动画相关样式,支持 ::details-content 伪元素动画,添加注释的视图转换动画规则。 |
| src/PanelContent.tsx | 移除 rendered 的 state,改为直接用表达式推导,渲染时通过 display 控制显示隐藏,简化渲染逻辑。 |
| tests/index.spec.tsx | 简化或移除部分关于折叠面板激活/非激活状态的 DOM 断言,聚焦面板存在性和激活状态的判断。 |
| docs/examples/_util/motionUtil.ts | getCurrentHeight 函数改用安全访问,避免 node 为空时报错,返回高度默认 0。 |
Sequence Diagram(s)
sequenceDiagram
participant User
participant Collapse
participant Panel (details)
participant PanelContent
User->>Collapse: 点击某面板
Collapse->>Collapse: React.startTransition(setActiveKey)
Collapse->>Panel: 传递 isActive, open
Panel->>Panel: 渲染 <details open={isActive}>
Panel->>PanelContent: 传递 isActive, forceRender
PanelContent->>PanelContent: 判断 isActive || forceRender
PanelContent-->>Panel: 渲染内容(动画或直接显示)
Poem
🐰
换 div 为 details,语义更美妙,
动画新姿态,CSS 也来跳。
类型精细化,代码更可靠,
折叠展开间,体验步步高!
📂✨🐇
"""
[!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.
📜 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 dba7f6407f2ef6cd8342e72a0b45eac8ba675812 and 5ed08070bd13a45b79ad3fc75e40d91bc3e848ad.
📒 Files selected for processing (1)
-
src/PanelContent.tsx(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- src/PanelContent.tsx
✨ Finishing Touches
- [ ] 📝 Generate Docstrings
🧪 Generate Unit Tests
- [ ] Create PR with Unit Tests
- [ ] Commit Unit Tests in branch
refactor/native-details-element - [ ] 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.
ci 挂了
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 100.00%. Comparing base (
129f11f) to head (5ed0807).
Additional details and impacted files
@@ Coverage Diff @@
## master #362 +/- ##
===========================================
+ Coverage 99.15% 100.00% +0.84%
===========================================
Files 5 5
Lines 118 118
Branches 43 42 -1
===========================================
+ Hits 117 118 +1
+ Misses 1 0 -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.
ci 挂了
fixed 少个包
收起动画丢了。
收起动画丢了。
我在想有没有必要开个discussion讨论下这个问题
改造前开闭实现其实是利用content的height值变化来实现的,本质上是没有open状态的
改造后基于details的实现其实是有open状态的,即content的可见性是有open决定的,而不是content的height值
因此改造后的渲染流程如下
open = true时,content展现,然后高度过渡 open = false时,content隐藏,此时动画不可见
所以我觉得需要讨论下这部分的实现,我先说下我的想法
- 渐进式增强,对于高版本浏览器,移除
CSSMotion,使用::details-content实现展开关闭动画,对于不支持的,使用CSSMotion实现展开动画 - 内部再维护一个动画状态,拦截details的点击事件,优先改变动画状态,待动画执行完毕再改变open状态
个人上是更倾向方式1,这样的话,在高版本浏览器中,组件将变得足够简单,性能也将更上一层楼,仅做状态同步即可
1 可以的,我也倾向渐进式。
1 可以的,我也倾向渐进式。
新增渐进式处理,为false则回退至CSSMotion,但这里存在一个问题,走服务端渲染时,会存在两端不一致的问题,有什么建议?
动画实现可以参考
或许可以移除CSSMotion,根据是否支持::details-content伪元素将motionName设置到details元素或者<PanelContent />上
有冲突
@IVLIU is attempting to deploy a commit to the React Component Team on Vercel.
A member of the Team first needs to authorize it.
有冲突
fixed
- [ ] ci 挂了,看看
- [ ] 收起的时候报错。
ping @IVLIU