Building a Reusable Progress Bar Component in React

# Building a Reusable Progress Bar Component in React Hey fellow devs! 👋 Today, I want to share my experience building a reusable progress bar component in React. We'll create something that's not just functional, but also looks great and can be easily customized for different use cases. ## The Problem Recently, I was working on a file upload feature for our web app, and I needed a progress bar that could: - Show upload progress - Be reusable across different parts of the app - Look consistent with our design system - Be easily customizable Instead of reaching for a third-party library, I decided to build one from scratch. Here's how I did it! ## The Solution First, let's look at how we want to use our component: ```jsx ``` Now, here's the complete implementation: ```jsx import React from 'react'; import styled, { keyframes } from 'styled-components'; const shimmer = keyframes` 0% { background-position: -1000px 0; } 100% { background-position: 1000px 0; } `; const ProgressBarContainer = styled.div` width: 100%; background-color: #f0f0f0; border-radius: ${props => props.height}px; height: ${props => props.height}px; overflow: hidden; `; const Progress = styled.div` width: ${props => props.progress}%; height: 100%; background-color: ${props => props.color}; border-radius: inherit; transition: width 0.3s ease-in-out; ${props => props.animated && ` background-image: linear-gradient( 45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent ); background-size: 40px 40px; animation: ${shimmer} 2s linear infinite; `} `; const ProgressBar = ({ progress = 0, color = '#4CAF50', height = 8, animated = false, ...props }) => { // Ensure progress stays between 0 and 100 const normalizedProgress = Math.min(Math.max(progress, 0), 100); return ( ); }; export default ProgressBar; ``` ## Breaking Down the Implementation ### 1. Styled Components I used styled-components for styling because it makes it super easy to create dynamic styles based on props. The `ProgressBarContainer` wraps our entire component, while `Progress` represents the actual progress indicator. ### 2. Animation The shimmer animation is totally optional but adds a nice touch. It creates that moving stripe effect you often see in progress bars. We use CSS animations and a linear gradient to achieve this effect. ### 3. Props Validation We validate the progress value to ensure it stays between 0 and 100. This prevents any weird visual bugs that might occur with invalid values. ## Using the Component Here are some examples of how flexible this component is: ```jsx // Basic usage // Custom styling // With animation ``` ## Pro Tips 🚀 1. **Color Inheritance**: You can make the progress bar inherit its color from its parent: ```jsx ``` 2. **Accessibility**: Add ARIA attributes for better screen reader support: ```jsx ``` ## Wrapping Up This progress bar component is now being used across our application for various purposes - file uploads, form completion indicators, and even game loading screens. The best part? It's super lightweight and we have complete control over its appearance and behavior. Remember, while building reusable components, it's important to: - Keep the API simple and intuitive - Provide sensible defaults - Allow for customization - Consider accessibility - Document your code well Happy coding! 🚀 #react #javascript #webdev #ui