progress.tsx
Overview
The progress.tsx file defines a React functional component named Progress that provides a customizable and accessible progress bar UI element. This component is built on top of the @radix-ui/react-progress primitives, leveraging their accessibility and behavior while allowing easy styling and integration within React applications.
The main purpose of this file is to encapsulate the Radix UI Progress primitive with additional styling and convenience, exposing a simple interface for rendering a horizontal progress bar that visually indicates completion percentage.
Component: Progress
Description
Progress is a React forward-ref component that wraps the ProgressPrimitive.Root and ProgressPrimitive.Indicator components from Radix UI's progress primitives. It displays a horizontal progress bar with a background track and an indicator that moves in response to the value prop, representing progress percentage.
The component supports external ref forwarding and accepts all props that the underlying Radix ProgressPrimitive.Root supports. It also allows additional CSS class names via the className prop.
Props
Progress accepts all props supported by ProgressPrimitive.Root:
Prop Name | Type | Description |
|---|---|---|
|
| The progress value, typically between 0 and 100, indicating the completion percentage. |
|
| Additional CSS classes to customize the root element's styling. |
...props | any | Other props are forwarded to |
Ref
The component supports forwarding a React ref to the underlying ProgressPrimitive.Root element.
Functionality & Implementation Details
The root element (
ProgressPrimitive.Root) is styled with a fixed height (h-4), full width (w-full), rounded corners, and a background color representing the inactive track (bg-bg-accent).The progress indicator (
ProgressPrimitive.Indicator) fills the height and transitions horizontally based on thevalueprop.The indicator's position is controlled via a CSS transform: translateX(...) property, which shifts it left by an amount proportional to
100 - value, effectively revealing the progress visually.The component uses the utility function
cn(imported from@/lib/utils) to concatenate and conditionally apply CSS class names.The
displayNameof the component is set to match the Radix primitive's display name for easier debugging.
Usage Example
import { Progress } from './progress';
function Example() {
const [progress, setProgress] = React.useState(45);
return (
<div>
<Progress value={progress} className="my-4" />
<button onClick={() => setProgress((prev) => Math.min(prev + 10, 100))}>
Increase Progress
</button>
</div>
);
}
Interaction with Other Parts of the System
Dependency on Radix UI: This component wraps the Radix UI progress primitives (
@radix-ui/react-progress), relying on them for core accessibility and structural behavior.Utility function
cn: Thecnfunction is imported from@/lib/utilsand is used to merge and conditionally apply class names. This utility likely handles string concatenation and filtering of falsy values for class names.Styling: The component uses Tailwind CSS classes for styling. It assumes that the Tailwind CSS configuration includes custom color tokens like
bg-bg-accentandbg-accent-primary.Forwarding refs: The forwarded ref allows parent components or libraries to directly access the root DOM element of the progress bar, enabling advanced interactions or integrations.
Mermaid Component Diagram
componentDiagram
component Progress {
+value: number (optional)
+className: string (optional)
+ref: React.Ref
+...props
}
component ProgressPrimitive.Root {
+ref
+className
+...props
}
component ProgressPrimitive.Indicator {
+style: { transform: string }
+className: string
}
Progress -- uses --> ProgressPrimitive.Root
ProgressPrimitive.Root -- contains --> ProgressPrimitive.Indicator
Summary
progress.tsx provides a clean, reusable React component for rendering accessible progress bars with a visually appealing indicator. By leveraging Radix UI's primitives and Tailwind CSS utility classes, it abstracts complexity and allows developers to quickly integrate progress bars with consistent styling and behavior throughout the application. The component's design supports easy extension through props and ref forwarding, making it flexible for various use cases involving progress indication.