react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

Menu Popover slide transition looks inaccurate when popover is flipped

Open phuoccss opened this issue 6 months ago โ€ข 4 comments

Provide a general summary of the issue here

Click the Menu button here when it's at bottom of the page https://react-aria.adobe.com/Menu#content. The transition looks wrong. It does not happen with Popover and Select.

๐Ÿค” Expected Behavior?

Slide transition works correctly.

๐Ÿ˜ฏ Current Behavior

Not sure. Seems like there's a delay when setting the data-placement attribute. When clicking the Menu button repeatedly, the slide transition looks accurate.

๐Ÿ’ Possible Solution

No response

๐Ÿ”ฆ Context

No response

๐Ÿ–ฅ๏ธ Steps to Reproduce

Follow official example.

Version

1.18

What browsers are you seeing the problem on?

Chrome

If other, please specify.

No response

What operating system are you using?

Windows 11

๐Ÿงข Your Company/Team

No response

๐Ÿ•ท Tracking Issue

No response

phuoccss avatar Jun 02 '26 19:06 phuoccss

Looks like it is present in our Spectrum 2 Menu as well, but strangely enough it doesn't reproduce if the trigger is extremely close to the bottom of the page. I'll have to dig some more, but bisecting to last December shows its been happening since then.

LFDanLu avatar Jun 03 '26 17:06 LFDanLu

Looks like:

  1. The enter animation starts when placement is available:

https://github.com/adobe/react-spectrum/blob/1e5a26596464ae9a5cd9fd60d211f20a955189a1/packages/react-aria-components/src/Popover.tsx#L237

  1. Menu builds its collection after the popover opens:

https://github.com/adobe/react-spectrum/blob/1e5a26596464ae9a5cd9fd60d211f20a955189a1/packages/react-aria-components/src/Menu.tsx#L300-L302

  1. Placement is computed before the menu reaches full height, then recalculated:

https://github.com/adobe/react-spectrum/blob/1e5a26596464ae9a5cd9fd60d211f20a955189a1/packages/react-aria/src/overlays/useOverlayPosition.ts#L317-L321

Maybe we could have MenuTrigger pre-build the collection (like in Select/Combobox), pass it via context, and have the Menu use the pre-built one if available.

reidbarber avatar Jun 05 '26 15:06 reidbarber

We hit this in production and traced it to a deterministic mechanism rather than a rendering delay. Verified against [email protected] / [email protected] by reading the shipped source and instrumenting getAnimations() frame by frame.

TL;DR: when a menu's placement flips after its collection populates, the flip cancels the in-flight entry animation, and useAnimation treats the settled (cancelled) animation as the entry having finished โ€” it clears data-entering, which also kills the replacement animation. The menu snaps open.

Step by step:

  1. Popover gates the entering flag on positioning: useEnterAnimation(popoverRef, !!placement). Once placement is computed, data-entering and data-placement commit together.
  2. useAnimation (animation.ts) then does a one-shot check:
    let animations = ref.current.getAnimations();
    if (animations.length === 0) { onEnd(); return; }
    Promise.allSettled(animations.map(a => a.finished)).then(() => flushSync(() => onEnd()));
    
    Promise.allSettled settles for cancelled animations (their finished promise rejects) exactly as for finished ones.
  3. RAC collections render empty first and populate on a second pass โ€” useOverlayPosition even compensates for this ("โ€ฆso that we can compensate for RAC collections populating after a second render"). A trigger near the bottom of the viewport therefore measures the empty popover as fitting below (data-placement="bottom"), then flips to "top" when the items land and the overlay resizes.
  4. With entry animations keyed on [data-entering][data-placement^=โ€ฆ] (the common styling pattern), the flip switches animation-name mid-entry. The first animation is cancelled โ†’ allSettled resolves โ†’ flushSync(onEnd) removes data-entering โ†’ the just-started replacement animation no longer matches any rule and is cancelled too. Net result: a snap, usually within a frame or two.

This explains both observations in the report:

  • "When clicking the Menu button repeatedly, the slide transition looks accurate" โ€” on warm opens the collection is cached, so the first measurement is full-size, placement is top from the start, nothing flips, and the single animation runs to completion.
  • It only affects components whose placement flips between the two collection passes; anything whose first-pass measurement already agrees with the final placement animates fine.

Workarounds: declare the accurate placement (e.g. placement="top end" for a bottom-pinned trigger) so no flip occurs; or key entry animations on mount instead of data-entering.

Possible fix: in useAnimation, don't treat cancelled animations as entry completion โ€” e.g. after allSettled, re-check getAnimations() and keep waiting if a replacement animation is running.

RobHannay avatar Jul 20 '26 09:07 RobHannay

Just a note. Tooltip has the same buggy behavior.

phuoccss avatar Aug 19 '26 11:08 phuoccss