-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Grid2] Replace context with cloneElement
#36399
Conversation
Netlify deploy preview@material-ui/core: parsed: +0.06% , gzip: +0.06% Bundle size report |
Hey @siriwatknp, thanks for looking into this!
Thanks again! |
Is having no padding and spacing the intended way ? When do you think it will be fixed ? |
@omermizr It looks all correct to me. (1) The usage is wrong. Grid containers and items should be used together, otherwise, there is no point in using the Grid component. We could make the docs more explicit on this point in https://mui.com/material-ui/react-grid2/#basic-grid. (2, 3) Those padding is correct. The goal of the grid is to create consistent spacing between grid items. If you take a look at this https://codesandbox.io/s/material-cra-ts-forked-01y0gm?file=/src/App.tsx, you will see that the spacing between grid items is |
I guess you meant to use CSS flexbox It is definitely better to have Flexbox Grid using |
* <Grid> // level 1 | ||
* ``` | ||
*/ | ||
level?: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the motivation of making this a public API? If we don't want it to be public, maybe we should prefix it with internal_
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping on this question, it's still not clear to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I will make it unstable_
for consistency.
/> | ||
> | ||
{React.Children.map(children, (child) => { | ||
if (React.isValidElement(child) && isMuiElement(child, ['Grid'])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work if the Grid
is styled? Should we add a test for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Good point. Visual regression added.
Thanks @siriwatknp! |
…nested-grid-context
Honestly, I don't understand your point on What do you have to change? because if I remove the background from your sandbox, the spacing looks correct https://codesandbox.io/s/material-cra-ts-forked-jcio79?file=/src/App.tsx. If you really want to add backgrounds, you should add them to grid items (not containers).
My bad, it is not between grid items but between grid items' content. I hope this picture is clearer. One thing I notice in all of your examples is |
if (tag.muiName) { | ||
Component.muiName = tag.muiName; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mnajdova This is required to make styled(Grid2)
works. Some Joy UI components would benefit from this fix as well.
Ok yes, if we remove the background color the Grid items look better, but does that mean we shouldn't expect styling to behave "normally"? If I need to set a background color for a container, how would I go about doing that? And background color is one thing, but I suspect there would be other things (like borders?) that might feel strange.
But my issue is that the last grid item content has extra spacing after it, but the first one doesn't have extra spacing before it... Even without background color, it just means the next element in the dom would be further away for no apparent reason. Maybe this clarifies it: This might be the same issue as before actually, because the negative margin means the next dom element would be affected by the negative margin 🤔 |
You might be able to style the root grid container but not a nested container. I think styling grid items should be the first option: <Grid container sx={{ '& > .MuiGrid2-root:not(.MuiGrid2-container)': {
backgroundColor: 'red',
}}}>
<Grid>…</Grid>
<Grid>…</Grid>
</Grid>
Now I got it. I don't think that's an issue. It is the effect of a negative margin that behaves the same since Material UI v4. You could say that it is a limitation, so you have to manually add spacing to the next element of the grid. <Grid container spacing={3}> … </Grid>
<Box sx={{ mt: 1.5 }}>test</Box> |
…nested-grid-context
This will fix #36777 Proof: https://codesandbox.io/s/material-cra-forked-hc042i?file=/src/App.js |
@mnajdova I think this PR is ready to go. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two comments, the logic looks good 👍
|
||
A nested grid container will inherits the columns from its parent unless the `columns` prop is specified to the instance. | ||
Note that a nested grid container should be a direct child of another grid container. If there are non-grid elements in between, the grid container will start as the new root container. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes much more sense 👍 Note, we need to add a BREAKING CHANGE section in the PR description (and in the chalengelog entry).
* <Grid> // level 1 | ||
* ``` | ||
*/ | ||
level?: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ping on this question, it's still not clear to me.
</Grid> | ||
</Grid> | ||
</Box> | ||
<Grid container xs={6} spacing={3}> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
<Grid container xs={6} spacing={3}> | |
<Grid container xs={6}> |
Should we remove the spacing
from some of these children and test that it will be inherited from the parent?
BREAKING CHANGE
Grid2 now uses
React.cloneElement
(was React context) to pass thespacing
andcolumns
to the next container. The change is close to how CSS flexbox behaves.closes #36372, closes #36777 visual regression added.
Before
https://stackblitz.com/edit/react-ts-v9cb4z?file=App.tsx
After
https://codesandbox.io/s/material-cra-ts-forked-h3qicw?file=/src/App.tsx