react-spectrum
react-spectrum copied to clipboard
Popover not changing position when triggerRef contents change
Provide a general summary of the issue here
When using Popover by itself with triggerRef, if the bounding box of the trigger changes the popover position does not change.
๐ค Expected Behavior?
The popover should automatically reposition to always be placed correctly when the triggerRef changes
๐ฏ Current Behavior
It does not change position.
๐ Possible Solution
There should be a way to manually trigger a reposition. MutationObserver should be used if it's not already.
๐ฆ Context
No response
๐ฅ๏ธ Steps to Reproduce
https://codesandbox.io/p/sandbox/polished-sun-g6c9lh?file=%2Fsrc%2FApp.js%3A31%2C25
Version
Using React-Aria-Components 1.2.0
What browsers are you seeing the problem on?
Chrome
If other, please specify.
No response
What operating system are you using?
Mac OSX
๐งข Your Company/Team
No response
๐ท Tracking Issue
No response
Maybe we can add the same useResizeOberver() for targetRef as well
https://github.com/adobe/react-spectrum/blob/5cacbdbf88b8c6d7f5d3fb741c2b7ccddf8def7f/packages/%40react-aria/overlays/src/useOverlayPosition.ts#L189-L193
I did a quick test locally, and it seems to work fine (not sure about the full ramifications of adding it though)
https://github.com/adobe/react-spectrum/assets/71210554/1dc00e2e-8f9b-425e-a799-a4881c4e5826
@sookmax That is what MutationObserver was intended to be used for, and I'm frankly surprised this doesn't already handle elements changing. If we're worried about performance, make it an optional prop.
I managed to trigger a reposition myself by toggling the shouldUpdatePosition prop from false and back to true again, which does work but shouldn't be necessary.
Additionally, there should be a way to manually reposition the popover via context or something.
Thank you @sookmax , I don't see an issue adding the useResizeObserver. I doubt that the contents would change frequently enough to warrant a performance risk.
Thanks for bringing this to our attention.
I encountered a similar problem where the popover wouldn't reposition itself when the parent element changed position. As suggested by @eranimo I worked around this by toggling shouldUpdatePosition on the same event that triggers the position change of the parent element.
I can try to reproduce this outside of my application if necessary.