Set Material-UI vertical and centered aligned Grid items in layout - reactjs

I'm pretty new to Material-UI and I would like to have this basic implementation with Grid components:
But so far I'm unable to center the Grid items correctly. I tried different approaches but none of them worked. This is my code so far:
<Grid container direction="column">
<Grid item>
<AppBar position="static">
<Toolbar>MY APP</Toolbar>
</AppBar>
</Grid>
<Grid item container direction="column">
<Grid item xs={11}>
Some text
</Grid>
<Grid item xs={11}>
Some text
</Grid>
</Grid>
</Grid>
Thanks in advance for any help.

You need to use some more of the Material UI components to achieve your goal. You need to use the Paper and Typography components and the makeStyles function. I set up a demo here and the code should look something like this:
import "./styles.css";
import Grid from "#material-ui/core/Grid";
import AppBar from "#material-ui/core/AppBar";
import Toolbar from "#material-ui/core/Toolbar";
import Paper from "#material-ui/core/Paper";
import Typography from "#material-ui/core/Typography";
import { makeStyles } from "#material-ui/core/styles";
// Set your styles for the paper component here:
const paperStyles = makeStyles((theme) => ({
root: {
margin: "15px",
height: "150px",
padding: "20px",
backgroundColor: "#cfcfcf"
}
}));
export default function App() {
// Call your styles function:
const paperClasses = paperStyles();
return (
<div className="App">
<Grid container direction="column">
<Grid item>
<AppBar position="static">
<Toolbar>MY APP</Toolbar>
</AppBar>
</Grid>
<Grid item container direction="column">
// Use the paper component here and assign paperClasses.root to the className prop:
<Paper className={paperClasses.root} item xs={11} >
// Put your other Grid components inside of the paper component:
<Grid item xs={11}>
// Use the Typography component for your text with the align prop set to "left":
<Typography variant="subtitle1" align="left">
Some text
</Typography>
</Grid>
</Paper>
<Paper className={paperClasses.root} item xs={11}>
<Grid item xs={11}>
<Typography variant="subtitle1" align="left">
Some text
</Typography>
</Grid>
</Paper>
</Grid>
</Grid>
</div>
);
}

If I understood your question, I think this should fix it, just add alignContent='center' and check props of Grid
<Grid container direction="column">
<Grid item>
<AppBar position="static">
<Toolbar>MY APP</Toolbar>
</AppBar>
</Grid>
<Grid item container direction="column" alignContent='center' >
<Grid item xs={11}>
Some text
</Grid>
<Grid item xs={11}>
Some text
</Grid>
</Grid>
</Grid>

Related

Material Ui layout Grid adjusting height to middle of the page

I have the following material UI grid code (copied from their documentation and adjusted to show the problem I'm having):
import React from 'react';
import { makeStyles } from '#material-ui/core/styles';
import Paper from '#material-ui/core/Paper';
import Grid from '#material-ui/core/Grid';
const useStyles = makeStyles((theme) => ({
root: {
flexGrow: 1,
},
paper: {
padding: theme.spacing(2),
textAlign: 'center',
color: theme.palette.text.secondary,
},
tall: {
height: 400,
padding: theme.spacing(2),
textAlign: 'center',
color: theme.palette.text.secondary,
},
}));
export default function CenteredGrid() {
const classes = useStyles();
return (
<div className={classes.root}>
<Grid container>
<Grid item container xs={4}>
<Grid item xs={12}>
<Paper className={classes.paper}>xs=12</Paper>
</Grid>
<Grid item xs={12}>
<Paper className={classes.tall}>xs=6</Paper>
</Grid>
<Grid item xs={12}>
<Paper className={classes.paper}>xs=6</Paper>
</Grid>
</Grid>
<Grid item container xs={8}>
<Grid item xs={12}>
<Paper className={classes.paper}>xs=12</Paper>
</Grid>
<Grid item xs={12}>
<Paper className={classes.paper}>xs=6</Paper>
</Grid>
<Grid item xs={12}>
<Paper className={classes.paper}>xs=6</Paper>
</Grid>
</Grid>
</Grid>
</div>
);
}
The issue I am having is that it is adjusting the height of the second column based on the first column, as you can see in the image below. I'd like the xs=6 tiles in the second column to be directly stacked one on top of the other. How do I do this?
adding to the highest grid container: alignItems="baseline" seems to have done the trick

reactjs: material ui: how to align two items to the two ends

I have the following code in reactjs using material ui theme
const useStyles = makeStyles((theme) => ({
root2: {
maxWidth: 345,
flexGrow: 1
},
paper2: {
padding: theme.spacing(1),
color: theme.palette.text.secondary,
}
})
<div className={classes.root2}>
<Grid container spacing={3}>
<Grid item xs={12}>
<Paper className={classes.paper2}>
<Grid container alignItems="center" spacing={3}>
<Grid item>
<h3>Instructions</h3>
</Grid>
<Grid item>
<IconButton>
<ExpandMoreIcon />
</IconButton>
</Grid>
</Grid>
</Paper>
</Grid>
</Grid>
</div>
What i want is
I saw an example at https://material-ui.com/system/flexbox/#flex-grow
which uses the code:
import React from 'react';
import Box from '#material-ui/core/Box';
export default function FlexGrow() {
return (
<div style={{ width: '100%' }}>
<Box display="flex" p={1} bgcolor="background.paper">
<Box p={1} flexGrow={1} bgcolor="grey.300">
Item 1
</Box>
<Box p={1} bgcolor="grey.300">
Item 2
</Box>
<Box p={1} bgcolor="grey.300">
Item 3
</Box>
</Box>
</div>
);
}
I am new to material ui. Here the example uses Box instead of Grid.
So which is the best way to handle this
I am used to bootstrap 4: IN bootstrap4 i can do this same thing using
https://getbootstrap.com/docs/4.4/utilities/flex/#justify-content
<div class="d-flex justify-content-between">...</div>
How to do something like this in material UI
Adding justify="space-between" to Grid container component will solve your issue.
Please refer this.
<Grid container spacing={3}>
<Grid item xs={12}>
<Paper className={classes.paper2}>
<Grid container alignItems="center" justify="space-between">
<Grid item>
<h3>Instructions</h3>
</Grid>
<Grid item>
<IconButton>
<ExpandMoreIcon />
</IconButton>
</Grid>
</Grid>
</Paper>
</Grid>
</Grid>

Material UI - Vertically Stack Typography and CircularProgress within Backdrop

How do I stack a CircularProgress component on top of a Typography component (both of which are in a Backdrop component)?
Currently, the progress component is on the left and the text is right next to the progress bar. I've attempted to use grid items/containers but no luck so far.
<Backdrop open={loading} style={{zIndex: 20, opacity: 0.8, backgroundColor: '#FFFF', color: '#0000'}}>
<Grid container alignItems='center' justify='center' alignContent='center'>
<Grid item sm={12} md={12} lg={12} align='center'>
<PurpleProgress/>
</Grid>
<Grid item sm={12} md={12} lg={12} align='center'>
<Typography variant="body2" style={{color: '#000000'}}><b>{message}</b></Typography>
</Grid>
</Grid>
</Backdrop>
You are probably having this issue on small screens because you do not have the xs prop set to take up the entire grid. To solve this issue, if you want the stacking to be consistent across all screen sizes, you can just use xs={12}
<Grid container alignItems='center' justify='center' alignContent='center'>
<Grid item xs={12} align='center'>
<PurpleProgress/>
</Grid>
<Grid item xs={12} align='center'>
<Typography variant="body2" style={{color: '#000000'}}><b>{message}</b></Typography>
</Grid>
</Grid>

Is it possible to put html elements to the left side of a material UI grid?

Using material UI for the styling.
Is it possible to place an HTML element or Typography element to the left side of a Grid and have it still be on the same line?
Here is a small snippet of the code.
return (
<Wrapper>
<form>
<Grid container spacing={1}>
<Grid item xsn={8}>
<SideLabel>{labels.dates}</SideLabel>
</Grid>
<Grid item xsn={22}>
<DatePicker name="serviceDateOne" label={labels.serviceDateOne} />
</Grid>
<Grid item xsn={23}>
<DatePicker name="serviceDateTwo" label={labels.serviceDateTwo} />
</Grid>
<Grid item xsn={47} />
</Grid>
</form>
</Wrapper>
);
Lets say for example, I want to add a typography element and have it shown on the same line as grid items serviceDateOne and serviceDateTwo, what would that look like? I've tried
return (
<Wrapper>
<form>
<Typography>TEST</Typography>
<Grid container spacing={1}>
<Grid item xsn={8}>
<SideLabel>{labels.dates}</SideLabel>
</Grid>
<Grid item xsn={22}>
<DatePicker name="serviceDateOne" label={labels.serviceDateOne} />
</Grid>
<Grid item xsn={23}>
<DatePicker name="serviceDateTwo" label={labels.serviceDateTwo} />
</Grid>
<Grid item xsn={47} />
</Grid>
</form>
</Wrapper>
);
Add the Typography element is displayed above the line where serviceDateOne and serviceDateTwo are. BTW, the component is just a styled div. The element is included as part of the Grid, what i'm trying to achieve is to place that element outside of the grid and to the left of the form, acting like side labels for that row, and only have the grid items as part of the grid
Grid is using for the seperation, so just make the seperation at the top level.
return (
<>
<Grid container spacing={1}>
<Grid item xs={3} style={{ backgroundColor: "lightgray" }}>
<Typography>TEST</Typography>
</Grid>
<Grid item xs={9}>
XXX
<Grid container spacing={1}>
<Grid item xs={12}>
YYY
</Grid>
<Grid item xs={6}>
ZZ
</Grid>
<Grid item xs={6}>
ZZ
</Grid>
</Grid>
</Grid>
</Grid>
</>
);
Update
Show two child in one line: ref QA
return (
<div style={{ display: "flex" }}>
<Typography style={{ backgroundColor: "lightgray", width: 100 }}>
TEST
</Typography>
<Grid container spacing={1} style={{ backgroundColor: "gray" }}>
<Grid item xs={12}>

Material UI React paper component in nested grid display issues

I have the following code that has a paper component in a nested grid:
import React from 'react';
import logo from './logo.svg';
import './App.css';
import {
Container,
AppBar,
Toolbar,
IconButton,
Typography,
Button,
Paper,
Grid,
Avatar,
} from '#material-ui/core';
import MenuIcon from '#material-ui/icons/Menu';
function App() {
return (
<div className="App">
<AppBar position="static">
<Toolbar>
<IconButton edge="start" color="inherit" aria-label="menu">
<MenuIcon />
</IconButton>
<Typography variant="h6">AwesomeApp</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
<Container style={{marginTop: '1em'}}>
<Grid container spacing={2}>
<Grid item container alignItems="center" xs={4}>
<Paper>
<Grid item xs={6} alignItems="center">
<Avatar>JD</Avatar>
</Grid>
<Grid item xs={6} alignItems="center">
John Doe
</Grid>
</Paper>
</Grid>
<Grid item xs={6}>
<Paper>xs=6</Paper>
</Grid>
<Grid item xs={2}>
<Paper>xs=6</Paper>
</Grid>
</Grid>
</Container>
</div>
);
}
export default App;
However, it seems that the paper component doesn't allow the grid to stretch out to occupy all of it's space as I would expect:
How do I get the paper element to take up the space of its container?
If you set the height and width to 100% on the Paper elements, they will use up the full space of the Grid items as desired.
import React from "react";
import ReactDOM from "react-dom";
import {
Container,
AppBar,
Toolbar,
IconButton,
Typography,
Button,
Paper,
Grid,
Avatar
} from "#material-ui/core";
import MenuIcon from "#material-ui/icons/Menu";
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles({
paper: {
width: "100%",
height: "100%"
}
});
function App() {
const classes = useStyles();
return (
<div className="App">
<AppBar position="static">
<Toolbar>
<IconButton edge="start" color="inherit" aria-label="menu">
<MenuIcon />
</IconButton>
<Typography variant="h6">AwesomeApp</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
<Container style={{ marginTop: "1em" }}>
<Grid container spacing={2}>
<Grid item container alignItems="center" xs={4}>
<Paper className={classes.paper}>
<Grid item xs={6} alignItems="center">
<Avatar>JD</Avatar>
</Grid>
<Grid item xs={6} alignItems="center">
John Doe
</Grid>
</Paper>
</Grid>
<Grid item xs={6}>
<Paper className={classes.paper}>xs=6</Paper>
</Grid>
<Grid item xs={2}>
<Paper className={classes.paper}>xs=2</Paper>
</Grid>
</Grid>
</Container>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
You might want to add an other container inside container to things to look more compact.
Try code below,
function App() {
return (
<div className="App">
<AppBar position="static">
<Toolbar>
<IconButton edge="start" color="inherit" aria-label="menu">
<MenuIcon />
</IconButton>
<Typography variant="h6">AwesomeApp</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
<Container style={{ marginTop: "1em" }}>
<Grid container spacing={0}>
<Grid item container alignItems="center" xs={1}>
<Paper>
<Grid item xs={6} alignItems="center">
<Avatar>JD</Avatar>
</Grid>
<Grid item xs={6} alignItems="center">
John Doe
</Grid>
</Paper>
</Grid>
<Grid container spacing={0} xs={11}>
<Grid item xs={6}>
<Paper>xs=6</Paper>
</Grid>
<Grid item xs={6}>
<Paper>xs=6</Paper>
</Grid>
</Grid>
</Grid>
</Container>
</div>
);
}
I have also created a live sandbox for you: https://codesandbox.io/s/lucid-blackwell-411ds?fontsize=14&hidenavigation=1&theme=dark

Resources