Material UI Grid component Fill second column to end of container - reactjs

I am using material UI's Grid component and making use of the auto property for the first column
so I have
<Grid container className={classes.borderclass}>
<Grid item xs={"auto"}>
<Items />
</Grid>
<Grid item xs={10}>
<Content />
</Grid>
</Grid>
However this will not fill the entire container but I do not seem to see an option for remainder in the sizes.
I have looked into css calc option however I do not see a way to get the size of the auto column in react to compare against the div
any suggestions even if it is not material ui will be appreciated.

If you check the Material-UI demo, they have a working example that shows the auto property in action. So, as the example depicted here, you don't need to specify the auto keyword. Do just this:
<Grid container className={classes.borderclass}>
<Grid item xs>
<Items />
</Grid>
<Grid item xs={10}>
<Content />
</Grid>
</Grid>

Related

Mui Grid Item Fixed Width Item Beside Fill Width Item

I'm currently using MUI Grid (but I'm open to alternate solutions) and I want two components side by side: the right most taking up 400px of width and the left component taking up the rest.
|<------------------------------- 100% of page ------------------------------->|
|<-------------------- Fill -------------------->| |<---------- 400px ----------->|
When page width is shrunk:
|<-------------------- 100% of page -------------------->|
|<--------- Fill --------->| |<---------- 400px ----------->|
My code is currently stretching the leftComponent to 100% of the page and pushing the rightComponent below it.
<Grid container spacing={3}>
<Grid item xs={12} >
<leftComponent />
</Grid>
<Grid item style={{width: "400px"}}>
<rightComponent />
</Grid>
</Grid>
All you need to do is remove the number for your xs value. Doing this just tells the item to have a width of auto which will just fill the space
<Grid container spacing={3}>
<Grid item xs>
<leftComponent />
</Grid>
<Grid item style={{width: "400px"}}>
<rightComponent />
</Grid>
</Grid>
Though I would recommend not using grid for this situation but using Box component instead as the Grid component is really meant to stick with the 12 columns and adding in a fixed width would break it.

material UI grid layout design

Im completely new to Material UI and im trying to make use of Grid concept to position my components in much dynamic way. I would like to create the grids in the following pattern and i'm stuck in finding out a solution to create two column grid having different number of rows or items.
Below is the sample layout which im trying to design using grids so, can somebody please share any ideas to achieve it?
Sample code would be more helpful.
Screenshot of my planned design
To get the Grid format in material UI, follow this docs. You can try like this, with multiple nested grid containers to achieve what you expect.
<div>
<Grid
container
spacing={1}
direction="row"
justify="flex-start"
alignItems="flex-start"
>
<Grid container item xs={4} spacing={1}>
<Grid item xs={12}>
<Paper>Some Text </Paper>
</Grid>
<Grid item xs={12}>
<Paper style={{height:400}}>Component A</Paper>
</Grid>
</Grid>
<Grid container item xs={8} spacing={1} >
<Grid item xs={12}>
<Paper style={{height:200}}>Component B</Paper>
</Grid>
<Grid item xs={12}>
<Paper style={{height:200}}>Component C</Paper>
</Grid>
</Grid>
</Grid>
</div>

I want to set flex-direction property to Material UI Grid tag

I want to set direction property to <Grid container> component of the Material UI framework. I have read the doc
and I tried setting direction property as column like below:
<Grid container direction="column">
<Grid item xs={12} >
<h3 className="center-align">Log In</h3>
<input type="text" id="user-email-on-login" placeholder="username#email.com" />
<input type="password" id="user-password-on-login" placeholder="password" />
<button>Log In</button>
</Grid>
<Grid item xs={12}>
<button>Forgot Id/Password</button>
<button>Create Account</button>
<button>Remember Me</button>
</Grid>
</Grid>
But the output is not expected result. The output seems the same with the way I have used direction="row".
When I remove <Grid item> tag, then direction=column property on <Grid container> works perfectly.
If I have to use <Grid item> tag, is there a way to set flex-direction property as column?
You could add the "container" property to the grid element and thus have the "direction" property.
It's possible for an item to be both container and item at the same time.
Anyway, if you want to be more precise, you can upload an example of sandbox :)
Here's an example of what you want:
https://stackblitz.com/edit/react-2m3blz

Centering button on extra small screen Material-UI React not working (justify-xs-center)

I tried everything but it seems that I'm missing something. I have been trying a lot of time to make a button center inside the grid when the screen is extra small.
This code works perfectly, but the problem is that i want my button to only center when the screen is extra small not on all sizes.
Working Code.
Grid item xs={12}>
<Grid container justify="center">
<Button color="primary" variant="raised">
Add Product
</Button>
</Grid>
</Grid>
Not working code...
Grid item xs={12}>
<Grid container className={"justify-xs-center"}>
<Button color="primary" variant="raised">
Add Product
</Button>
</Grid>
</Grid>
I have been reading the API documentation for the Grid, as i understand this is the correct way to add a predefined class in the component, but the effect seems not to work. When i inspect the element though the class justify-xs-center is found on the Grid container component as expected.
Any help is appreciated, Thank you.
Well it seems i have really misunderstood the CSS API of the Grid.
https://material-ui.com/api/grid/#css-api
My solution to this was to use the breakpoints offered by material-ui.
https://material-ui.com/layout/breakpoints/#theme-breakpoints-up-key-media-query
I created this CSS rule and applied it to the Grid container element that i wanted its contents to be centered.
const styles = theme => ({
addButtonContainer: {
[theme.breakpoints.down("xs")]: {
justifyContent: "center"
}
}
});
And this is the container that is being centered on extra small screens
<Grid item xs={12}>
<Grid container className={classes.addButtonContainer}>
<Button color="primary" variant="raised">
Add Product
</Button>
</Grid>
</Grid>

Jumping to specific location in a page in react

I need to go to a specific line on page, when clicking on a button. How can it be done using react-router.I have :
<Grid xs={3}>
<Button>heading</Button>
<Button>Heading2</Button>
</Grid>
<Grid xs={3}>
<h1>heading</h1>
<p>Paragraph</p>
<h2>Heading2</h2>
<p>Paragraph2</p>
</Grid>
When clicking on heading button, it should jump to the relevant place in 2nd grid. How it can be done. I referred, How to use react-router to jump to specific location in the page as well.
Wrap your Link component in MUI Button and define an id attribute for your second grid,say gridWrapper.
<Grid xs={3}>
<Button component={Link} to="#gridWrapper">heading</Button>
<Button>Heading2</Button>
</Grid>
<Grid xs={3} id="gridWrapper">
<h1>heading</h1>
<p>Paragraph</p>
<h2>Heading2</h2>
<p>Paragraph2</p>
</Grid>

Resources