Material-ui: [Popper] Leverage ResizeObserver

Created on 21 Aug 2020  路  6Comments  路  Source: mui-org/material-ui

https://codesandbox.io/s/practical-kalam-jz2ew?file=/src/Demo.js

open the popper by clicking on the button

  1. When scrolling (fast) it does not remain in position but jiggles around
  2. When using "keepMounted" open the popper, close it -> additional white space at bottom of page
  3. When the height of the content inside the popper increases (i.e. because of a form interaction) the position of the popper is not updated until scrolling. After scrolling it jumps up to accommodate for the new height.
Popper enhancement

Most helpful comment

For #1 you can add the following popperOptions to the Popper component:

popperOptions={{
    positionFixed: true,
}}

and this should fix this problem (https://codesandbox.io/s/practical-kalam-jz2ew?file=/src/Demo.js:2167-2232)

All 6 comments

  1. An issue with https://github.com/popperjs/popper-core (but I can't reproduce)
  2. By definition
  3. Interesting. It sounds like we should document how you can use the popperRef to update the position of the positioning or start using the resize observer https://github.com/mui-org/material-ui/issues/9337#issuecomment-413789329

For #1 you can add the following popperOptions to the Popper component:

popperOptions={{
    positionFixed: true,
}}

and this should fix this problem (https://codesandbox.io/s/practical-kalam-jz2ew?file=/src/Demo.js:2167-2232)

For #3 you could also just let the parent Popper component know when it's Child component updates using a callback function - https://codesandbox.io/s/loving-water-b9950?file=/src/Demo.js. You could also just nest the Child component in the Popper component too. Not sure if that would be more efficient than using a Ref but it might be easier.

@mlizchap Thank you for the suggestions. While the first one is really helpful (and should probably be included in the docs) using the second one feels a bit weird - For example I would not expect that I have to pass an update to a Card when it's child is changing. Also in my case it's a bit harder as I deal with a form which updates in multiple occasions not just on a button click.

@oliviertassinari regarding 2. what do you mean by definition? inside the docs it says: "Always keep the children in the DOM." - I would expect that the popper gets hidden simply but not that it adds whitespace at the bottom of the page.. Is there any way to prevent it?

  1. This is a concern for popper, not us, we already mention them in the documentation, it's up to developers to dive deeper.
  2. It's up to developers to hide the popper if it's the behavior they desire.
  3. I agree that asking to rerender is a workaround, not a proper solution.

To clarify the "improvement" I have in mind behind the label: look into using ResizeObserver to avoid having to manually get a ref on popper and update the position as we used to do in the Slider demo or Rating demo.

Thank you for clarification. Wrapping the inner of the popper with
<Box display={open ? "block" : "none"}></Box>
as demonstrated here: https://codesandbox.io/s/nervous-cloud-s2x4p?file=/src/Demo.js:2264-2316 does the job

Was this page helpful?
0 / 5 - 0 ratings

Related issues

newoga picture newoga  路  3Comments

sys13 picture sys13  路  3Comments

reflog picture reflog  路  3Comments

iamzhouyi picture iamzhouyi  路  3Comments

chris-hinds picture chris-hinds  路  3Comments