Menu

Components

Progress Bar

Shows how far along a task is, such as an upload or an install, or that work is going on when how much is done is not known.

Introduction

Progress Bar tells people how far along something is and how much is left: files uploading, a form's steps, an install. It is for a task that goes from a start to an end. For a measurement that is not going anywhere, such as storage used, use Meter.

  • Announced as a progressbar with its value, its range and the value as text, so a screen reader can say "64 percent" or "3 of 8 files".
  • The value is written as a percentage, as a number with a unit, or in your own words, over any range.
  • With isIndeterminate it is for when the work is going on but how much is done is not known. A short bar slides across the track, and no value is shown or announced.
  • The fill grows smoothly. For people who prefer reduced motion an indeterminate bar does not slide: it fills the track and breathes.

For a small mark next to text, or a button that is waiting, use Loader. Use a progress bar when the wait is long enough that people will want to know how much is left.

Installation

centerUI has no CLI or registry yet: components are copied into your project.

# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx

# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx

# npm
npm install react-aria-components tailwind-variants tailwind-merge clsx

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles

src/types/progress-bar-styles.tsShow code
import { tv } from "tailwind-variants";

// Styles for ProgressBar: a track, and a fill as wide as the progress. Without
// a known progress the fill is a short bar that slides across the track (see
// `animate-progress-indeterminate` in globals.css).
export const progressBarStyles = tv({
  slots: {
    root: "flex w-full flex-col gap-1.5",
    header: "flex items-baseline justify-between gap-3 text-sm",
    label: "font-medium",
    value: "text-muted-fg tabular-nums",
    track: "relative w-full overflow-hidden rounded-full bg-fg/10",
    fill: "h-full rounded-full transition-[width,background-color] duration-300 ease-out motion-reduce:transition-none",
    sliding:
      "absolute inset-y-0 left-0 w-2/5 animate-progress-indeterminate rounded-full",
  },
  variants: {
    size: {
      sm: { track: "h-1.5" },
      md: { track: "h-2" },
      lg: { track: "h-3" },
    },
    variant: {
      default: { fill: "bg-primary", sliding: "bg-primary" },
      success: { fill: "bg-success", sliding: "bg-success" },
      warning: { fill: "bg-warning", sliding: "bg-warning" },
      danger: { fill: "bg-danger", sliding: "bg-danger" },
    },
  },
  defaultVariants: { size: "md", variant: "default" },
});

2. Add the Progress Bar file

src/components/ui/progress-bar/progress-bar.tsxShow code
"use client";

import type { ReactNode } from "react";
import { ProgressBar as AriaProgressBar, Label } from "react-aria-components";
import { cn } from "@/lib/cn";
import { progressBarStyles } from "@/types/progress-bar-styles";

export interface ProgressBarProps {
  /** How far along it is. Not needed when the bar is indeterminate. */
  value?: number;
  /** The value at the start. Defaults to 0. */
  minValue?: number;
  /** The value at the end. Defaults to 100. */
  maxValue?: number;
  /**
   * The work is going on but how much is done is not known, for example while
   * waiting for a server. The bar slides back and forth, and there is no value.
   */
  isIndeterminate?: boolean;
  /** What is in progress, shown above the bar. */
  label?: ReactNode;
  /** Show the value, at the right of the label. Defaults to `true`. */
  showValue?: boolean;
  /**
   * How the value is written. The default is a percentage. Use
   * `{ style: "unit", unit: "megabyte" }` and the like for a real unit.
   */
  formatOptions?: Intl.NumberFormatOptions;
  /** Replaces the written value with your own text, such as "3 of 8 files". It is also what a screen reader hears. */
  valueLabel?: string;
  /** The color of the fill. Defaults to the neutral one. */
  variant?: "default" | "success" | "warning" | "danger";
  size?: "sm" | "md" | "lg";
  /** Names the bar when it has no `label`. */
  "aria-label"?: string;
  className?: string;
}

/**
 * Shows how far along a task is: an upload, an install, a step of a form. It
 * is announced as a progress bar with its value, so it is for something that
 * advances toward an end. For a measurement that is not going anywhere, such
 * as storage used, use `Meter`.
 *
 * When it does not know how far along it is, it is indeterminate: a short bar
 * slides across the track, and it says only that work is going on.
 *
 * @example
 * <ProgressBar label="Uploading" value={64} />
 * <ProgressBar label="Connecting" isIndeterminate />
 */
export function ProgressBar({
  label,
  showValue = true,
  variant,
  size,
  className,
  ...props
}: ProgressBarProps) {
  return (
    <AriaProgressBar
      {...props}
      className={cn(progressBarStyles().root(), className)}
    >
      {({ percentage, valueText, isIndeterminate }) => {
        const styles = progressBarStyles({ size, variant });

        return (
          <>
            {label || (showValue && !isIndeterminate) ? (
              <div className={styles.header()}>
                {label ? (
                  <Label className={styles.label()}>{label}</Label>
                ) : (
                  <span />
                )}
                {showValue && !isIndeterminate ? (
                  <span className={styles.value()}>{valueText}</span>
                ) : null}
              </div>
            ) : null}
            <div
              data-indeterminate={isIndeterminate ? "" : undefined}
              className={styles.track()}
            >
              {isIndeterminate ? (
                <div className={styles.sliding()} />
              ) : (
                <div
                  className={styles.fill()}
                  style={{ width: `${percentage}%` }}
                />
              )}
            </div>
          </>
        );
      }}
    </AriaProgressBar>
  );
}

3. Add the animation

In src/app/globals.css copy the --animate-progress-indeterminate line of the theme, the progress-indeterminate keyframes, and the [data-indeterminate] rule inside the reduced-motion query. The reduced-motion rule uses the loader-soft keyframes from Loader. Without the animation an indeterminate bar does not move.

4. Use it

import { ProgressBar } from "@/components/ui/progress-bar/progress-bar";

Anatomy

A label and a value on top, and a track with a fill under them.

<ProgressBar label="Uploading" value={64} />
<ProgressBar label="Connecting" isIndeterminate />
PartRendered asNotes
ProgressBardiv with role progressbarReports the value, the range and the written value to assistive technology. Without a known value it reports none.
LabellabelWhat is in progress, above the bar.
ValuespanThe value as text, at the right of the label. Hidden while indeterminate, or with showValue={false}.
TrackdivThe whole way.
FilldivAs wide as the progress. When indeterminate it is a short bar that slides across the track.

Examples

Usage

Uploading64%

Value format

A percentage by default. minValue and maxValue set the ends, formatOptions writes the number with a unit, and valueLabel replaces it with your own text, which a screen reader hears too.

Percentage64%
With a unit128 MB
Your own text3 of 8 files

Indeterminate

For work whose progress is not known, such as waiting for a server. A short bar slides across the track. No value is shown, and none is announced.

Connecting to the server
Danger

Variants

The color of the fill: neutral, or the theme's success, warning and danger. Use success when it is done, and danger when it failed.

Default60%
Success60%
Warning60%
Danger60%

Sizes

Small60%
Medium60%
Large60%

Without a label

Every bar needs a name. Without a visible label, give it an aria-label. showValue={false} hides the written value too.

Live progress

A simulated upload: one file every 0.7 seconds. The fill grows, the text follows, and the bar turns green when it is done.

Uploading files0 of 8 files

Component API

Progress Bar takes the props below.

PropTypeDefault
valuenumber0
minValuenumber0
maxValuenumber100
isIndeterminatebooleanfalse
labelReactNode-
showValuebooleantrue
formatOptionsIntl.NumberFormatOptionsa percentage
valueLabelstring-
variant"default" | "success" | "warning" | "danger""default"
size"sm" | "md" | "lg""md"
aria-labelstring- (when there is no label)
classNamestring-