collapse icon indicating copy to clipboard operation
collapse copied to clipboard

refactor: 重构collapse为details实现

Open IVLIU opened this issue 11 months ago • 15 comments

关联antd https://github.com/ant-design/ant-design/issues/48974

Summary by CodeRabbit

  • 新功能
    • 折叠面板组件现采用原生 HTML <details><summary> 结构,提升可访问性与语义化。
    • 针对支持的浏览器,折叠内容区域新增原生动画效果,展开与收起更流畅。
  • 样式
    • 折叠面板内容区域新增动画和过渡效果,视觉体验优化。
  • 优化
    • 提升了折叠面板的渲染一致性和性能,部分浏览器下动画表现更佳。
    • 点击折叠项时状态更新采用了过渡调度,提升响应体验。
    • 简化了面板内容渲染逻辑,避免不必要的状态管理。

IVLIU avatar Mar 17 '25 14:03 IVLIU

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

vercel[bot] avatar Mar 17 '25 14:03 vercel[bot]

"""

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 改为 HTMLDetailsElementstyle 类型更精确,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.

❤️ Share
🪧 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 @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in 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 pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file 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.

coderabbitai[bot] avatar Mar 17 '25 14:03 coderabbitai[bot]

ci 挂了

afc163 avatar Mar 19 '25 02:03 afc163

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.

codecov[bot] avatar Mar 19 '25 04:03 codecov[bot]

ci 挂了

fixed 少个包

IVLIU avatar Mar 19 '25 04:03 IVLIU

收起动画丢了。

afc163 avatar Mar 19 '25 04:03 afc163

收起动画丢了。

我在想有没有必要开个discussion讨论下这个问题 改造前开闭实现其实是利用content的height值变化来实现的,本质上是没有open状态的 改造后基于details的实现其实是有open状态的,即content的可见性是有open决定的,而不是content的height

因此改造后的渲染流程如下

open = true时,content展现,然后高度过渡 open = false时,content隐藏,此时动画不可见

所以我觉得需要讨论下这部分的实现,我先说下我的想法

  1. 渐进式增强,对于高版本浏览器,移除CSSMotion,使用::details-content实现展开关闭动画,对于不支持的,使用CSSMotion实现展开动画
  2. 内部再维护一个动画状态,拦截details的点击事件,优先改变动画状态,待动画执行完毕再改变open状态

个人上是更倾向方式1,这样的话,在高版本浏览器中,组件将变得足够简单,性能也将更上一层楼,仅做状态同步即可

IVLIU avatar Mar 19 '25 10:03 IVLIU

1 可以的,我也倾向渐进式。

afc163 avatar Mar 19 '25 12:03 afc163

1 可以的,我也倾向渐进式。

image 新增渐进式处理,为false则回退至CSSMotion,但这里存在一个问题,走服务端渲染时,会存在两端不一致的问题,有什么建议?

image 动画实现可以参考

IVLIU avatar May 10 '25 03:05 IVLIU

或许可以移除CSSMotion,根据是否支持::details-content伪元素将motionName设置到details元素或者<PanelContent />

IVLIU avatar May 10 '25 04:05 IVLIU

有冲突

afc163 avatar May 20 '25 03:05 afc163

@IVLIU is attempting to deploy a commit to the React Component Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar May 20 '25 11:05 vercel[bot]

有冲突

fixed

IVLIU avatar May 20 '25 12:05 IVLIU

  • [ ] ci 挂了,看看
  • [ ] 收起的时候报错。 图片

afc163 avatar May 20 '25 15:05 afc163

ping @IVLIU

afc163 avatar Jun 11 '25 02:06 afc163