Nested Grid containers ignore columns prop #28554
Labels
Comments
|
Root cause: context is used before prop, so nested does not work if the top Grid has specified // Grid.js
const {
className,
columns: columnsProp = 12,
}
const columns = React.useContext(GridContext) || columnsProp
const wrapChild = (element) =>
columns !== 12 ? (
<GridContext.Provider value={columns}>{element}</GridContext.Provider>
) : (
element
);Suggested Fix diff --git a/packages/mui-material/src/Grid/Grid.js b/packages/mui-material/src/Grid/Grid.js
index 61c964e1fb..d725639502 100644
--- a/packages/mui-material/src/Grid/Grid.js
+++ b/packages/mui-material/src/Grid/Grid.js
@@ -246,7 +246,7 @@ const Grid = React.forwardRef(function Grid(inProps, ref) {
const props = extendSxProp(themeProps);
const {
className,
- columns: columnsProp = 12,
+ columns: columnsProp,
columnSpacing: columnSpacingProp,
component = 'div',
container = false,
@@ -267,7 +267,8 @@ const Grid = React.forwardRef(function Grid(inProps, ref) {
const rowSpacing = rowSpacingProp || spacing;
const columnSpacing = columnSpacingProp || spacing;
- const columns = React.useContext(GridContext) || columnsProp;
+ const columnsContext = React.useContext(GridContext);
+ const columns = columnsProp || columnsContext || 12;
const ownerState = {
...props,
cc @mnajdova |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Current Behavior😯
When nesting a Grid container inside a Grid item, only the columns prop of the root container is used.
Expected Behavior🤔
It should be allowed to place a Grid container with X columns inside a Grid container with Z columns.
Steps to Reproduce🕹
https://codesandbox.io/embed/columnsgrid-material-demo-forked-s2vsr?fontsize=14&hidenavigation=1&theme=dark
Steps:
Context🔦
I have a responsive media grid, with custom columns prop depending on breakpoints.
Inside each media pods, I want to arrange text and button element, and keep the layout regardless the amount of columns the media grid have.
Your Environment🌎
`npx @mui/envinfo`
The text was updated successfully, but these errors were encountered: