useAlign reuses a stale flip decision after the viewport changes (popup stays flipped until reopen)

Ouverte
#651 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

@SXongin y travaille déjà.

Depuis le 31/10/2026.

  • #652 par @SXongin — ouverte

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
30/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
À l'abandon
Stack technique
react, typescript
Domaine
frontend

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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:

  1. open the dropdown → it renders below the trigger (top: 472px);
  2. resize the window to 1440×450 → it flips above the trigger (top: 260px) — correct, there is no room below;
  3. resize back to 1440×900 → it stays above (top: 260px), although 900−400 is plenty for the 104px popup;
  4. 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.

Langage dominant
TypeScript
Étoiles
392
Forks
250
Métriques de merge des PR
Aucune PR mergée en 30 j

Préparer son environnement

Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de react-component/trigger

Toutes les issues de react-component/trigger

Issues similaires

Plus d'issues TypeScript

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.