Material-Ui How to adjust width to display items horizontally - css

I'm working on kanban/Trello clone app with Material-Ui,
I tried to display columns horizontally and keep columns the same width.
All columns are displayed horizontally in the same row.
If display area for columns requires to extend browser width, the display area width is automatically adjusted.
The issue is Material UI doesn't allow me to display columns with side scroll.
If I decided the display area directly, like width:2000. the all columns displayed horizontally,
but there is always redundant space.
Because the width is fixed, and we don't know how many columns users create.
What is the solution for this issue?
these are the code I tried.
1/wrapped child with Grid
<Grid container style={{margin: 0,width: "100%"}}>
{children}
</Grid>
2/set style like this
const useStyles = makeStyles({
root: {
display: "flex",
justifyContent: "flex-start",
alignContent: "center",
width: 20000,
overflowX: "auto"
}
3/use Container and Grid
<Container className={classes.container}>
<Grid container direction='row' justify='flex-start' alignItems='flex-start'>
{children}
</Container >
</Grid >
onst useStyles = makeStyles({
root: {
display: "flex",
justifyContent: "flex-start",
alignContent: "center"
// width: 20000,
// overflowX: "auto"
},
container: {
overflowY: "scroll",
whiteSpace: "nowrap"
}
});

Related

How to change the size of a component with effect when another component is rendered inside of it?

I have a modal component that overlays my page with a form to make some data inputs.
a second component is rendered next to the form when a certain condition is met (try to fetch data, render if anything is found).
When this happens I would like the whole card containing the two items to expand with some effect (horizontally if on desktop, vertically if on mobile), reducing smoothly the width of the first box during the transion.
I have tried to achieve this using a simple combination of the MUI Grid Container and Grid items,
but with this approach the second item always go on a new line or shows on the right but never changing the modal card overall size.
this is the style applied to the modal card:
sx={{
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
display: 'flex',
flexDirection: 'column',
transitionDuration: '500ms',
minWidth: '30%'
}}
So I thought of wrapping the 2 brothers in some DIV and apply some custom CSS taking advantage of the transitionDuration to apply an effect. But I couldn't make it work.
here is the style applied to the DIV that wraps the 2 brothers:
<div style={{ display: 'flex', flexDirection: 'row', width: '100%' }}>
this is the div that contains the form grid container (1):
<div
style={{
flexGrow: 1,
minWidth: '30%',
transitionDuration: '500ms',
transitionTimingFunction: 'ease-out'
}}
>
and this is the div that contains the component to be shown when condition is met (2):
<div
style={{
paddingLeft: 10,
transitionDuration: '500ms',
transitionTimingFunction: 'ease-in',
whiteSpace: 'nowrap',
minWidth: condition ? '200px' : '0%',
width: '0%'
}}
>
can anybody point me in the right direction?
I'm new to CSS
Thanks
When you fetch the data you can add a class (.active or something similar)to the parent container as well and add the styles you want to change like
container.active > child{Blahblah styles}

centering a div in a grid item

I've been trying to horizontally center a div on the screen of an Iphone. The div is a bit narrower than the phone screen. I have been googling and searching this site and trying every combination of display, flex, margin, width, justify*, align*, placeself, etc. etc. in the container as well as item style and nothing has worked. Here is the code, with all my attempts removed from the style since none of it worked and I assume may interfere with correct solution. No matter what I try, the div just stays on the left side of the screen.
{left} and {right} are divs, with width: 400px
<Mobile>
<Grid container style={{ }} >
<Grid item xs={12} style={{ }}>{left}{right}</Grid>
</Grid>
</Mobile>
const Mobile = ({ children }) => useMediaQuery({ maxWidth: 799 }) && children
Here is what finally worked for me after lots of trial and error.
<Mobile>
<Grid container style={{ justifyItems: 'center', alignItems: 'center', display: 'inline-grid', width: '100%' }} >
<Grid item xs={12} style={{ width: '400px' }}>{left}{right}</Grid>
</Grid>
</Mobile>
To clarify, my question was probably inaccurate. What I needed to do was center the grid item in the grid container.

Making titles of cards have same height

So I have the following situation:
From a certain point on the darkblue color of the boxes just doesn't align anymore. These two boxes are currently being rendered in a flex container. What would be a solution for this problem? I want the upper container to always be aligned with the one next to it. It is also possible that it will be more than 2 components next to each other.
Flex container (material ui Grid component uses Flex)
<Box>
<Grid container>
{resultField.fields?.map((blockField, index) => {
return (
<Grid item xs={12} md={6}>
<DarkValueBox label={blockField.label} value={blockField.value} />
</Grid>
);
})}
</Grid>
DarkValueBox
export const DarkValueBox = ({ label, value, index }) => {
const classes = useStyles();
return (
<Box sx={{ borderRadius: 5 }}>
<Box
sx={{
backgroundColor: "#0A7BB1",
color: "#fff",
px: 2,
py: 3,
borderRight: "1px solid #fff",
}}
>
<Typography fontWeight={600} variant="subtitle1">
{label}
</Typography>
</Box>
<Box sx={{ px: 2, py: 2, border: "1px solid lightgrey" }}>
<Typography variant="subtitle1">
{value}
</Typography>
</Box>
</Box>
);
};
Here's a codesandbox where I recreated the problem:
https://codesandbox.io/s/add-material-ui-forked-ge42z?file=/src/index.js
An approach would be - if you do not want to cut the labels for showing the user as much information as possible - to ensure the blue box is always taking the height of the highest container in the row.
You can do this with adding a wrapper class to the container DarkValueBox with following specification:
wrapper: {
height: "100%",
display: "flex",
flexDirection: "column"
}
Then, tell your box class to take 100%:
box: {
backgroundColor: "darkblue",
border: "1px solid grey",
color: "white",
wordBreak: "break-word", // btw: its `break-word`, not `word-break`
height: "100%"
},
That is it, find it working in a forked sandbox:
https://codesandbox.io/s/add-material-ui-forked-24zg7?file=/src/index.js
Ok, so I was going through your Sandbox and instead of writing the code I decided to point you in the right direction.
You have declared a Grid with material UI which is a type of container similar to FlexBox.
You have not declared a size or width for the box or the containers inside.
Once you set the grid size and wrap direction for the outer 'main' container you will be able to set the sizes for the containers inside, your label boxes.
This will align them.
For the text, there is a 'textOverflow' property which will stop any additional text wrapping over the component and instead replace it with '...'
Material UI Grid - https://mui.com/components/grid/
Textoverflow - CSS: text-overflow: ellipsis with reactJS and IE

How to stretch div to 100% width inside material ui TableBody

I'm trying to display a CircularProgress centered inside material UI TableBody. The below is what I have tried, but it's not centered as I expected. (Please note that only relevant code has been posted)
const styles = (theme) => ({
progress: {
width: "100%",
display: "flex",
justifyContent: "center",
minHeight: "10vh",
alignItems: "center"
}
})
<TableBody>
<div className={classes.progress}>
<CircularProgress />
</div>
</TableBody>
It looks like this, but I want it to be centered in the TableBody. Currently, it only takes the width of the first column.
Here your div extends 100% to the parent which is TableBody you can make table body 100% or give position: 'absolute' to div

How to display material-ui cards one next each others with space between them + go to next line when space is over in the row

Currently I am displaying on the same line like 10 cards and is enabled by default the scrolling horizontally and the cards are no spaced between them.
I read to use "flex-wrap=wrap" to avoid the scrolling but the cards are put all vertically on a single column...
My code is this
const useStyles = makeStyles((theme) => ({
root: {
// flexGrow: 1,
flexWrap: "wrap",
padding: theme.spacing(2),
display: "flex",
width: "50%",
},
}));
const classes = useStyles();
.
.
.
<Grid
container
direction="row"
justify-content="space-evenly "
>
<Grid>
<Card>
<CardContent>...
Any ideas on what am I doing wrong?

Resources