Material UI Grid off center when has background color - material-ui

Grid No Background - centered
Material UI grid looks centered without a background.
Grid Background - not centered
When Material UI grid and grid items have a border and background, you can see they're not centered. They are closer to the left. Only the text and elements inside the grid and grid items are centered. The grid and grid items themselves are not centered.
How would one add a background to the grid so the grid still looks centered?
Codesandbox
import React, { useState } from "react";
import { Grid } from "#mui/material";
export default function GridTest() {
const [hasBackground, setHasBackground] = useState(true);
return (
<>
<div
style={{
border: "3px solid purple",
padding: "40px",
position: "relative"
}}
>
<h2
style={{
position: "relative",
top: "-50px",
color: "purple"
}}
>
div
</h2>
<h2
style={{
position: "relative",
color: "green"
}}
>
Grid
</h2>
<Grid
container
spacing={3}
style={
hasBackground
? {
background: "#bbe4c3",
border: "3px solid green"
}
: { border: "3px solid white" }
}
>
<Grid
item
style={
hasBackground
? {
color: "red",
border: "3px solid red"
}
: { border: "3px solid white" }
}
xs={6}
>
grid item
<div
style={{
color: "blue",
border: "3px solid blue"
}}
>
div inside grid Item
</div>
</Grid>
<Grid
item
style={
hasBackground
? {
color: "red",
border: "3px solid red"
}
: { border: "3px solid white" }
}
xs={6}
>
grid item
<div
style={{
color: "blue",
border: "3px solid blue"
}}
>
div inside grid Item
</div>{" "}
</Grid>
</Grid>
</div>
<button
onClick={() => {
setHasBackground(!hasBackground);
}}
>
toggle grid and grid items backgrounds/borders
</button>
</>
);
}

Add a div around the grid and give that a background instead.
Codesandbox
import React, { useState } from "react";
import { Grid } from "#mui/material";
export default function GridTest() {
const [hasBackground, setHasBackground] = useState(true);
return (
<>
<div
style={{
border: "3px solid purple",
padding: "40px",
position: "relative"
}}
>
<h2
style={{
position: "relative",
top: "-50px",
color: "purple"
}}
>
div
</h2>
<h2
style={{
top: "20px",
position: "relative",
color: "green"
}}
>
Grid
</h2>
<span
style={{
position: "relative",
color: "green"
}}
>
div
</span>
<div
style={{
background: "#bbe4c3",
border: "3px solid green"
}}
>
<Grid
container
spacing={3}
style={
hasBackground
? {
background: "#bbe4c3",
border: "3px solid green"
}
: {}
}
>
<Grid
item
style={
hasBackground
? {
color: "red",
border: "3px solid red"
}
: {}
}
xs={6}
>
grid item
<div
style={{
color: "blue",
border: "3px solid blue"
}}
>
div inside grid Item
</div>
</Grid>
<Grid
item
style={
hasBackground
? {
color: "red",
border: "3px solid red"
}
: {}
}
xs={6}
>
grid item
<div
style={{
color: "blue",
border: "3px solid blue"
}}
>
div inside grid Item
</div>{" "}
</Grid>
</Grid>
</div>
</div>
<button
onClick={() => {
setHasBackground(!hasBackground);
}}
>
toggle grid and grid items backgrounds/borders
</button>
</>
);
}

Related

How do I set space between control and label of `FormControlLabel` inside a grid?

I am trying to make space between control switch and it's label. what property of FormControlLabel would set it correct?
my code snippet:
switchContainer: {
display: 'flex',
flexDirection: 'row',
justifyContent: 'flex-start',
paddingTop: theme.spacing(2),
paddingBottom: theme.spacing(2),
},
<Grid className={classes.switchContainer} sx={{ boxShadow: 2 }}>
<FormControlLabel onClick={setDarkMode} control={<Switch />} label={dark ? 'Dark' : 'Light'} labelPlacement="start" />
</Grid>

How to resize chart.js graph dynamically for responsive sites?

Hej,
I know this has been answered in one way or the other many times, but I really can't get it work in my context with flex-boxes.
I have fixed elements, such as headers and footers on my page, and a chart that shall take up all the remaining space in between. While it appears to be working on first glance, you can see that the text and numbers are blurred and not properly scaled when the window is resized.
How can I fix this, or have chart.js redraw the canvas upon resize?
Check out my fiddle
https://jsfiddle.net/fg9pd1b3/
<body>
<div class="container">
<div class="fixed">
header
</div>
<div id="chartcontainer">
<canvas id="chartJSContainer"></canvas>
</div>
<div class="fixed">
footer
</div>
</div>
</body>
CSS:
html,
body {
height: 100vh;
min-height: 100vh;
max-height: 100vh;
margin: 0;
padding: 0;
}
.container {
width: 80%;
height: 100%;
margin: 0 auto;
display: flex;
flex-direction: column;
flex-basis: 100%;
flex-grow: 1;
background: #CCC;
}
.p {
text-align: center;
font-size: 14px;
padding-top: 140px;
}
.fixed {
background: black;
height: 100px;
min-height: 100px;
color: white;
text-align: center;
}
#chartcontainer {
position: relative;
min-height: 200px;
display: flex;
flex-grow: 1;
}
canvas {
width: 100% !important;
height: 100% !important;
}
JS:
Chart.defaults.global.responsive = true;
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [{
ticks: {
reverse: false
}
}]
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
You have given fixed min-height to the canvas div which restricts the chart to further change it's dimensions. So, the following code has auto height and width automates to 100% which gives flexible chart.
#chartcontainer {
position:relative;
min-height:auto;
width:100%;
display: flex;
flex-grow:1;
}

Center buttons in flex in Appbar

I have trouble aligning/justifying as centered buttons in the AppBar.
It looks like this (see demo here):
<AppBar position='sticky'>
<Toolbar>
<div className={classes.topMenuBoxes}>
<img className={classes.logoSize} src={LogoFull} alt="logo" ></img>
</div>
<div className={classes.topMenuBoxes}>
<Button className={classes.menuText} color='inherit' >Offer</Button>
<Button className={classes.menuText} color='inherit' >Join</Button>
<Button className={classes.menuText} color='inherit' component={AboutLink}>About us</Button>
</div>
<div className={classes.topMenuBoxes}>
<DropMenu />
</div>
</Toolbar>
</AppBar>
...and using this makeStyles:
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
},
logoSize: {
marginTop: '30px',
height: '70px',
},
menuButton: {
[theme.breakpoints.up('md')]: {
display: 'none',
},
marginRight: theme.spacing(2),
},
menuText: {
[theme.breakpoints.down('sm')]: {
display: "none",
},
textTransform: 'none',
},
topMenuBoxes: {
flex: 1,
},
}));
How do I align content inside the divs where I have referenced classes.topMenuBoxes?
Thanks!
Add textAlign:"center" to your topMenuBoxes:
const useStyles = makeStyles(theme => ({
// ...
topMenuBoxes: {
flex: 1,
textAlign: "center"
}
}));

Focus pseudo-class in Material ui

I am trying to apply some styles when a material-ui form control gets focus.
The pseudo-class 'hover' works well in the code below but 'focus' does not.
I am assuming that clicking into the Input component gives it focus but that does not seem to work. Any idea why and how to make it work?
import withStyles from "#material-ui/core/styles/withStyles"
import Input from "#material-ui/core/Input"
import InputLabel from "#material-ui/core/InputLabel"
import FormControl from "#material-ui/core/FormControl"
const styles = theme => ({
root: {
border: "1px solid",
borderRadius: theme.shape.borderRadius,
borderColor: "rgba(0, 0, 0, 0.23)",
marginBottom: theme.spacing.unit * 2,
padding: theme.spacing.unit,
width: "100%",
display: "flex",
flexDirection: "row",
alignItems: "center",
"&:focus": {
backgroundColor: "blue"
}
}
})
class TagsComponent extends React.Component {
render() {
return (
<FormControl variant="outlined">
<InputLabel>Tags</InputLabel>
<Input
disableUnderline
classes={{
root: this.props.classes.root
}}
/>
</FormControl>
)
}
}
export default withStyles(styles)(TagsComponent)
You can apply styles by overriding styles for input or override class name focused
const styles = {
...,
focused: {
backgroundColor: "green"
},
input: {
"&:focus": {
backgroundColor: "blue",
color: "white"
}
}
}
<Input
disableUnderline
classes={{
root: classes.root,
focused: classes.focused,
input: classes.input
}}
/>
Sandbox example https://codesandbox.io/embed/gallant-pascal-rh8jc

Echarts - Scrollbar outside the canvas

I am using echarts library for bar charts. I want scrollbar to display outside the canvas. Is there any way I can accomplish this?
Unfortunately, Echarts don't support scrollbar option.
However you can simply achieve this use a wrap DOM.
check this demo:
let echartsObj = echarts.init(document.querySelector('#canvas1'));
option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [120, 200, 150, 80, 70, 110, 130],
type: 'bar'
}]
};
echartsObj.setOption(option)
echartsObj = echarts.init(document.querySelector('#canvas2'));
option = {
xAxis: {},
yAxis: {},
series: [{
symbolSize: 20,
data: [
[10.0, 8.04],
[8.0, 6.95],
[13.0, 7.58],
[9.0, 8.81],
[11.0, 8.33],
[14.0, 9.96],
[6.0, 7.24],
[4.0, 4.26],
[12.0, 10.84],
[7.0, 4.82],
[5.0, 5.68]
],
type: 'scatter'
}]
};
echartsObj.setOption(option)
.echart-wrap {
display: inline-block;
width: 300px;
height: 300px;
overflow: scroll;
border: 1px solid black;
}
<html>
<header>
<script src="https://cdn.bootcss.com/echarts/4.1.0.rc2/echarts-en.min.js"></script>
</header>
<body>
<div class="echart-wrap">
<div id="canvas1" style="width: 500px; height: 500px">
</div>
</div>
<div class="echart-wrap">
<div id="canvas2" style="width: 500px; height: 500px">
</div>
</div>
</body>
</html>