question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Popover] Reposition when rerender

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

If the <Select> component is opened, and we are loading its items dynamically, it’s expected that when the items are finally loaded the <Select>'s opened overlay adjusts itself to the current available space.

Current Behavior

The <Select>'s overlay overflows.

Steps to Reproduce (for bugs)

In the sandbox, click on the <Select> and wait for it to load.

https://codesandbox.io/s/vqvm1704q3

Your Environment

Tech Version
Material-UI 1.0.0-beta.35
React 16.2.0
browser Google Chrome 64.0.3282.186

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

9reactions
oliviertassinaricommented, Sep 23, 2019

@KevinAsher We have added an action prop to the Popover component since to solve the problem. You can trigger the .updatePosition() method when you need to update the position of the Popover (it was added in #9588)

For instance:

function MyPopover() {
  const [itemCount, setItemCount] = React.useState(0);
  const popoverActions = React.useRef();

  React.useEffect(() => {
    setTimeout(() => setItemCount(100), 1000);
  }, []);

  React.useEffect(() => {
    if (popoverActions.current) {
      popoverActions.current.updatePosition();
    }
  }, [itemCount]);

  const items = [];
  for (let i = 0; i < itemCount; i++) {
    items.push(<p key={i}>item</p>);
  }

  return <Popover action={popoverActions}>{items}</Popover>;
}

However, it might require too much boilerplate, a workaround is to trigger a window resize event (don’t abuse it!):

window.dispatchEvent(new CustomEvent('resize'))

In the future, we should consider using the ResizeObserver API for out of the box support:

Alternatively, we can consider updating the position at each render. It could be a low hanging fruit 😃.

5reactions
smsteelcommented, Sep 23, 2019

How to use this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Popover] Reposition when rerender #10595 - mui/material-ui
If the <Select> component is opened, and we are loading its items dynamically, it's expected that when the items are finally loaded the...
Read more >
Bootstrap 4 - how does automatic Popover re-positioning work?
First, click the green button to open the popover. Then use button 2 to move the popover toggle. Now the toggle and the...
Read more >
Popover - Chakra UI
Even though you specified the placement, Popover will try to reposition itself in the event that available space at the specified placement isn't...
Read more >
Popover API - Material UI - MUI
Name Type Default open * bool false action ref anchorEl HTML element | func
Read more >
How to Position a React-Bootstrap Popover - Pluralsight
In this guide, you'll learn how to use a popover component from the ... The placement prop defines the position of your popover....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found