Reputation: 57
Using React Material UI's [slider]:(https://material-ui.com/components/slider/#slider)and [button]:(https://material-ui.com/api/button/).
The goal is to change the border radius of the button with the slider but struggling to grab the value variable and to figure out a way to apply it to the Button component.
I am brain dead due to trial and error. Can someone help me out or give me good advice?
import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Grid from "@material-ui/core/Grid";
import Slider from "@material-ui/core/Slider";
import Input from "@material-ui/core/Input";
import Button from "@material-ui/core/Button";
const useStyles = makeStyles((theme) => ({
root: {
"& > *": {
margin: theme.spacing(1)
}
},
Button: {
width: 150,
height: 50,
borderRadius: "var(--borderRadius)"
}
}));
export default function InputSlider() {
const classes = useStyles();
const [value, setValue] = React.useState(30);
const handleSliderChange = (event, newValue) => {
setValue(newValue);
};
const handleInputChange = (event) => {
setValue(event.target.value === "" ? "" : Number(event.target.value));
};
const handleBlur = () => {
if (value < 0) {
setValue(0);
} else if (value > 100) {
setValue(100);
}
};
return (
<div className={classes.root}>
<style>
{`:root {
--borderRadius = ${value}px;
}`}
</style>
<Button
variant="contained"
color="primary"
value="value"
onChange={handleSliderChange}
className={classes.Button}
>
Fire laser
</Button>
<Grid container spacing={2}>
<Grid item xs>
<Slider
value={typeof value === "number" ? value : 0}
onChange={handleSliderChange}
aria-labelledby="input-slider"
/>
</Grid>
<Grid item>
<Input
value={value}
margin="dense"
onChange={handleInputChange}
onBlur={handleBlur}
inputProps={{
step: 10,
min: 0,
max: 24,
type: "number"
}}
/>
</Grid>
</Grid>
</div>
);
}
Here the work in a [sandbox]: (https://codesandbox.io/s/material-demo-forked-nm2qw?file=/demo.js:0-1878)
Upvotes: 1
Views: 4398
Reputation: 6405
You can change the borderRadius
of a Button
using the style
prop.
Just add this to your Button
,
style={{ borderRadius: value }}
Check this sandbox for working example.
Upvotes: 3