Menu Popover slide transition looks inaccurate when popover is flipped
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
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.
Looks like:
- The enter animation starts when placement is available:
https://github.com/adobe/react-spectrum/blob/1e5a26596464ae9a5cd9fd60d211f20a955189a1/packages/react-aria-components/src/Popover.tsx#L237
- 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
- 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.
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:
Popovergates the entering flag on positioning:useEnterAnimation(popoverRef, !!placement). Once placement is computed,data-enteringanddata-placementcommit together.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.allSettledsettles for cancelled animations (theirfinishedpromise rejects) exactly as for finished ones.- RAC collections render empty first and populate on a second pass โ
useOverlayPositioneven 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. - With entry animations keyed on
[data-entering][data-placement^=โฆ](the common styling pattern), the flip switchesanimation-namemid-entry. The first animation is cancelled โallSettledresolves โflushSync(onEnd)removesdata-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
topfrom 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.
Just a note. Tooltip has the same buggy behavior.