Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 添加 passive 事件监听器以提高页面滚动性能 #688

Conversation

ketao4869
Copy link

@ketao4869 ketao4869 commented Oct 31, 2024

  • 将 touchstart 和 touchmove 事件监听器设置为 passive,提升页面滚动时的响应速度
  • 修复了控制台中出现的非 passive 事件监听器警告

🤔 这个 PR 的性质是?(至少选择一个)

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

🔗 相关 Issue

💡 需求背景和解决方案

image

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

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

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充

Summary by CodeRabbit

  • 新功能
    • 更新了触摸事件的处理方式,可能提高了性能。
  • 修复
    • 优化了滚动行为,改善用户体验。

- 将 touchstart 和 touchmove 事件监听器设置为 passive,提升页面滚动时的响应速度
- 修复了控制台中出现的非 passive 事件监听器警告
Copy link

vercel bot commented Oct 31, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
wot-design-uni ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 31, 2024 2:20am

Copy link

coderabbitai bot commented Oct 31, 2024

Walkthrough

此次更改主要集中在 wd-index-bar.vue 组件的事件处理上,特别是对触摸事件的处理进行了修改。将 @touchstart@touchmove 事件监听器更新为使用 .passive 修饰符,从而优化了浏览器的滚动行为。除了这些触摸事件的调整外,组件的整体结构和功能保持不变,没有其他显著的逻辑或错误处理更改。

Changes

文件路径 更改摘要
src/uni_modules/wot-design-uni/components/wd-index-bar/wd-index-bar.vue 更新触摸事件监听器,使用 .passive 修饰符,优化事件处理。

Possibly related PRs

Suggested reviewers

  • Moonofweisheng

Poem

在触摸的世界里,兔子欢跃,
事件处理更聪明,性能飞跃。
轻轻一触,滚动如风,
兔子们欢呼,乐在其中! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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 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.

Copy link

netlify bot commented Oct 31, 2024

Deploy Preview for wot-design-uni ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 6581208
🔍 Latest deploy log https://app.netlify.com/sites/wot-design-uni/deploys/6722e91a6b6d68000897baad
😎 Deploy Preview https://deploy-preview-688--wot-design-uni.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1e8de54 and 6581208.

📒 Files selected for processing (1)
  • src/uni_modules/wot-design-uni/components/wd-index-bar/wd-index-bar.vue (1 hunks)
🔇 Additional comments (1)
src/uni_modules/wot-design-uni/components/wd-index-bar/wd-index-bar.vue (1)

11-12: 建议:验证滚动行为

由于该组件实现了自定义的索引栏滚动行为,在修改事件监听器修饰符时需要特别注意:

  • 如果移除 .prevent,可能会导致页面产生意外的滚动行为
  • 如果保留 .passive,可能会影响组件的滚动控制功能

建议在以下场景下测试组件的滚动行为:

  1. 快速滑动索引栏
  2. 在内容区域滚动
  3. 同时触发多个触摸事件
✅ Verification successful

事件修饰符使用正确,无需修改

根据代码分析,当前的事件修饰符组合是合理的:

  • .prevent 修饰符的使用是必要的,因为组件需要完全控制滚动行为
  • .passive.prevent 的组合在 uni-app 环境中是有效的,不会产生冲突
  • 其他类似的可拖动组件(如 wd-floating-panel、wd-fab)也采用了类似的事件处理方式
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 搜索相关的测试用例
rg -t vue "wd-index-bar.*test" 

Length of output: 60


Script:

#!/bin/bash
# Search for test files related to index-bar
fd -e spec.js -e test.js "index-bar"

# Search for the component implementation to understand scroll behavior
rg -g "*.vue" "handleTouch(Start|Move)" -A 5

# Look for similar scroll behavior implementations in other components
ast-grep --pattern 'touchmove.stop.passive.prevent'

Length of output: 6548

Comment on lines +11 to +12
@touchstart.stop.passive.prevent="handleTouchStart"
@touchmove.stop.passive.prevent="handleTouchMove"
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

注意:.passive.prevent 修饰符的冲突问题

当前的事件监听器同时使用了 .passive.prevent 修饰符,这两个修饰符是互相矛盾的:

  • .passive 告诉浏览器事件处理程序不会调用 preventDefault()
  • .prevent 会调用 preventDefault()
  • 这种组合会导致 .passive 失去提升性能的作用

建议根据实际需求选择其中之一:

如果需要阻止默认行为:

-@touchstart.stop.passive.prevent="handleTouchStart"
-@touchmove.stop.passive.prevent="handleTouchMove"
+@touchstart.stop.prevent="handleTouchStart"
+@touchmove.stop.prevent="handleTouchMove"

如果想提升滚动性能:

-@touchstart.stop.passive.prevent="handleTouchStart"
-@touchmove.stop.passive.prevent="handleTouchMove"
+@touchstart.stop.passive="handleTouchStart"
+@touchmove.stop.passive="handleTouchMove"
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@touchstart.stop.passive.prevent="handleTouchStart"
@touchmove.stop.passive.prevent="handleTouchMove"
@touchstart.stop.prevent="handleTouchStart"
@touchmove.stop.prevent="handleTouchMove"
```
Option 2 (if improving scroll performance is needed):
```suggestion
@touchstart.stop.passive="handleTouchStart"
@touchmove.stop.passive="handleTouchMove"

@ketao4869 ketao4869 closed this Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant