How to create mui slider ios in react js?
September 27, 2023Hi Friends 👋,
Welcome To aGuideHub!
To create mui slider ios in react js, you can use <IOSSlider>
. it will create mui slider ios in React JS.
Today, I am going to show you, How to create mui slider ios in react js
Installation
Install the following packages to use mui slider in react js.
npm
npm install @mui/material @emotion/react @emotion/styled
yarn
yarn add @mui/material @emotion/react @emotion/styled
Table of contents
- Install MUI and create a new React app.
- Import Material-UI slider.
- Use the slider Component
Step 1: Install MUI and create a new React app.
First you have to install the React project. You should use create-react-app
command to create a new React project.
npx create-react-app my-app
cd my-app
npm start
Step 2: Import Material-UI slider.
After installing MUI
, you have to import your React component. To do this, add the following line to the top of your component file.
import * as React from "react";
import Slider from "@mui/material/Slider";
import { styled } from "@mui/material/styles";
import Typography from "@mui/material/Typography";
import Box from "@mui/material/Box";
Step 3: Use the slider Component.
Sliders reflect a range of values along a bar, from which users may select a single value. They are ideal for adjusting settings such as volume, brightness, or applying image filters.
<IOSSlider
aria-label="ios slider"
defaultValue={60}
marks={marks}
valueLabelDisplay="on"
/>
MUI material create mui slider ios example.
The below code is an example, you need to import slider
Component. Then, you can use IOSSlider
component. Then it will create mui slider ios in react js.
App.js
import * as React from "react";
import Slider from "@mui/material/Slider";
import { styled } from "@mui/material/styles";
import Typography from "@mui/material/Typography";
import Box from "@mui/material/Box";
const iOSBoxShadow =
"0 3px 1px rgba(0,0,0,0.1),0 4px 8px rgba(0,0,0,0.13),0 0 0 1px rgba(0,0,0,0.02)";
const marks = [
{
value: 0
},
{
value: 20
},
{
value: 37
},
{
value: 100
}
];
const IOSSlider = styled(Slider)(({ theme }) => ({
color: theme.palette.mode === "dark" ? "#3880ff" : "#3880ff",
height: 2,
padding: "15px 0",
"& .MuiSlider-thumb": {
height: 28,
width: 28,
backgroundColor: "#fff",
boxShadow: iOSBoxShadow,
"&:focus, &:hover, &.Mui-active": {
boxShadow:
"0 3px 1px rgba(0,0,0,0.1),0 4px 8px rgba(0,0,0,0.3),0 0 0 1px rgba(0,0,0,0.02)",
// Reset on touch devices, it doesn't add specificity
"@media (hover: none)": {
boxShadow: iOSBoxShadow
}
}
},
"& .MuiSlider-valueLabel": {
fontSize: 12,
fontWeight: "normal",
top: -6,
backgroundColor: "unset",
color: theme.palette.text.primary,
"&:before": {
display: "none"
},
"& *": {
background: "transparent",
color: theme.palette.mode === "dark" ? "#fff" : "#000"
}
},
"& .MuiSlider-track": {
border: "none"
},
"& .MuiSlider-rail": {
opacity: 0.5,
backgroundColor: "#bfbfbf"
},
"& .MuiSlider-mark": {
backgroundColor: "#bfbfbf",
height: 8,
width: 1,
"&.MuiSlider-markActive": {
opacity: 1,
backgroundColor: "currentColor"
}
}
}));
export default function CustomizedSlider() {
return (
<Box sx={{ width: 320 }}>
<Typography gutterBottom>iOS</Typography>
<IOSSlider
aria-label="ios slider"
defaultValue={60}
marks={marks}
valueLabelDisplay="on"
/>
</Box>
);
}
In the above code example, I have used the @mui/material
component and create mui slider ios in react js.
Check the output of the above code example.
Here, we are provided code sandbox links for the above program create mui slider ios in react js. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍