Skip to content

fix(popover): clamp the bubble to the right screen edge as well - #4737

Open
lnuxe wants to merge 1 commit into
Tencent:developfrom
lnuxe:fix/popover-clamp-right-edge
Open

lnuxe wants to merge 1 commit into
Tencent:developfrom
lnuxe:fix/popover-clamp-right-edge

Conversation

@lnuxe

@lnuxe lnuxe commented Nov 23, 2026

Copy link
Copy Markdown

🤔 这个 PR 的性质是?

  • 日常 bug 修复
  • 新特性提交
  • 文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • CI/CD 改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他

🔗 相关 Issue

Fixes #4197

💡 需求背景和解决方案

问题:气泡超出左侧屏幕时会紧靠左侧(Math.max(left, 0)),但超出右侧时没有对应处理,气泡会跑到屏幕外。

calcPlacement 只能在「对侧放得下」时翻转 placement(triggerLeft - contentWidth >= 0 之类)。当一个气泡比触发元素宽很多、又居中对齐触发元素时,两侧都放不下,翻转也就无从谈起,最终直接越出右边缘。

修复前的最终定位:

const style = `top:${Math.max(top, 0)}px;left:${Math.max(left, 0)}px;`;

只有下界,没有上界。

解决方案:把横向夹取抽成纯函数 clampHorizontal(left, contentWidth, windowWidth),用 windowWidth - contentWidth 作为上界:

export default function clampHorizontal(left: number, contentWidth: number, windowWidth: number): number {
  const maxLeft = Math.max(windowWidth - contentWidth, 0);
  return Math.min(Math.max(left, 0), maxLeft);
}
  • 左越界 → 贴左边缘(保持原行为)
  • 右越界 → 贴右边缘(本次修复)
  • 内容比屏幕还宽 → 固定在左边缘,保证开头部分可见,而不是被推到负坐标完全看不见

纵向没有一起改:竖向越界由 calcPlacement 翻转 placement 处理,本次只补齐 issue 指出的横向缺口,避免影响现有纵向布局。

回归测试(packages/components/popover/__test__/utils.test.js):

场景 期望 修复前
本来就在屏幕内 (100, 50, 375) 100 100
左越界 (-20, 100, 375) 0 0
右越界 (360, 100, 375) 275 360 ✗
超宽 (50, 500, 375) 0 50 ✗

修复前:Tests: 2 failed, 2 passed(Expected: 275 / Received: 360,Expected: 0 / Received: 50);修复后:Tests: 4 passed。

全量单测:147 suites / 883 tests / 410 snapshots 全部通过;eslint 干净。

📝 更新日志

  • 本条 PR 不需要纳入 Changelog

tdesign-miniprogram

  • fix(popover): 气泡超出右侧屏幕时改为贴住右边缘,不再跑到屏幕外

The final position was clamped with Math.max(top, 0) / Math.max(left, 0)
only, so a bubble that overflowed the right edge stayed off-screen while
the left edge was already handled. calcPlacement can only flip the
placement when the opposite side fits, so a wide bubble centred on its
trigger still ran off the right edge.

Extract the horizontal clamp into clampHorizontal and bound it by
windowWidth - contentWidth. Content wider than the viewport is pinned to
the left edge so its beginning stays visible.

Fixes Tencent#4197
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.

[t-popover] 气泡支持了不超出左侧屏幕,但没有支持不超出右侧屏幕

1 participant