How to make mui progress bar with steps in react js?
October 09, 2023Hi Friends 👋,
Welcome To aGuideHub!
To make mui progress bar with steps in react js, you can use Stepper
component. it will make mui progress bar with steps in React JS.
Today, I am going to show you, How to make mui progress bar with steps in react js
Installation
Install the following packages to use mui progress 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 progress.
- Use the progress 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 progress.
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 React from "react";
import PropTypes from "prop-types";
import { makeStyles, withStyles } from "@material-ui/core/styles";
import clsx from "clsx";
import Stepper from "@material-ui/core/Stepper";
import Step from "@material-ui/core/Step";
import StepLabel from "@material-ui/core/StepLabel";
import Check from "@material-ui/icons/Check";
import SettingsIcon from "@material-ui/icons/Settings";
import GroupAddIcon from "@material-ui/icons/GroupAdd";
import VideoLabelIcon from "@material-ui/icons/VideoLabel";
import StepConnector from "@material-ui/core/StepConnector";
import Button from "@material-ui/core/Button";
import Typography from "@material-ui/core/Typography";
Step 3: Use the progress Component.
Steppers display progress through a sequence of logical and numbered steps. They may also be used for navigation. Steppers may display a transient feedback message after a step is saved.
<Stepper
alternativeLabel
activeStep={activeStep}
connector={<QontoConnector />}
>
{steps.map((label) => (
<Step key={label}>
<StepLabel StepIconComponent={QontoStepIcon}>{label}</StepLabel>
</Step>
))}
</Stepper>
MUI material make mui progress bar with steps example.
The below code is an example, you need to import progress
Component. Then, you can use activeStep={activeStep}
in Stepper. Then it will make mui progress bar with steps in react js.
App.js
import React from "react";
import PropTypes from "prop-types";
import { makeStyles, withStyles } from "@material-ui/core/styles";
import clsx from "clsx";
import Stepper from "@material-ui/core/Stepper";
import Step from "@material-ui/core/Step";
import StepLabel from "@material-ui/core/StepLabel";
import Check from "@material-ui/icons/Check";
import SettingsIcon from "@material-ui/icons/Settings";
import GroupAddIcon from "@material-ui/icons/GroupAdd";
import VideoLabelIcon from "@material-ui/icons/VideoLabel";
import StepConnector from "@material-ui/core/StepConnector";
import Button from "@material-ui/core/Button";
import Typography from "@material-ui/core/Typography";
const QontoConnector = withStyles({
alternativeLabel: {
top: 10,
left: "calc(-50% + 16px)",
right: "calc(50% + 16px)"
},
active: {
"& $line": {
borderColor: "#784af4"
}
},
completed: {
"& $line": {
borderColor: "#784af4"
}
},
line: {
borderColor: "#eaeaf0",
borderTopWidth: 3,
borderRadius: 1
}
})(StepConnector);
const useQontoStepIconStyles = makeStyles({
root: {
color: "#eaeaf0",
display: "flex",
height: 22,
alignItems: "center"
},
active: {
color: "#784af4"
},
circle: {
width: 8,
height: 8,
borderRadius: "50%",
backgroundColor: "currentColor"
},
completed: {
color: "#784af4",
zIndex: 1,
fontSize: 18
}
});
function QontoStepIcon(props) {
const classes = useQontoStepIconStyles();
const { active, completed } = props;
return (
<div
className={clsx(classes.root, {
[classes.active]: active
})}
>
{completed ? (
<Check className={classes.completed} />
) : (
<div className={classes.circle} />
)}
</div>
);
}
QontoStepIcon.propTypes = {
/**
* Whether this step is active.
*/
active: PropTypes.bool,
/**
* Mark the step as completed. Is passed to child components.
*/
completed: PropTypes.bool
};
function ColorlibStepIcon(props) {
const classes = useColorlibStepIconStyles();
const { active, completed } = props;
const icons = {
1: <SettingsIcon />,
2: <GroupAddIcon />,
3: <VideoLabelIcon />
};
return (
<div
className={clsx(classes.root, {
[classes.active]: active,
[classes.completed]: completed
})}
>
{icons[String(props.icon)]}
</div>
);
}
ColorlibStepIcon.propTypes = {
/**
* Whether this step is active.
*/
active: PropTypes.bool,
/**
* Mark the step as completed. Is passed to child components.
*/
completed: PropTypes.bool,
/**
* The label displayed in the step icon.
*/
icon: PropTypes.node
};
const useStyles = makeStyles((theme) => ({
root: {
width: "100%"
},
button: {
marginRight: theme.spacing(1)
},
instructions: {
marginTop: theme.spacing(1),
marginBottom: theme.spacing(1)
}
}));
function getSteps() {
return ["Select campaign settings", "Create an ad group", "Create an ad"];
}
function getStepContent(step) {
switch (step) {
case 0:
return "Select campaign settings...";
case 1:
return "What is an ad group anyways?";
case 2:
return "This is the bit I really care about!";
default:
return "Unknown step";
}
}
export default function CustomizedSteppers() {
const classes = useStyles();
const [activeStep, setActiveStep] = React.useState(1);
const steps = getSteps();
const handleNext = () => {
setActiveStep((prevActiveStep) => prevActiveStep + 1);
};
const handleBack = () => {
setActiveStep((prevActiveStep) => prevActiveStep - 1);
};
const handleReset = () => {
setActiveStep(0);
};
return (
<div className={classes.root}>
<Stepper
alternativeLabel
activeStep={activeStep}
connector={<QontoConnector />}
>
{steps.map((label) => (
<Step key={label}>
<StepLabel StepIconComponent={QontoStepIcon}>{label}</StepLabel>
</Step>
))}
</Stepper>
<div>
{activeStep === steps.length ? (
<div>
<Typography className={classes.instructions}>
All steps completed - you're finished
</Typography>
<Button onClick={handleReset} className={classes.button}>
Reset
</Button>
</div>
) : (
<div>
<Typography className={classes.instructions}>
{getStepContent(activeStep)}
</Typography>
<div>
<Button
disabled={activeStep === 0}
onClick={handleBack}
className={classes.button}
>
Back
</Button>
<Button
variant="contained"
color="primary"
onClick={handleNext}
className={classes.button}
>
{activeStep === steps.length - 1 ? "Finish" : "Next"}
</Button>
</div>
</div>
)}
</div>
</div>
);
}
In the above code example, I have used the @mui/material
component and make mui progress bar with steps in react js.
Check the output of the above code example.
Here, we are provided code sandbox links for the above program make mui progress bar with steps in react js. Then you can use whenever you want and do the changes as per your requirements.
All the best 👍