Material UI is a Material Design library made for React.
It’s a set of React components that have Material Design styles.
In this article, we’ll look at how to add sliders with Material UI.
We can add sliders to snaps to discrete values.
To do that, we set the marks
prop to true
.
For instance, we can write:
import React from "react";
import Slider from "@material-ui/core/Slider";
export default function App() {
return (
<form>
<Slider marks />
</form>
);
}
to make an uncontrolled slider that snaps to the nearest 10.
We can change the steps with the steps
prop.
For example, we can write:
import React from "react";
import Slider from "@material-ui/core/Slider";
export default function App() {
return (
<form>
<Slider step={0.00001} marks />
</form>
);
}
to change the increment between each discrete value.
We can add our own custom marks with the marks
prop.
For example, we can write:
import React from "react";
import Slider from "@material-ui/core/Slider";
const marks = [
{
value: 0,
label: "0m"
},
{
value: 25,
label: "25m"
},
{
value: 50,
label: "50m"
},
{
value: 75,
label: "75m"
},
{
value: 100,
label: "100m"
}
];
export default function App() {
return (
<form>
<Slider step={10} marks={marks} valueLabelDisplay="auto" />
</form>
);
}
We add the marks
array with the value
and label
properties in each entry to show the labels with the text in the label
property.
We can restrict values to the ones that are in the marks
array by setting step
to null
.
For example, we can write:
import React from "react";
import Slider from "@material-ui/core/Slider";
const marks = [
{
value: 0,
label: "0m"
},
{
value: 25,
label: "25m"
},
{
value: 50,
label: "50m"
},
{
value: 75,
label: "75m"
},
{
value: 100,
label: "100m"
}
];
export default function App() {
return (
<form>
<Slider step={null} marks={marks} valueLabelDisplay="auto" />
</form>
);
}
Now we can only select the values that are marked in the marks
array because of the null
value we passed into the step
prop.
#software-development #technology #programming #web-development #slider