useAlign reuses a stale flip decision after the viewport changes (popup stays flipped until reopen)
評估
- 難度
- 2/5
- 預估耗時
- 1-3 小時
- 新手友好度
- 30/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 停滯
- 技術堆疊
- react, typescript
- 領域
- frontend
研究方向
Edit src/hooks/useAlign.ts: add prevViewportRef and reset flip cache when clientWidth/clientHeight change, as sketched in the issue. Add a test opening the popup, resizing smaller, resizing back, and asserting it returns to original side. PR #652 is already open against this issue.
由索引模型根據 Issue 內容生成。
描述
What happens
A popup that legitimately flips once stays flipped after the viewport grows back — until it is closed and reopened.
Repro on a real page (antd 6.4.3 → @rc-component/[email protected], plain mouse interaction, no tooling involved), a Select whose trigger sits at y=368 in a 1440×900 viewport:
- open the dropdown → it renders below the trigger (
top: 472px); - resize the window to 1440×450 → it flips above the trigger (
top: 260px) — correct, there is no room below; - resize back to 1440×900 → it stays above (
top: 260px), although 900−400 is plenty for the 104px popup; - close and reopen → it renders below again.
Control: resizing to 1200×320 does not flip the popup (it still fits), and after restoring it stays below — the bug only shows up once a flip has actually been recorded.
Why
useAlign computes the visible region from document.documentElement.clientWidth/clientHeight and memoizes the flip decision in prevFlipRef:
// hooks/useAlign.js
const prevFlipRef = React.useRef({});
const resetFlipCache = () => {
prevFlipRef.current = {};
};
if (!open) {
resetFlipCache();
}
...
if (needAdjustY && popupPoints[0] === 't' &&
(nextPopupBottom > adjustCheckVisibleArea.bottom || prevFlipRef.current.bt)) {
...
prevFlipRef.current.bt = true; // remembered
}
useWatch re-runs onAlign on window resize, so the visible area is recomputed — but prevFlipRef.current.bt/tb/rl/lr survives, so the flip is forced again even though the popup now fits. Only closing the popup (!open) clears it.
Why it is not just a resize edge case
Any temporary viewport rewrite during an open popup hits the same path. Concretely, Chromium rewrites the viewport to 1×1 while taking a full-page screenshot; popups then align against a 1px area, record the flip, and stay off by hundreds of pixels after the viewport is restored. That makes visual-regression runs report phantom changes (we measured 34/300 runs on one scenario). Pinning clientWidth during capture does not help — the layout side still recomputes.
Suggested fix
Track the viewport size used for the last alignment; when it changes, drop the memo before aligning:
const prevViewportRef = React.useRef(null);
...
const { clientWidth, clientHeight, ... } = doc.documentElement;
if (
!prevViewportRef.current ||
prevViewportRef.current.width !== clientWidth ||
prevViewportRef.current.height !== clientHeight
) {
prevViewportRef.current = { width: clientWidth, height: clientHeight };
resetFlipCache();
}
This keeps the memo doing its original job (a popup resize that changes the outcome within the same viewport still reuses the decision, so no back-and-forth flipping), and only forces a recompute when the reference area itself changed — which is exactly the case where the memo is meaningless.
We are carrying this as a local patchedDependencies patch, and would happily send a PR against src/hooks/useAlign.ts with a test (open → resize smaller → resize back → assert the popup is back on its original side) if the approach looks right.
- 主要語言
- TypeScript
- 星號
- 392
- 分支
- 250
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
react-component/trigger 的其他 Issue
-
難度 2/5 1-3 小時 新手友好度 65/100
react-component/trigger#593 · 1 個 reaction ·
-
Does not work when rendered inside an iframe可能已有人在做 @PinkChampagne17 於 185 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 58/100
react-component/trigger#623 ·
-
useAlign.js: inline style reset to 0 is silently overridden under prefers-reduced-motion CSS (CSSTransition interpolation)可能已有人在做 @baofuen 於 144 天前認領。 未關閉
難度 3/5 1-2 天 新手友好度 65/100
react-component/trigger#618 · 1 則留言 ·
-
`collectScroller` is not working as expected when the element is in shadow DOM可能已有人在做 關聯的 PR 仍在進行中或已合併。 未關閉
難度 3/5 1-2 天 新手友好度 42/100
react-component/trigger#555 ·
-
難度 3/5 1-2 天 新手友好度 55/100
react-component/trigger#550 · 1 則留言 ·
查看 react-component/trigger 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 68/100
Chocobozzz/PeerTube#7901 ·
維護者通常 3 天內回覆
-
area:tools bug has repro
難度 2/5 1-3 小時 新手友好度 86/100
anthropics/claude-code#113984 ·
-
難度 2/5 1-3 小時 新手友好度 72/100
ehmpathy/declapract-typescript-ehmpathy#960 ·
維護者通常 1 天內回覆
-
area/web platform/windows type/bug version/v2
難度 2/5 1-3 小時 新手友好度 65/100
anomalyco/opencode#68237 · 1 則留言 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 85/100