Education
Software Development Executive - I
Last updated on Jun 27, 2024
Last updated on Jun 27, 2024
In web development, visual indicators like the progress bar play a crucial role in enhancing user experience by providing feedback on ongoing tasks. For React developers, creating a progress bar react component that is both functional and aesthetically pleasing is an essential skill.
In this guide, we will walk through the process of creating a custom progress bar component in React, ensuring it is adaptable and accessible.
Before diving into code, it's important to understand what a progress bar is and how it functions within a React app. A progress bar visually represents the completion status of a task, filling up as the task progresses. In React, this is typically achieved by manipulating the width of a child div within a parent div, based on the completed percentage number.
To begin, ensure your React environment is set up. If you're starting a new project, you can create a new React app using the command:
1npx create-react-app progress-bar-react
Navigate to your project directory, and you're ready to start implementing your progress bar component.
Creating a custom progress bar component allows for greater flexibility and customization. Let's start by defining the structure of our component.
The progress bar consists of two main elements: a parent div that defines the outer boundary, and a child div that represents the completed part. Here's a basic structure:
1function ProgressBar({ value, max }) { 2 return ( 3 <div className="progress-bar"> 4 <div className="progress-bar-completed" style={{ width: `${(value / max) * 100}%` }}> 5 </div> 6 </div> 7 ); 8}
Styling is crucial for making the progress bar visually appealing. Here's some basic CSS to get started:
1.progress-bar { 2 width: 100%; 3 background-color: #eee; 4} 5 6.progress-bar-completed { 7 height: 20px; 8 background-color: pink; 9 transition: width 0.5s ease-in-out; 10}
The inline style for the child div dynamically sets the width based on the value prop, which represents the completed percentage number.
The logic behind the progress bar involves updating the value prop as the task progresses.
To dynamically update the progress, manage the state in your function app using the useState hook:
1import React, { useState } from 'react'; 2import ProgressBar from './ProgressBar'; 3 4function App() { 5 const [progress, setProgress] = useState(0); 6 7 // Simulate progress 8 const simulateProgress = () => { 9 setProgress(progress + 10); 10 }; 11 12 return ( 13 <div> 14 <ProgressBar value={progress} max={100} /> 15 <button onClick={simulateProgress}>Increase Progress</button> 16 </div> 17 ); 18} 19 20export default App;
Accessibility is an important aspect of web development. Use aria-labelledby to ensure that the progress bar is accessible:
1<div className="progress-bar" aria-labelledby="progress-bar-label"> 2 <span id="progress-bar-label" className="visually-hidden">Progress: {value}%</span> 3 <div className="progress-bar-completed" style={{ width: `${(value / max) * 100}%` }}> 4 </div> 5</div>
Animations can make the progress bar more engaging for the user.
The CSS provided earlier includes a transition property, which animates the width of the completed part smoothly.
You can control the animation speed and timing by adjusting the transition property in the CSS. For example, to make the animation slower, you could change the duration to 1 second:
1.progress-bar-completed { 2 transition: width 1s ease-in-out; 3}
Once your progress bar component is created and styled, you can integrate it into your React app.
Export your ProgressBar component using export default and import it where needed using import ProgressBar:
1// In ProgressBar.js 2export default ProgressBar; 3 4// In App.js 5import ProgressBar from './ProgressBar';
Pass props for customization to the ProgressBar component to control the label text, the completed value, and the maximum value:
1<ProgressBar label="Loading..." value={progress} max={100} />
In the ProgressBar component, you can use these props to display the label text and calculate the width of the completed part:
1function ProgressBar({ label, value, max }) { 2 return ( 3 <div className="progress-bar" aria-labelledby="progress-bar-label"> 4 <span id="progress-bar-label">{label}: {value}%</span> 5 <div className="progress-bar-completed" style={{ width: `${(value / max) * 100}%` }}> 6 </div> 7 </div> 8 ); 9}
To further enhance your progress bar, you can add advanced features and allow for deeper customization.
In addition to displaying the label text, you might want to include dynamic width calculation for responsiveness. Here's an example of how you could adjust the width based on the parent div's width:
1function ProgressBar({ label, value, max }) { 2 const [parentWidth, setParentWidth] = useState(0); 3 const progressBarRef = useRef(null); 4 5 useEffect(() => { 6 if (progressBarRef.current) { 7 setParentWidth(progressBarRef.current.offsetWidth); 8 } 9 }, []); 10 11 const completedWidth = (value / max) * parentWidth; 12 13 return ( 14 <div ref={progressBarRef} className="progress-bar" aria-labelledby="progress-bar-label"> 15 <span id="progress-bar-label">{label}: {value}%</span> 16 <div className="progress-bar-completed" style={{ width: `${completedWidth}px` }}> 17 </div> 18 </div> 19 ); 20}
To make your component more robust, you can define default props and prop types. This helps in maintaining consistency and provides a clear understanding of the component's expected usage:
1import PropTypes from 'prop-types'; 2 3ProgressBar.defaultProps = { 4 label: 'Progress', 5 value: 0, 6 max: 100, 7}; 8 9ProgressBar.propTypes = { 10 label: PropTypes.string, 11 value: PropTypes.number.isRequired, 12 max: PropTypes.number, 13};
Test your progress bar across different browsers to ensure compatibility and fine-tune performance. You may need to use vendor prefixes for CSS properties or polyfills for JavaScript features not supported in all browsers.
In conclusion, the progress bar react component offers a versatile and customizable solution for visually representing the completion status of tasks or processes within your React applications. By leveraging the flexibility of props and styling options, you can tailor the progress bar's appearance and behavior to seamlessly integrate with your project's design and functionality. Consider using the progress bar to enhance user experience by providing clear and informative feedback on ongoing operations.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.