Material UI React paper component in nested grid display issues - reactjs

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

Related

Why mui grid iten is not justify horizontally in a Grid

<Container maxWidth="lg">
<AppBar position="static" color="inherit">
<Typography variant="h2" align="center">
Memories
</Typography>
<img src={memories} alt="memories" height="60" />
</AppBar>
<Grow in>
<Container>
<Grid
container
justifyContent="space-between"
alignItems="stretch"
spacing={3}
>
<Grid item xs={12} sm={7}>
<Posts />
</Grid>
<Grid item xs={12} sm={7}>
<Form />
</Grid>
</Grid>
</Container>
</Grow>
</Container>
I'm new to mui and following a tutorial.The problem is in tutorial the form
grid item is coming in row and here it is showing me vertically. I don't know what the issue is how can i align it horizontally

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

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>

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: using <Divider> breaks the grid

so I have this annoying issue I can't find a result for.
I'm new to material-ui and it feels like I'm missing something here...
I just want a divider between the grid items, without it breaking the order of the grid. What am I missing?
Sandbox: https://vpbyd.csb.app/
import React from "react";
import "./styles.css";
import {Grid, Typography, Divider} from '#material-ui/core'
export default function App() {
return (
<div className="App">
<Grid container spacing={3}>
<Grid item xs={4}>
<Typography variant="h5" component="h2">
One
</Typography>
</Grid>
<Grid item xs={4}>
<Typography variant="h5" component="h2">
Two
</Typography>
</Grid>
<Grid item xs={4}>
<Typography variant="h5" component="h2">
Three
</Typography>
</Grid>
</Grid>
<Grid container spacing={0} alignItems="center">
<Grid item xs={4}>
<Typography variant="h6" component="h2">
first value
</Typography>
</Grid>
<Divider orientation="vertical" flexItem/>
<Grid item xs={4}>
<Typography variant="h6" component="h2">
second value
</Typography>
</Grid>
<Divider orientation="vertical" flexItem/>
<Grid item xs={4}>
<Typography variant="h6" component="h2">
third value
</Typography>
</Grid>
</Grid>
</div>
);
}
I had the same issue and found a trick : add a negative right margin to your Divider
<Grid item xs={4}>
<Typography variant="h6" component="h2">
first value
</Typography>
</Grid>
<Divider orientation="vertical" flexItem sx={{ mr: "-1px" }} />
<Grid item xs={4}>
<Typography variant="h6" component="h2">
second value
</Typography>
</Grid>
Try putting the divider inside of a grid container of its own.
<Grid item xs={2}>
<Divider orientation="vertical" flexItem/>
</Grid>
The material ui grid uses flexbox so dropping an item inside of it that does not have the correct properties is going to mess up the grid.
A good generic solution to me is adding this global style
.MuiGrid-container > .MuiDivider-vertical.MuiDivider-flexItem {
margin-right: -1px;
}
in conjunction with adding the JSX property flexItem to the Divider:
<Grid container>
<Grid item xs={4}>
...
</Grid>
<Divider flexItem orientation="vertical" />
<Grid item xs={8}>
...
</Grid>
</Grid>
That's a clean, transparent workaround to me which keeps the grid flowing as expected.
None of those solutions above were ideal for me, so I ended up simulating a Divider by showing the right side left border (you could obviously also do it by showing the left side right border), with the same CSS properties that the Divider uses. Here's a small example:
<Grid
container
direction="row"
justifyContent="space-evenly"
alignItems="top"
>
<Grid
item
xs={6}
style={{
paddingRight: '10px',
}}
Left Side
</Grid>
<Grid
item
xs={6}
style={{
paddingLeft: '10px',
borderStyle: 'solid',
borderColor: 'rgba(0, 0, 0, 0.12)',
borderWidth: '0 0 0 1px'
}}
>
Right Side
</Grid>
</Grid>
note that you are passing 14 column in total to de grid container, at least on the sandbox. Anyway sometimes it happen with 12 columns, it work for me to pass empty breakpoints props at the last grid item, or even for all the Grid Items in your case will be:
import React from "react";
import "./styles.css";
import { Grid, Typography, Divider } from "#material-ui/core";
export default function App() {
return (
<div className="App">
<Grid container spacing={3}>
<Grid item xs={4}>
<Typography variant="h5" component="h2">
One
</Typography>
</Grid>
<Grid item xs={4}>
<Typography variant="h5" component="h2">
Two
</Typography>
</Grid>
<Grid item xs={4}>
<Typography variant="h5" component="h2">
Three
</Typography>
</Grid>
</Grid>
<Grid container spacing={0} alignItems="center">
<Grid item xs={4}>
<Typography variant="h6" component="h2">
first value
</Typography>
</Grid>
<Divider orientation="vertical" flexItem />
<Grid item xs={4}>
<Typography variant="h6" component="h2">
second value
</Typography>
</Grid>
<Divider orientation="vertical" flexItem />
<Grid item **xs**>
<Typography variant="h6" component="h2">
third value
</Typography>
</Grid>
</Grid>
</div>
);
}
This will say to the item to occupy all the available space.
The problem is that giving the 12 columns you are stablishing the 100% of the available space, and adding a divider then you have 100% + 1px. normally without grid you'll need to set a calc(100% - 1px).

What's the right way to float right or left using the material-ui appbar with material-ui-next?

I can't figure out if I'm using the right approach to get the login/logout buttons to float right in while using material-ui-next ("material-ui": "^1.0.0-beta.22",)
It seems they removed iconElementRight= from the api. Do we have to use the <Grid> now in the appbar? It feels kinds of cludgy. What's the right way to float buttons (e.g. login) in the appbar?
<AppBar position="static">
<Toolbar>
<Grid container spacing={24}>
<Grid item xs={11}>
<Typography type="title" color="inherit">
Title
</Typography>
</Grid>
<Grid item xs={1}>
<div>
<HeartIcon />
<Button raised color="accent">
Login
</Button>
</div>
</Grid>
</Grid>
</Toolbar>
</AppBar>
#Kyle You had it right :)
just add to the grid container:
justify="space-between"
With your example:
<AppBar position="static">
<Toolbar>
<Grid
justify="space-between" // Add it here :)
container
spacing={24}
>
<Grid item>
<Typography type="title" color="inherit">
Title
</Typography>
</Grid>
<Grid item>
<div>
<HeartIcon />
<Button raised color="accent">
Login
</Button>
</div>
</Grid>
</Grid>
</Toolbar>
</AppBar>
You need to add flex: 1 to your <Typography /> component so it pushes the <div /> to the rightmost part of the AppBar:
<AppBar position="static">
<Toolbar>
<Typography type="title" color="inherit" style={{ flex: 1 }}>
Title
</Typography>
<div>
<HeartIcon />
<Button raised color="accent">
Login
</Button>
</div>
</Toolbar>
</AppBar>
<Toolbar>
<Box sx={{ flexGrow: 1 }}>
<Button variant='text' color='inherit'>Button1</Button>
<Button variant='text' color='inherit'>Button2</Button>
<Button variant='text' color='inherit'>Button3</Button>
</Box>
<Button variant='text' color='inherit'>Button4</Button>
</Toolbar>
Now Button4 will be positioned in the far right!
Just use the property align="right"
as shown here https://mui.com/api/typography/
Fresh variant:
<Grid
container
direction="row"
justifyContent="space-between"
alignItems="center"
>
<Grid item>Back</Grid>
<Grid item>Next</Grid>
</Grid>

Resources