Skip to content

Comments

fix(sidebar): scroll animation not working with sidebarDuration prop#3169

Merged
xiaoyatong merged 1 commit intojdf2e:feat_v3.xfrom
Miles-hxy:fix_sidebarduration
Apr 11, 2025
Merged

fix(sidebar): scroll animation not working with sidebarDuration prop#3169
xiaoyatong merged 1 commit intojdf2e:feat_v3.xfrom
Miles-hxy:fix_sidebarduration

Conversation

@Miles-hxy
Copy link
Collaborator

@Miles-hxy Miles-hxy commented Apr 11, 2025

🤔 这个变动的性质是?

  • 新特性提交
  • 日常 bug 修复
  • 站点、文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • TypeScript 定义更新
  • 包体积优化
  • 性能优化
  • 功能增强
  • 国际化改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他改动(是关于什么的改动?)

🔗 相关 Issue

💡 需求背景和解决方案

☑️ 请求合并前的自查清单

⚠️ 请自检并全部勾选全部选项⚠️

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • fork仓库代码是否为最新避免文件冲突
  • Files changed 没有 package.json lock 等无关文件

Summary by CodeRabbit

  • 重构
    • 改进侧边栏滚动逻辑:在未设置滚动时长时实现即时定位;设置时长后启用平滑过渡动画,提升用户体验。

@coderabbitai
Copy link

coderabbitai bot commented Apr 11, 2025

Walkthrough

此次改动更新了 SideBar 组件中 scrollDirection 函数的动画逻辑。当 sidebarDuration 为 0 时,函数直接将 scrollTop 设置为目标值,实现即时滚动;而当 sidebarDuration 大于 0 时,根据持续时间计算动画帧数,并在每一帧中根据进度插值更新 scrollTop。此外,条件判断也进行了简化,以更清晰地控制动画流程。

Changes

File Change Summary
src/.../sidebar.taro.tsx 更新 scrollDirection 函数:
- 当 sidebarDuration 为 0 时,直接设置 scrollTop 达到目标值。
- 当 sidebarDuration 大于 0 时,计算动画帧数并逐步更新 scrollTop,实现平滑滚动。

Sequence Diagram(s)

sequenceDiagram
    participant User as 用户
    participant SideBar as 侧边栏组件
    participant Animator as 动画处理器

    User->>SideBar: 触发滚动事件
    SideBar->>SideBar: 检查 sidebarDuration
    alt sidebarDuration == 0
        SideBar->>SideBar: 直接设置 scrollTop 为目标值
    else sidebarDuration > 0
        SideBar->>Animator: 计算动画帧数
        loop 每一帧
            Animator->>SideBar: 根据进度更新 scrollTop
        end
    end
    SideBar->>User: 滚动完成
Loading

Possibly related PRs

Suggested reviewers

  • xiaoyatong
  • oasis-cloud

Poem

我是一只快乐的小兔儿,
悄悄蹦跳进代码森林,
滚动逻辑轻盈如风,
每一帧都是跳跃的节拍,
欢庆新变更,乐在其中! 🐇
快来分享这一刻的甜美吧!

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@github-actions github-actions bot added 3.x Target branch 3.x action:review This PR needs more reviews (less than 2 approvals) labels Apr 11, 2025
@codecov
Copy link

codecov bot commented Apr 11, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.19%. Comparing base (cc94ce3) to head (bc3abf5).
Report is 8 commits behind head on feat_v3.x.

Additional details and impacted files
@@              Coverage Diff              @@
##           feat_v3.x    #3169      +/-   ##
=============================================
+ Coverage      87.18%   87.19%   +0.01%     
=============================================
  Files            279      279              
  Lines          18537    18537              
  Branches        2820     2821       +1     
=============================================
+ Hits           16161    16164       +3     
+ Misses          2371     2368       -3     
  Partials           5        5              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/packages/sidebar/sidebar.taro.tsx (1)

113-135: 建议添加对极小 sidebarDuration 值的处理

sidebarDuration 非常小时,frames 可能计算为 0,这会导致 progress = count / frames 出现除零错误。

建议添加安全检查:

const scrollDirection = (to: number) => {
  if (sidebarDuration === 0) {
    setScrollTop(to)
    return
  }

  const from = scrollTop
  const frames = Math.round(sidebarDuration / 16)
+ // 确保至少有一帧动画
+ const safeFrames = Math.max(1, frames)
  let count = 0

  function animate() {
-   const progress = count / frames
+   const progress = count / safeFrames
    const current = from + (to - from) * progress
    setScrollTop(current)

-   if (count < frames) {
+   if (count < safeFrames) {
      count++
      raf(animate)
    }
  }

  animate()
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cc94ce3 and bc3abf5.

📒 Files selected for processing (1)
  • src/packages/sidebar/sidebar.taro.tsx (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: test
🔇 Additional comments (4)
src/packages/sidebar/sidebar.taro.tsx (4)

114-117: 优化了 sidebarDuration 为 0 时的处理逻辑

sidebarDuration 为 0 时,直接设置目标滚动位置并立即返回,避免了不必要的动画帧计算,提高了性能并使行为更符合预期。


119-120: 动画帧数计算逻辑合理

基于 sidebarDuration 计算动画帧数的方法恰当,以 16ms 作为帧率基准符合常见的动画标准。


124-126: 动画过渡计算逻辑清晰

使用线性插值(Linear Interpolation)计算当前滚动位置的方法直观且高效,progress 变量的引入使代码更易读。


128-130: 简化了动画循环条件

将动画继续的条件简化为 count < frames,并在条件内部递增计数器,逻辑更清晰。

@xiaoyatong xiaoyatong merged commit 2c421d0 into jdf2e:feat_v3.x Apr 11, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3.x Target branch 3.x action:review This PR needs more reviews (less than 2 approvals) size/S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants