Material UI Stack Component is not displaying flex correctly - reactjs

I want to implement flex system from material-ui using the Stack component Im faced with a issue. The other half of my screen is displaying another component or blank space and I dont know how to get rid of it.
I'm using the Stack component from material-ui and the normal html iframe tag to build a code editor. Any Idea how I can fix this using Stack?
Screenshot of UI:
Here is my code:
index.js
<Stack sx={{ bgcolor: 'white', pt: 1 }}>
<TabContext value={value} >
<Box sx={{ borderBottom: 1, borderColor: 'divider', bgcolor: 'white' }}>
<TabList onChange={handleChange} >
<Tab label="HTML" value="1" />
<Tab label="CSS" value="2" />
<Tab label="JS" value="3" />
<Tab label="Output" value="4" sx={{ display: { xs: 'block', md: 'none' }, mt: 1 }} />
</TabList>
</Box>
{value === '4' ?
<Stack sx={{ height: 'calc(100vh - 201px)' }}>
<iframe title="result"
sandbox="allow-scripts"
frameBorder="0"
width="100%"
srcDoc={iframeDisplay}
/>
</Stack>
:
<Stack direction="row" sx={{ width: '100%', height: '100%', position: 'relative'}}>
<Stack sx={{ width: { xs: '100%', md: '50%' }, bgcolor: "white" }}>
<TabPanel value={value} sx={{ p: 0 }}>
<CodeMirror
value={codeEditor.value}
height={CODE_EDITOR_PROPS.height}
theme={CODE_EDITOR_PROPS.theme}
extensions={codeEditor.extensions}
onChange={onChange}
/>
</TabPanel>
</Stack>
<Box sx={{ display: { xs: 'none', md:'flex' }, height: 'calc(100vh - 201px)', width: 'calc(50% - 1024px)'}}>
<iframe
title="result"
frameBorder="0"
height="calc(100vh - 201px)"
srcDoc={iframeDisplay}
/>
</Box>
</Stack>}
</TabContext>
</Stack>

Related

Why aren't the buttons and links to page sections in the material ui drawer not working

I have a web app that uses a material ui drawer for smaller screens. The buttons on the top navigation pane work as expected( take the user to a section of the page ) but the ones in the drawer(created for responsiveness) don't. I am using react 18.2.0, typescript 4.9.4 and material ui 5.11.6
<AppBar>
<Toolbar sx={{
display: "flex",
justifyContent : "flex-end",
bgcolor : "whitesmoke",
color : "black"
}}>
<Stack component={"span"} spacing={.5} direction="row" sx={{
color: "inherit",
margin: "0px 15px 0px 0px",
position : "absolute",
left: 0
}}>
<Typography sx={{
fontFamily: "'Pacifico'",
fontSize : "1.2rem"
}}>{"< George Kimari />"}</Typography>
</Stack>
<Stack spacing={2} direction="row" component={"span"} sx={{
display : {
xs : "none",
sm: "flex"
},
justifySelf: "flex-end"
}}>
/*The button below works*/
<Button color={"inherit"} sx={ButtonTheme} href="#about">About me</Button>
</Stack>
<IconButton onClick={() => setDrawerOpen(true)}>
<Menu sx={{
color: "inherit",
display : {
xs : "flex",
sm : "none",
md : "none"
}
}}/>
</IconButton>
<Drawer open={drawerOpen} onClose={() => setDrawerOpen(false) } anchor="right">
/*The link and button below don't work
<Stack width={"200px"} sx={{
height: "70px",
display : "grid",
placeContent : "center"
}}><Link href={"#about"}component={"button"}>About</Link></Stack>
<Divider />
<Stack width={"200px"} sx={{
height: "70px",
display : "grid",
placeContent : "center"
}}><Button sx={{
width : "200px",
height : "50px"
}} color={"inherit"} href="#projects">Projects</Button></Stack>
</Drawer>
</Toolbar>
</AppBar>
)
}
I have tried replacing them with links to no avail.

Center content in Material UI Box

I want the Box content on the right (Textfields, etc.) as shown in the image below to be always centered even in responsive,
I tried justifyContent="center" in <Box > but nothing happened it didn't move at all also tried justifyContent="center" in <Stack > also nothing happened, I'm not sure if I missed something or there is another way to do it!!
Some help would be appreciated.
The code:
return (
<div className="login">
<Box>
<Stack direction="row"
// spacing={1} >
{/* FIRST BOX ON THE LEFT WITH THE IMAGE & LOGO */}
<Box sx={{
width: "40%",
height: "100%",
backgroundColor: '#3AAFA9',
display: {xs: "none", md: "none", sm: "none", lg:"block"},
}}
>
{/* TO HOLD IMAGE, LOGO & DISCRIPTION */}
<Stack direction="column"
spacing={-2}
sx={{ display: 'block' }}
py={10}
ml={7}>
{/* STUFF */}
</Stack>
</Box>
{/* HERE WHERE THE PROBLEM IS*/}
<Box component="div"
// sx= {{
// padding:{ xs: 2, sm: 4, md: 4, lg:10 },
// }}
justifyContent= "center">
<Stack
direction='column'
width="100%"
justifyContent="flex-start"
// sx={{ ml: 10 }}
>
<Typography variant="h5"
gutterBottom
component="span"
style={{ fontWeight: 600, textAlign: 'left' }}
>
Login to
</Typography>
<Divider style={{width:'100%'}}>
Or
</Divider>
{/* LOGIN FORM */}
<form className="loginForm" onSubmit={handleSubmit}>
</form>
{/* LOGIN FORM ENDS */}
</Stack>
</Box>
</Stack>
</Box>
</div>
)
The Box-Component of Mui dont have a prop justifyContent.
You have to declare it inside youre sx.
sx={{
display: "flex",
justifyContent: "space-between",
width: "100%",
//...
}}
The width is just to say the box it have to use the hole space.
Cheers

How to make paper responsive - Material Ui

I'm making an app, and I am currently working on making the login page.
I want the login tools to be responsive, centered by width and height. justify-content :
'center' was not working for me, but it seemed to be working with other people's code, so I am not sure what the problem is.
This is the image of the website
My Body Code
<div>
<Paper elevation={10} className={classes.root}>
<Grid align="center">
<h2 className={classes.title}>Log in </h2>
</Grid>
<form onSubmit={this.submitLogin}>
<Paper className={classes.paper}>
<TextField
id="standard-basic"
label="Email"
name="name"
type="text"
value={this.state.name}
onChange={this.handleInputChange}
fullWidth
required
/>
</Paper>
<Paper className={classes.paper}>
<TextField
id="standard-basic"
label="Password"
name="password"
type="password"
value={this.state.password}
onChange={this.handleInputChange}
fullWidth
required
/>
</Paper>
<Typography className={classes.pf}>
<Link href="">Forgot Password?</Link>
</Typography>
<Typography className={classes.pf}>{this.state.text}</Typography>
<Tooltip title="Will Remember You When You Come Back">
<FormControlLabel
control={<Checkbox name="checkedB" color="primary" />}
label="Remember Me"
/>
</Tooltip>
<Button
type="submit"
className={classes.log}
onClick={this.submitLogin}
variant="contained"
fullWidth
>
LOG IN
</Button>
</form>
<Typography className={classes.fp}>
Haven't created an account?
<Tooltip title="Go to the Sign Up page">
<Link href="./register"> Sign Up</Link>
</Tooltip>
</Typography>
</Paper>
</div>
My Theme Code
const styles = (theme) => ({
root: {
padding: 50,
height: 450,
width: 400
},
title: {
fontSize: "30px"
},
paper: {
marginTop: "15px"
},
log: {
background: "#3f51b5",
color: "white",
border: "0px",
borderRadius: "5px",
borderColor: "grey",
fontSize: "20px",
height: "45px",
marginTop: "5%"
},
fp: {
marginTop: "80px",
marginLeft: "65px"
},
pf: {
marginTop: "8px"
}
});
I couldn't really find the 'working' solutions for me, so I'm posting a question
Wrap your Paper component in a div with the styles you mention, that should work as expected:
<div style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
// Change the size to fit the parent element of this div
width: '100%',
height: '100%',
}}>
<Paper>
SomeStuff...
</Paper>
</div>

Get InputBase value out from the same component

I have different places that use the same input component.
Below are the input component:
const stationSelection = (stationName) => {
return (
<Paper
component="form"
elevation={5}
sx={{
m: '10px 0 30px 5px',
p: '2px 4px',
display: 'flex',
alignItems: 'center',
width: 300,
}}
>
//HERE
<InputBase
sx={{ ml: 1, flex: 1 }}
placeholder="Please select a station."
value={stationName}
onChange={ /*how to get the data each of the input*/ }
/>
<Divider sx={{ height: 28, m: 0.5 }} orientation="vertical" />
<IconButton
color="primary"
sx={{ p: '10px' }}
aria-label="directions"
>
<ArrowDropDownCircleOutlinedIcon />
</IconButton>
</Paper>
)
}
In my react app, I will use it two times for different input.
<CardContent className="default-font">
<Box sx={{ mb: 10 }}>
<Box className="bold">DEPARTURE STATION</Box>
{stationSelection(departStation)} //HERE
<Box className="center">
<Box className="bold" sx={{ flexGrow: 1 }}>
ARRIVAL STATION
</Box>
<IconButton
color="primary"
sx={{ p: '5px', m: '-10px 0 -10px 0' }}
>
<ImportExportIcon />
</IconButton>
</Box>
{stationSelection(arriveStation)} //HERE
</Box>
{ticketDetails()}
</CardContent>
Because I use it two times by using the different variables into the same component, it makes it difficult for me to found out a way to get the data out from the component.
Here are my useState in case you need that.
const [departStation, setDepartStation] = useState('');
const [arriveStation, setArriveStation] = useState('');
Basically, I need to get that two user input.
Full code link.
First, you functional component should start with a capital letter, so change your declaration to:
const StationSelection = ...
Second, in order to edit the state in your component, you should also pass the setter method, and change it in the onChange function:
const StationSelection = ({ stationName, setStationName }) => {
const handleStationChange = (e) => {
setStationName(e.target.value)
}
return (
<Paper
component="form"
elevation={5}
sx={{
m: '10px 0 30px 5px',
p: '2px 4px',
display: 'flex',
alignItems: 'center',
width: 300,
}}
>
//HERE
<InputBase
sx={{ ml: 1, flex: 1 }}
placeholder="Please select a station."
value={stationName}
onChange={handleStationChange}
/>
<Divider sx={{ height: 28, m: 0.5 }} orientation="vertical" />
<IconButton
color="primary"
sx={{ p: '10px' }}
aria-label="directions"
>
<ArrowDropDownCircleOutlinedIcon />
</IconButton>
</Paper>
)
}
Finally, change your main component like this
<CardContent className="default-font">
<Box sx={{ mb: 10 }}>
<Box className="bold">DEPARTURE STATION</Box>
<StationSelection stationName={departStation} setStationName={setDepartStation} />
<Box className="center">
<Box className="bold" sx={{ flexGrow: 1 }}>
ARRIVAL STATION
</Box>
<IconButton
color="primary"
sx={{ p: '5px', m: '-10px 0 -10px 0' }}
>
<ImportExportIcon />
</IconButton>
</Box>
<StationSelection stationName={arriveStation} setStationName={setArriveStation} />
</Box>
{ticketDetails()}
</CardContent>

How to stretch the card according to the content displayed in reactjs?

I'm new to material UI framework, i want to stretch the grid accordingly displayed in the content. I couldn't able to do that. Can anyone help me in this?
<Card>
<CardHeader
avatar={<Avatar aria-label="recipe">S</Avatar>}
title={
<>
<InputBase placeholder="Search Google Maps" margin="normal" />
<IconButton type="submit" aria-label="search">
<SearchIcon />
</IconButton>
</>
}
/>
<Divider />
<CardContent
style={{ overflow: "scroll" }}
className={classes.contentHeight}
id="chatList"
>
<div>
<Message isSender content="Hello" />
{this.state.data.map(item => {
if (item.isSender) {
return (
<Message isSender name="Sanjana" content={item.content} />
);
}
return <Message name="Sanjana" content={item.content} />;
})}
</div>
</CardContent>
<Divider />
<CardActions>
<Paper className={classes.contentPaper}>
<Input
margin="dense"
className={classes.input}
placeholder="Enter a Message"
disableUnderline
/>
</Paper>
</CardActions>
</Card>
Can anyone help me in this query?
Thanks in advance!
Remove width: 100
<Card
style={{
backgroundColor: "#eee",
float: isSender ? "right" : "left",
padding: 16,
minHeight: 40,
width: 100,
display: "flex",
alignItems: "center",
textAlign: "center"
}}
>
{content}
</Card>
should be
<Card
style={{
backgroundColor: "#eee",
float: isSender ? "right" : "left",
padding: 16,
minHeight: 40,
display: "flex",
alignItems: "center",
textAlign: "center"
}}
>
{content}
</Card>
because the width: 100 gives it a width of 100px.
To make the content to stretch full width, you can use width: "fit-content" and assign your width value of 100 to minWidth: 100.
So your card will look like,
<Card
style={{
backgroundColor: "#eee",
float: isSender ? "right" : "left",
padding: 16,
diaplay: "block",
minHeight: 40,
width: "fit-content",
minWidth: 100,
display: "flex",
alignItems: "center",
textAlign: "center"
}}
>
To make the content left align inside card, you can assign style to Grid like,
<Grid style={{ paddingBottom: 8, textAlign: "left" }} item xs={12}>
<span>{name}</span>
</Grid>
Forked sandbox

Resources