MUI Grid System to Take Up 100% of Width - reactjs

hope you're all good.
I'm having an issue finding out how to make my MUI Grid take up the entire width of the Box component it is nested within.
Here is how it looks:
I want the empty space at the end right to be taken up by the entire grid but I can't seem to find a concise answer on how to fix this.
Here is my code in VS Code:
Here is the code in text for the Grid:
<Grid item key={days}>
<Box
key={days}
sx={{
width: 150,
height: 150,
backgroundColor: 'primary.dark',
'&:hover': {
backgroundColor: 'primary.main',
opacity: [0.9, 0.8, 0.7],
},
}}
/>
</Grid>
);
}
return (
<Grid container spacing={1}>
{/* And here I render the box array */}
{box}
</Grid>
);
And here is the parent elements:
Here is the code in real text for the parent elements:
// Parent Element
<Container>
<Box
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
width: 1,
}}
>
{/* The Title of The Page */}
<Typography align="center" variant="h4" mt={2} mb={2} sx={{ fontWeight: 'bold' }}>
{t('Happy Chocolate Days!')}
</Typography>
{/* My Calendar Grid Component Inside The Box Component */}
<Calendar />
</Box>
</Container>
Thanks for any help in advance

You are setting the boxes to a fixed width & height of 150, thus even though their wrapper has more space left, there is not enough space for another box so it breaks into a new line, thus leaving you with the empty space on the right.

You either set the boxes all of them in the middle of the Container,
OR pick a width for the Container that can have 7 boxes of the same width with some space between them, like 770px width for container, and 10px spaces between them. but in that case you wont need Grid, just pure css.

Related

MUI alternative to CardHeader

Good evening,
I am looking for a better way to display what you see in the picture. With the CardHeader this is all not centered and I can't use any useState methods.
how it should look
<Card sx={{ height: '100%', display: 'flex', flexDirection: 'column' }}>
<CardHeader avatar={<Avatar sx={{ bgcolor: red[500] }} aria-label="logo">N</Avatar>} action={<IconButton aria-label="enable"><FavoriteBorderIcon /></IconButton>} title="Netflix" subheader="netflix.com" />
</Card>
The way it looks here is how I want it to look in the end, but I can't useState methods here. Also the content (e.g. the icon) is not centered vertically.
I dont know why there is a problem with useState hook. Feel free to add it to the and depending on the state switch between normal and red icon.
https://codesandbox.io/s/distracted-oskar-1h7xmj?file=/src/App.js

Material-UI: How to center pagination button?

I tried to get Pagination from Material-UI
but I want to center the buttons of the arrow and number of page.
I tried to center by creating a <div style={{textAlign: "center"}}> but it doesn't work because it came all in one box.
there is any way to get inside the this component and make the numbers and button to get in the center?
If you're using TablePagination, you need to remove the spacer div which push the pagination content to the right and set the container justify-content to center:
import TablePagination, {
tablePaginationClasses
} from "#mui/material/TablePagination";
<TablePagination
sx={{
[`& .${tablePaginationClasses.spacer}`]: {
display: "none"
},
[`& .${tablePaginationClasses.toolbar}`]: {
justifyContent: "center"
}
}}
{...}
/>
If you're using the Pagination from DataGrid, just set the justify-content to center because the container is already flex:
import { makeStyles } from "#mui/styles";
import { paginationClasses } from "#mui/material/Pagination";
const useStyles = makeStyles({
root: {
[`& .${gridClasses.footerContainer}`]: {
justifyContent: "center"
}
}
});
<DataGrid pagination {...data} className={classes.root} />
Their Pagination component is using display: flex. Adding the following style rule should achieve what you're trying to do
.MuiPagination-ul { justify-content: center; }
using below code
<Box display="flex" justifyContent="center">
<Pagination ... />
</Box>
To center pagination, i suggest you wrap it inside a Grid System, then put it in the middle of two grid items with the flexGrow property equal to 1.
<Grid container>
<Grid item sx={{ flexGrow: 1 }}></Grid>
<Grid item>
<Pagination />
</Grid>
<Grid item sx={{ flexGrow: 1 }}></Grid>
</Grid>

Material UI responsive grid direction

I want the container direction as "row" above md size screen and "column" below md size screen?
How can I implement it?
<Grid container direction = "row"(in large screens)/direction = "column"(in small screens)>
I tried something this.
<Grid container classes={gridDirection}>
gridDirection: {
direction = "row",
[theme.breakpoints.down('md')]: {
direction = "column",
},
}
but it is not working probably because "direction" is a react prop and not a CSS style.
Is there a way to access these react props inside stylesheet file?
The easiest way and I believe the modern way is to use the sx attribute. Then let MUI handle the breakpoints for you.
These are the default MUI breakpoints as of writing.
xs = extra-small: 0px
sm = small: 600px
md = medium: 900px
lg = large: 1200px
xl = extra-large: 1536px
xs and sm are generally considered to be mobile/small tablet. MUI is mobile first, so xs will be set and not overwritten until the size is md or larger in my example below.
<Grid container sx={{ flexDirection: { xs: "column", md: "row"} }}>
</Grid>
You can use useMediaQuery
import useMediaQuery from '#material-ui/core/useMediaQuery';
const largeScreen = useMediaQuery(theme => theme.breakpoints.up('md'));
<Grid container direction={largescreen?"row":"column"}>
in MUI v5 you can do it like below:
<Grid container direction={{xs: "column", md: "row"}}>
</Grid>
I am thinking there should be at least 3 ways to do it in MUI v5.
with default prop
<Grid
container
spacing={0}
direction={{ xs: 'column', md: 'row' }}
justifyContent={{ xs: 'center', md: 'flex-end' }}
alignItems={{ xs: 'center', md: 'flex-end' }}
>
...
with sx props
<Grid
container
spacing={0}
sx={{ flexDirection: { xs: 'column', md: 'row'} }}
>
in theme override
MuiGrid: {
styleOverrides: {
root: {
'&.MuiGrid-container': {
flexDirection: 'column'
[theme.breakpoints.up('md')]: {
flexDirection: 'row'
},
},
Use flexDirection instead of direction.
gridDirection: {
flexDirection = "row",
[theme.breakpoints.down('md')]: {
flexDirection = "column",
},
}
At least as on date of answering here, Material UI supports direction or flexDirection attributes with component, with these attributes supporting responsive value based on breakpoints mapped object passed to it.
For example, in your case, if direction="row" is needed on large screens and direction="column" is needed for small screens, then the prop can be passed in as:
direction={{xs:'row', lg:'column'}}, thus letting MUI prop handle it with the Grid element as:
<Grid container direction={{xs:'row', lg:'column'}}> ... </Grid>
This is the right style to use it in your code, for this case, in my opinion, though other alternative answers here also work well.
Just set the container false for column view and true for row view
<Grid container={{ lg: true, sm: false }}
...
/>

Dropdown increasing its size after selecting options (React)

How can i make the dropdown fixed i.e., after selecting options , the dropdown is increasing its size if the option length is big. I dont want it to increase because it is disturbing other components which are beside it.
Ive my code like this:
dropdown:{
marginTop: 17,
maxWidth: '120%'
},
<Typography variant="h3" component="h3" style={{ marginBottom: 10}} gutterBottom>
Region
<div className={classes.dropdown}>
<Dropdown
options={this.state.regionList}
onChange={this.handleRegionChange}
/>
</div>
</Typography>
Please use both width and maxWidth. for example:
dropdown:{
marginTop: 17,
maxWidth: '200px'
width: '200px'
},

React Native: Why is image stretching vertically to fill container and how to prevent it?

In my React Native app, I have a red box of height 300 that I want to center vertically, and an image that I want to sit inside and at the top of this red box. Here is my code so far:
import React, { Component } from 'react';
import { View, Image} from 'react-native';
export default class Login extends Component {
render() {
return (
<View style={{
flex: 1,
justifyContent: "center",
alignItems: "center"
}}>
<View
style={{
borderWidth: 3,
borderColor: 'red',
width: "100%",
height: 300
}}
>
<Image
source={require('../../images/swoord-top.png')}
style={{
width: "100%",
height: "100%",
resizeMode: "contain",
borderWidth: 3
}}
>
</Image>
</View>
</View>
);
}
}
Here's what this looks like:
The red box is the outer box I mentioned, and the black box is just the border of the Image. The problem is that this black box (ie the Image) expands to fill the red box vertically, and the image is vertically centered inside this black box, so it's vertically centered inside the red box, as opposed to at the flex-start position that I want it at. I've tried adding justifyContent: flex-start and flexShrink: 1 to the Image and neither has made a difference.
Does anyone know how I can approach this?
NOTE:
If I remove the height: 100% on the Image, I get this:
UPDATE:
To clarify, this is what I'd like it to look like. I've removed the black border here. I moved it up to where I want it by adding top: -95, but this won't work in general because the value would be different for different devices:
try doing like this
<Image
source={require('../../images/swoord-top.png')}
style={{
width: "100%",
borderWidth: 3
}}
resizeMode={"contain"}
/>
There is a difference between Image (the view) and the content of that image. On your screenshot, the image view has the black border and its content is sitting inside of it. Because they have different aspect ratios, there is going to be either a blank space or cut-off on the content. You can't adjust position of the content as it's not laid out on flex basis. Image view is just a window that then renders image content. There is no property to tell Image where to align that content
Your problem comes from the fact that screen width differs, and aspect ratio of your container also differs (variable width but constant 300 height). What you need to do is
measure container width
determine proper aspect ratio for the image based on image resource width and height
set your image width to 100% and its height according to received aspect ratio
here, my image dimensions are 1005 * 219:
const Test = () => {
const [width, setWidth] = useState(0);
return (
<View>
<View
onLayout={e => setWidth(e.nativeEvent.layout.width)}
style={{ width: '100%', height: 300, borderWidth: 1 }}>
<Image
style={{
width: '100%',
height: (width / 1005) * 219,
borderWidth: 1,
borderColor: 'red',
}}
source={require('...')}
/>
</View>
</View>
);
};

Resources