Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this Material-UI Grid item move when I click on it?

I am developing a React app using Material UI. I am having an issue with one page of the app.

That page contains some text and a button, which are vertically aligned with one another. It also contains a second piece of text and a second button, which are vertically aligned with one another and which are initially hidden. The first button can be used to reveal the second piece of text and the second button. The second button can be used to conceal the second piece of text and itself (i.e. the second button).

Here's a link to a screenshot of the page with the initially-hidden content visible (I don't have enough StackOverflow rep points to embed an image yet).

react-material-ui-app-page-screenshot

The issue I'm having is:

When I click the first button (i.e. the "SHOW" button), then click the second button (i.e. the "HIDE" button), then click the first button again; the first button jumps across the page. More specifically, it goes from having its right edge touch the right edge of the <body> element, to having its left edge touch the right edge of the preceding piece of text.

Once the button has jumped, when I resize the browser window (e.g. by clicking and dragging the Chrome application window), the button returns to its original location.

Note: The jumping happens in Chrome, but not in Firefox.

Do any of you know why that is happening?

I created a demo of this phenomenon on CodeSandbox. Here's a link to that demo:

  • https://codesandbox.io/s/mui-hidden-grid-item-jump-4wsft
like image 305
ongobongo Avatar asked Oct 17 '19 21:10

ongobongo


People also ask

Does material UI grid use Flexbox?

Material UI Grid's grid uses Flexbox under the hood. The properties of Flexbox are used as properties of the Grid, so you can control the component as if it were a flexbox. Additionally, Material UI's Grid also provides helpers for spacing , responsive design, and fitting a 12 column layout.

What does material UI's grid do?

Additionally, Material UI's Grid also provides helpers for spacing, responsive design, and fitting a 12 column layout. To see all the properites of Grid, check out the Grid API or look at the bottom of the Grid source code to see prop types.

What is the grid component in Material Design?

The gridcreates visual consistency between layouts while allowing flexibility across a wide variety of designs. Material Design's responsive UI is based on a 12-column grid layout. Feedback Bundle size Material Design ⚠️ The Gridcomponent shouldn't be confused with a data grid; it is closer to a layout grid.

How do I align items in material-UI grid?

This guide focuses on aligning items in every conceivable way in Material-UI Grid: aligning right, left, and horizontally centered; aligning top, bottom, and vertically centered. In this example I will use the Material-UI Grid, but the alignment principles apply for aligning all MUI components.


1 Answers

Upon a closer look, it appears to have to do with having flex-wrap: wrap on a display: flex; flex-direction: column;.

wrap={"nowrap"}

...fixes it.

See it here.

It's difficult to say if it's a bug or not. What should we expect from a flex column container with flex-wrap:wrap and without a set height, in terms of column width?
Because the correct behavior might be to shrink the column as much as possible.

The bug is that it renders differently in different contexts. Whatever the correct behavior is, it should be consistent.

like image 199
tao Avatar answered Sep 28 '22 11:09

tao