Material Ui Grid doesn't scale properly - reactjs

I'm trying to deal with Grid responsiveness but I can't achieve my goal, idea is to with full screen show all 6 columns next to each other, with smaller screen decrease it to 3 columns and then another 3 columns below it. On big screen it looks fine as it looks like this:
However when I decrease width of the screen it starts to look like this:
and I have no really a clue how to make it work right, my code :
<Grid container spacing={24} style={{ padding: 24 }}>
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">Aid</StyledTypography>
</Grid>
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">
Associated Security Domain
</StyledTypography>
</Grid>
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">Life Cycle</StyledTypography>
</Grid>
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">Load File Aid</StyledTypography>
</Grid>
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">Version</StyledTypography>
</Grid>
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">Privileges</StyledTypography>
</Grid>
{mainData.map((el, i) => {
return (
<React.Fragment key={i}>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<StyledTypography variant="body1">{el.aid}</StyledTypography>
</Grid>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<StyledTypography variant="body1">
{el.associatedSecurityDomain}
</StyledTypography>
</Grid>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<StyledTypography variant="body1">
{el.lifeCycle}
</StyledTypography>
</Grid>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<StyledTypography variant="body1">
{el.loadFileAid}
</StyledTypography>
</Grid>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<StyledTypography variant="body1">{el.version}</StyledTypography>
</Grid>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<Tooltip
title={
<React.Fragment>
{el.privileges && el.privileges.length ? (
el.privileges.map((el, i) => <p key={i}>{el}</p>)
) : (
<p>None</p>
)}
</React.Fragment>
}
placement="top"
>
<Button className={classes.tooltipBtn}>
Hover to see Privilages
</Button>
</Tooltip>
</Grid>
</React.Fragment>
);
})}
</Grid>
and here is result I wish to achieve:

In order to achieve what you want, you'll have to change the structure for small screens.
You can use the withWidth or useMediaQuery hook from Material-UI and use conditional rendering to render the "desktop" or "mobile" layout.
Below is how the mobile structure should look like. This will contain a lot of duplicate code though.
const SmallScreenComponent = () => (
<Grid container spacing={24} style={{ padding: 24 }}>
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">Aid</StyledTypography>
</Grid>
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">
Associated Security Domain
</StyledTypography>
</Grid>
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">Life Cycle</StyledTypography>
</Grid>
{mainData.map((el, i) => {
return (
<React.Fragment key={i}>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<StyledTypography variant="body1">{el.aid}</StyledTypography>
</Grid>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<StyledTypography variant="body1">
{el.associatedSecurityDomain}
</StyledTypography>
</Grid>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<StyledTypography variant="body1">
{el.lifeCycle}
</StyledTypography>
</Grid>
</React.Fragment>
);
})}
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">Load File Aid</StyledTypography>
</Grid>
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">Version</StyledTypography>
</Grid>
<Grid item lg={2} md={4}>
<StyledTypography variant="h4">Privileges</StyledTypography>
</Grid>
{mainData.map((el, i) => {
return (
<React.Fragment key={i}>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<StyledTypography variant="body1">
{el.loadFileAid}
</StyledTypography>
</Grid>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<StyledTypography variant="body1">{el.version}</StyledTypography>
</Grid>
<Grid item lg={2} md={4} className={classes.gpProfileGrid}>
<Tooltip
title={
<React.Fragment>
{el.privileges && el.privileges.length ? (
el.privileges.map((el, i) => <p key={i}>{el}</p>)
) : (
<p>None</p>
)}
</React.Fragment>
}
placement="top"
>
<Button className={classes.tooltipBtn}>
Hover to see Privilages
</Button>
</Tooltip>
</Grid>
</React.Fragment>
);
})}
</Grid>
);

Related

Item content not center aligns in Grid and irregular height Material UI

I am trying to use MUI Card in Grid layout
<Grid container spacing={5} alignContent="center" alignItems="center" justifyContent="center" columnSpacing={{ xs: 1, sm: 2, md: 3 }}>
<Grid item xs={4} md={6} alignItems="center">
<Card />
</Grid>
<Grid item xs={4} md={6}>
<Card />
</Grid>
<Grid item xs={4} md={6}>
<Card />
</Grid>
<Grid item xs={4} md={6}>
<Card />
</Grid>
</Grid>
But the problem is its not center aligns with in each grid item. I have added border for each item
What I did wrong?
Also as you can see the grid overlap on the title above

MUI Grid: items do not change their width despite defining the columns for items

So i was following a tutorial, where the items would change depending on the size of the screen. And would take up the whole width if defined the following way:
class App extends Component {
render() {
return (
<div>
<Grid container>
<Grid>
<Paper item xs={12} sm={6} md={3}>1</Paper>
</Grid>
<Grid>
<Paper item xs={12} sm={6} md={3}>2</Paper>
</Grid>
<Grid>
<Paper item xs={12} sm={6} md={3}>3</Paper>
</Grid>
<Grid>
<Paper item xs={12} sm={6} md={3}>4</Paper>
</Grid>
</Grid>
</div>
);
}}
Well for me, no matter the width of my screen it always looks like this:
I see no reason for it not to work, but somehow the elements don't stretch to take up the complete width of the screen.
The mistake you are doing here is that you are adding the Grid properties (item xs={12} sm={6} md={3}) in the Paper component instead of the Grid item.
Here is the right code:
return (
<div>
<Grid container>
<Grid item xs={12} sm={6} md={3}>
<Paper>1</Paper>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Paper>2</Paper>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Paper>3</Paper>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Paper>4</Paper>
</Grid>
</Grid>
</div>
);
Here is a working codesandbox with your example

No duplicate props allowed react/jsx-no-duplicate-props

I am getting this error on line Grid item :
<Grid container spacing={3} className='centerd-text' >
<Grid item xs={12} xs={12} md={4} >
<img src={img01} alt="img" />
</Grid>
</Grid>
Please remove one xs={12} in Grid

Change Grid item column order on small screens

I'm attempting to have a grid looking like this for larger displays:
and this for smaller sizes:
Currently the skeleton of my grid is succinctly like this:
<Fragment>
<Grid container spacing={3}>
<Grid item xs={12} md={6}>
<img src={img1} />
</Grid>
<Grid item xs={12} md={6}>
<Paper>Text Content 1...</Paper>
</Grid>
</Grid>
<Grid container spacing={3}>
<Grid item xs={12} md={6}>
<Paper>Text Content 2...</Paper>
</Grid>
<Grid item xs={12} md={6}>
<img src={img2} />
</Grid>
</Grid>
</Fragment>
How can I do such as in mobile view (width 12) the first Grid item from the second Grid container (Text content 2) displays after (below) the image?
Use like this:
<Grid container>
<Grid spacing={3} container item xs={12} md={12}>
<Grid item xs={12} md={6} className={classes.item}>
<img
width="50px"
src="https://cdn3.vectorstock.com/i/1000x1000/37/37/simple-cartoon-a-cute-bear-vector-18823737.jpg"
alt="abc"
/>
</Grid>
<Grid container item xs={12} md={6} className={classes.item}>
<Paper>Text Content 1...</Paper>
</Grid>
</Grid>
<Grid container item spacing={3} xs={12} md={12} className="flexgrid">
<Grid container item xs={12} md={6} className={classes.item}>
<Paper>Text Content 2...</Paper>
</Grid>
<Grid container item xs={12} md={6} className={classes.item}>
<img
width="50px"
src="https://cdn3.vectorstock.com/i/1000x1000/37/37/simple-cartoon-a-cute-bear-vector-18823737.jpg"
alt="abd"
/>
</Grid>
</Grid>
</Grid>
SOURCE
PREVIEW
for re-order added:
<Grid container item spacing={3} xs={12} md={12} className="flexgrid">
and css :
#media screen and (max-width: 720px) {
.flexgrid{
flex-direction: column-reverse
}
}

Material UI stepper does not animate

I have a vertical stepper I'm using in Material UI to render a pricing calculator. The animation is no longer working. Previously, the stepper would animate a collapse effect when the steps changed.
I suspect it may be due to the nesting of multiple transitions, but I'm not entirely sure.
Relevant render method for the calculator is below. Thanks!
return (
<Grid container>
<Grid item xs={12} sm={12} md={8} className="nocPricingCalculatorContainer">
<Collapse in={activeStep < 2}>
<Stepper elevation={0} activeStep={activeStep} className="nocPricingCalculatorStepper" connector={<StepConnector classes={{ line: classes.stepConnector }} />} orientation="vertical">
{steps.map((stepObj, index) => {
return (
<Step key={uuidv4()}>
<StepLabel classes={{ label: classes.stepLabel }} StepIconProps={{
classes: { text: classes.stepLabelText }
}}>{stepObj.label}</StepLabel>
<StepContent className={classes.stepContentBorder}>
<div className={classes.actionsContainer}>
<Grid container item xs={12}>
{getStepContent(index)}
</Grid>
<div>
{
(activeStep !== 0) ?
<Button
className="nocButtonClear"
onClick={handleBack}>
Back
</Button> :
<div></div>
}
<Button
variant="contained"
onClick={handleNext}
className="nocButtonYellow"
disableRipple
>
{activeStep === steps.length - 1 ? 'Finish' : 'Next'}
</Button>
</div>
</div>
</StepContent>
</Step>
)
})}
</Stepper>
</Collapse>
<Collapse in={activeStep >= 2}>
{activeStep === steps.length && (
<Grid item container className="nocPricingCalculatorFinalStep" alignItems="center" justify="center">
<Grid item container xs={12} justify="center" className="nocPricingCalculatorResetArea">
<Grid item>
<Button className="nocButtonClear nocPricingCalculatorResetButton" disableRipple onClick={handleReset}>
<RefreshIcon className="nocResetIcon" />Reset calculator
</Button>
</Grid>
</Grid>
<Grid item container xs={12} justify="center" className="nocPricingCalculatorPriceArea">
<Grid item container alignItems="center" justify="center" xs={12} className="nocPriceColumnLabel">
<CheckCircle className="nocPriceAreaCheckIcon" />
<Typography className="nocPricingCalculatorPricingHeader">
{pricingSectionTitle}
</Typography>
</Grid>
<Grid item container className="nocPricingCalculatorPriceColumn left" alignItems="center" justify="center" xs={12} sm={12} md={6}>
<Grid item xs={12}>
<Typography className="nocPricingCalculatorPricingRepairType">
{pricingColumnLeft.title}
</Typography>
<Typography className="nocPricingCalculatorPricingPrice">
{pricingColumnLeft.price}
</Typography>
</Grid>
<Grid item>
<List dense={true}>
{generateRenderableList(pricingColumnLeft.details)}
</List>
</Grid>
</Grid>
<Grid item container className="nocPricingCalculatorPriceColumn right" alignItems="center" justify="center" xs={12} sm={12} md={6}>
<Grid item xs={12}>
<Typography className="nocPricingCalculatorPricingRepairType">
{pricingColumnRight.title}
</Typography>
<Typography className="nocPricingCalculatorPricingPrice">
{pricingColumnRight.price}
</Typography>
</Grid>
<Grid item>
<List dense={true}>
{generateRenderableList(pricingColumnRight.details)}
</List>
</Grid>
</Grid>
</Grid>
</Grid>
)}
</Collapse>
</Grid>
</Grid>
);
}
Never mind. I accidentally put a random UUID key on the Step and that caused the stepper to jump to the next step without animating.
#reactprobs

Resources