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

Popover not changing position when triggerRef contents change

Open eranimo opened this issue 1 year ago โ€ข 3 comments

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

eranimo avatar May 14 '24 16:05 eranimo

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 avatar May 15 '24 14:05 sookmax

@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.

eranimo avatar May 15 '24 18:05 eranimo

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.

snowystinger avatar May 15 '24 20:05 snowystinger

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.

modiho avatar May 21 '24 14:05 modiho