-
Notifications
You must be signed in to change notification settings - Fork 266
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(popup): 增加禁止 page 滚动的例子 #2803
Conversation
Walkthrough此次更改为 Changes
Possibly related PRs
Suggested labels
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## next #2803 +/- ##
=======================================
Coverage 84.07% 84.07%
=======================================
Files 217 217
Lines 17830 17830
Branches 2609 2609
=======================================
Hits 14991 14991
Misses 2834 2834
Partials 5 5 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (2)
src/packages/popup/demos/taro/demo8.tsx (1)
16-28
: 建议提取环境检查逻辑为了提高代码的可维护性和复用性,建议将微信环境检查和样式设置逻辑提取到独立的 hook 中。
建议创建一个自定义 hook:
const usePreventScroll = () => { const isWeapp = getEnv().toLowerCase() === 'weapp' const preventScroll = () => { if (!isWeapp) return wx.setPageStyle({ style: { overflow: 'hidden' }, fail: (error) => { console.error('设置页面样式失败:', error) } }) } const restoreScroll = () => { if (!isWeapp) return wx.setPageStyle({ style: { overflow: 'visible' }, fail: (error) => { console.error('恢复页面样式失败:', error) } }) } return { preventScroll, restoreScroll } }然后在组件中使用:
const Demo8 = () => { const [scrollPenetration, setScrollPenetration] = useState(false) + const { preventScroll, restoreScroll } = usePreventScroll() return ( <Popup visible={scrollPenetration} position="bottom" lockScroll - onOpen={() => { - Taro.getEnv().toLowerCase() === 'weapp' && - wx.setPageStyle({ - complete: console.log, - style: { overflow: 'hidden' }, - }) - }} + onOpen={preventScroll} + onClose={restoreScroll} >🧰 Tools
🪛 GitHub Check: build
[failure] 23-23:
Cannot find name 'wx'.src/packages/popup/doc.taro.md (1)
83-84
: 文档补充合理且有价值!新增的说明很好地解释了如何在不同环境下处理页面滚动问题。建议补充一个具体的代码示例,以便开发者更好地理解实现方式。
建议在文档中添加以下示例代码:
const handleOpen = () => { if (process.env.TARO_ENV === 'weapp') { wx.setPageStyle({ style: { overflow: 'hidden', }, }) } }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
src/packages/popup/demos/taro/demo8.tsx
(1 hunks)src/packages/popup/doc.taro.md
(1 hunks)
🧰 Additional context used
🪛 GitHub Check: build
src/packages/popup/demos/taro/demo8.tsx
[failure] 23-23:
Cannot find name 'wx'.
// @ts-ignore | ||
Taro.getEnv().toLowerCase() === 'weapp' && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要添加微信小程序的类型定义
建议不要使用 @ts-ignore
来绕过类型检查。相反,应该正确引入微信小程序的类型定义。
建议添加以下类型定义:
+import { getEnv } from '@tarojs/taro'
+import { wx } from '@tarojs/taro/types/wx/index'
然后移除 @ts-ignore
注释并使用导入的类型:
- // @ts-ignore
- Taro.getEnv().toLowerCase() === 'weapp' &&
+ getEnv().toLowerCase() === 'weapp' &&
Committable suggestion skipped: line range outside the PR's diff.
wx.setPageStyle({ | ||
complete: console.log, | ||
style: { overflow: 'hidden' }, | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
优化错误处理和清理逻辑
当前实现存在以下问题:
- 没有处理
setPageStyle
可能的失败情况 - 在弹窗关闭时没有恢复页面样式
- 在生产环境中使用了
console.log
建议按照以下方式优化实现:
<Popup
visible={scrollPenetration}
position="bottom"
lockScroll
onOpen={() => {
- Taro.getEnv().toLowerCase() === 'weapp' &&
- wx.setPageStyle({
- complete: console.log,
- style: { overflow: 'hidden' },
- })
+ if (getEnv().toLowerCase() === 'weapp') {
+ wx.setPageStyle({
+ style: { overflow: 'hidden' },
+ success: () => {
+ console.debug('页面样式设置成功')
+ },
+ fail: (error) => {
+ console.error('设置页面样式失败:', error)
+ }
+ })
+ }
}}
+ onClose={() => {
+ if (getEnv().toLowerCase() === 'weapp') {
+ wx.setPageStyle({
+ style: { overflow: 'visible' },
+ fail: (error) => {
+ console.error('恢复页面样式失败:', error)
+ }
+ })
+ }
+ }}
>
📝 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.
wx.setPageStyle({ | |
complete: console.log, | |
style: { overflow: 'hidden' }, | |
}) | |
<Popup | |
visible={scrollPenetration} | |
position="bottom" | |
lockScroll | |
onOpen={() => { | |
if (getEnv().toLowerCase() === 'weapp') { | |
wx.setPageStyle({ | |
style: { overflow: 'hidden' }, | |
success: () => { | |
console.debug('页面样式设置成功') | |
}, | |
fail: (error) => { | |
console.error('设置页面样式失败:', error) | |
} | |
}) | |
} | |
}} | |
onClose={() => { | |
if (getEnv().toLowerCase() === 'weapp') { | |
wx.setPageStyle({ | |
style: { overflow: 'visible' }, | |
fail: (error) => { | |
console.error('恢复页面样式失败:', error) | |
} | |
}) | |
} | |
}} | |
> |
🧰 Tools
🪛 GitHub Check: build
[failure] 23-23:
Cannot find name 'wx'.
🤔 这个变动的性质是?
🔗 相关 Issue
💡 需求背景和解决方案
☑️ 请求合并前的自查清单
Summary by CodeRabbit
新功能
Popup
组件中添加了onOpen
事件处理程序,以改善在特定环境(如微信小程序)中的弹出行为,防止页面滚动。文档
Popup
组件的文档,增加了关于弹出时处理滚动行为的说明,强调了lockScroll
属性和catchMove
方法的使用。