Progress

How far along a task is.

Pass value as a number, or null while the amount of work is unknown: the bar then pulses instead of filling.

Preview
basic.tsx
import { Progress, ProgressLabel, ProgressTrack, ProgressValue } from "~/components/ui/progress"

export default function Example() {
  return (
    <div className="grid w-full max-w-sm gap-xl">
      <Progress value={62}>
        <ProgressLabel>Uploading photos</ProgressLabel>
        <ProgressValue />
        <ProgressTrack />
      </Progress>
      <Progress value={null}>
        <ProgressLabel>Measuring pages</ProgressLabel>
        <ProgressTrack />
      </Progress>
    </div>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesTextprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/progress.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/progress.tsx
import {
  Progress as ProgressPrimitive,
  ProgressIndicator as ProgressIndicatorPrimitive,
  ProgressLabel as ProgressLabelPrimitive,
  ProgressTrack as ProgressTrackPrimitive,
  ProgressValue as ProgressValuePrimitive,
} from "@ui-sdk/react/progress"
import type * as React from "react"

import { textVariants } from "~/components/ui/text"
import { cnState } from "~/lib/utils"

/**
 * A task's progress. Holds an optional `ProgressLabel` and `ProgressValue`, and
 * a `ProgressTrack` with its indicator built in. Leave out `value` while the
 * amount of work is unknown.
 */
function Progress({ className, ...props }: React.ComponentProps<typeof ProgressPrimitive>) {
  return (
    <ProgressPrimitive
      className={cnState("flex w-full flex-wrap items-center gap-x-md gap-y-sm", className)}
      {...props}
    />
  )
}

/** Names the task. */
function ProgressLabel({
  className,
  ...props
}: React.ComponentProps<typeof ProgressLabelPrimitive>) {
  return (
    <ProgressLabelPrimitive
      className={cnState(textVariants({ size: "sm", weight: "medium" }), className)}
      {...props}
    />
  )
}

/** The formatted value, like 40%. */
function ProgressValue({
  className,
  ...props
}: React.ComponentProps<typeof ProgressValuePrimitive>) {
  return (
    <ProgressValuePrimitive
      className={cnState(
        [textVariants({ size: "sm", tone: "muted", tabular: true }), "ml-auto"],
        className,
      )}
      {...props}
    />
  )
}

/** The bar, with its indicator built in unless you pass your own. */
function ProgressTrack({
  className,
  children,
  ...props
}: React.ComponentProps<typeof ProgressTrackPrimitive>) {
  return (
    <ProgressTrackPrimitive
      className={cnState(
        "relative flex h-1.5 w-full basis-full items-center overflow-hidden rounded-lg bg-muted",
        className,
      )}
      {...props}
    >
      {children ?? <ProgressIndicator />}
    </ProgressTrackPrimitive>
  )
}

/** The filled part of the bar. Pulses while the progress is unknown. */
function ProgressIndicator({
  className,
  ...props
}: React.ComponentProps<typeof ProgressIndicatorPrimitive>) {
  return (
    <ProgressIndicatorPrimitive
      className={cnState(
        "h-full bg-primary transition-[width] duration-300 data-indeterminate:w-1/3 data-indeterminate:animate-pulse motion-reduce:transition-none motion-reduce:data-indeterminate:animate-none",
        className,
      )}
      {...props}
    />
  )
}

export { Progress, ProgressIndicator, ProgressLabel, ProgressTrack, ProgressValue }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Progress

A task's progress. Holds an optional ProgressLabel and ProgressValue, and a ProgressTrack with its indicator built in. Leave out value while the amount of work is unknown.

className

Type string | ((state: NoInfer<ProgressState>) => string)

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

format

How the value reads, for screen readers and the Value part: Intl.NumberFormat options, or a function of the value. A percent style shows where the value sits between min and max; any other style shows the value itself, like { style: "unit", unit: "gigabyte" }.

Type NumberFormat · Default { style: "percent" }

getValueLabel

The text screen readers announce for the value, when the formatted value says too little, like "3 of 12 files". It receives the value, null while a progress bar is indeterminate, and details: the formatted value, its percent, min and max.

Type (value: number | null, details: GaugeLabelDetails<number | null>) => string

locale

The locale the value is formatted in. Defaults to the page's lang, or the browser's language without one, once the page is interactive. Server markup uses English until then, so it never disagrees with the client.

Type string

max

The highest value.

Type number · Default 100

min

The lowest value.

Type number · Default 0

render

Type RenderProp<NoInfer<ProgressState>>

style

Type CSSProperties | ((state: NoInfer<ProgressState>) => CSSProperties)

value

How far the task has come, between min and max. Null means it's under way but how far is unknown, and the bar is indeterminate.

Type number | null · Default null

ProgressIndicator

The filled part of the bar. Pulses while the progress is unknown.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<ProgressState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<ProgressState>) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<ProgressState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<ProgressState>) => CSSProperties)

ProgressLabel

Names the task.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<ProgressState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<ProgressState>) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<ProgressState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<ProgressState>) => CSSProperties)

ProgressTrack

The bar, with its indicator built in unless you pass your own.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<ProgressState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<ProgressState>) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<ProgressState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<ProgressState>) => CSSProperties)

ProgressValue

The formatted value, like 40%.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: ProgressValueState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: ProgressValueState) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<ProgressValueState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: ProgressValueState) => CSSProperties)