Skip to content

fix(dialog): cancel the pending lock-style removal when reopening - #4482

Open
lnuxe wants to merge 1 commit into
Tencent:developfrom
lnuxe:fix/dialog-lock-style-reopen
Open

lnuxe wants to merge 1 commit into
Tencent:developfrom
lnuxe:fix/dialog-lock-style-reopen

Conversation

@lnuxe

@lnuxe lnuxe commented Nov 12, 2026

Copy link
Copy Markdown

🤔 这个 PR 的性质是?

  • 日常 bug 修复

🔗 相关 Issue

无(本 PR 直接修复 Dialog 快速重开时的滚动穿透)

📝 描述

useLockStyle 在 Dialog 关闭后延迟 150ms 才移除滚动锁样式,这样离场动画不会被截断。但重新打开时只做了 appendChild,没有取消上一次排队的移除:

if (visible) {
  document.head.appendChild(lockStyleRef.current);
} else {
  clearStyleFunc();
}

所以「关闭 → 150ms 内重新打开」时,旧定时器到点会把正在使用的锁样式摘掉,页面在弹窗打开的状态下可以滚动——滚动穿透。

改动

     if (visible) {
+      // 重新打开时取消上一次关闭排队的移除,否则 150ms 后会把正在使用的锁样式删掉
+      clearTimeout(timerRef.current);
       document.head.appendChild(lockStyleRef.current);
     } else {
       clearStyleFunc();
     }

🔍 自测

修复前(还原 useLockStyle.ts 后跑新测试):

× Dialog > scenarios > lock style > keeps the scroll lock when the dialog reopens within the close delay
  → expected +0 to be 1 // Object.is equality
Tests  1 failed (1)

修复后:

✓ Dialog > scenarios > lock style > keeps the scroll lock when the dialog reopens within the close delay
✓ Dialog > scenarios > lock style > removes the scroll lock after the dialog closes and stays closed
Tests  2 passed (2)

第二个用例确认正常关闭路径仍然会移除锁样式,修复没有把清理一起关掉。

回归:

npx vitest run packages/components/dialog
Test Files  2 passed (2)
     Tests  11 passed (11)

✅ 贡献者检查

  • 我的代码遵循这个项目的代码风格
  • 我已经自己检查了自己的代码
  • 我已经添加了必要的测试
  • 我已经在本地通过了所有测试

Closing a modal queues the lock-style removal on a 150ms timer. Reopening
within that window appended the style again but left the old timer running,
so it fired afterwards and removed the style the open dialog was using -
the background page became scrollable while the dialog was still up.

Clear the pending timer before re-appending.

Verified:
- new test fails on the previous implementation (expected +0 to be 1)
- new test passes with the fix
- dialog: 12 tests pass
@lnuxe
lnuxe force-pushed the fix/dialog-lock-style-reopen branch from 07b793b to acc6e41 Compare November 13, 2026 03:05
@tdesign-bot

Copy link
Copy Markdown
Collaborator

TDesign Component Site Preview Open

Component Preview
tdesign-react 完成
@tdesign-react/chat 完成

@pkg-pr-new

pkg-pr-new Bot commented Nov 13, 2026

Copy link
Copy Markdown
  • tdesign-react-demo

    npm i https://pkg.pr.new/tdesign-react@4482
    
    npm i https://pkg.pr.new/@tdesign-react/chat@4482
    

commit: acc6e41

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.

2 participants