Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Progress for Astro

Astro progress tracks for a known value or an unknown completion time.

Files exported65%

Installation

bunx --bun shadcn@latest add @gnostic/progress
npx shadcn@latest add @gnostic/progress
pnpm dlx shadcn@latest add @gnostic/progress

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/progress/progress.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import Track from "./progress-track.astro"
import Indicator from "./progress-indicator.astro"
type Props = HTMLAttributes<"div"> & {
  value?: number | null
  min?: number
  max?: number
}
const {
  class: className,
  value = null,
  min = 0,
  max = 100,
  style,
  ...props
} = Astro.props
if (
  !Number.isFinite(min) ||
  !Number.isFinite(max) ||
  max <= min ||
  (value !== null && !Number.isFinite(value))
)
  throw new RangeError(
    "Progress needs finite bounds with max greater than min, and a finite value.",
  )
const current = value === null ? null : Math.min(max, Math.max(min, value))
const percent = current === null ? 0 : ((current - min) / (max - min)) * 100
const mergedStyle =
  typeof style === "string"
    ? `${style};--progress-percentage:${percent}%`
    : { ...style, "--progress-percentage": percent + "%" }
---

<div
  data-slot="progress"
  data-indeterminate={current === null ? "" : undefined}
  role="progressbar"
  aria-valuemin={min}
  aria-valuemax={max}
  aria-valuenow={current ?? undefined}
  style={mergedStyle}
  class={cn("flex w-full flex-col gap-2", className)}
  {...props}
>
  <slot>
    <Track>
      <Indicator />
    </Track>
  </slot>
</div>
src/components/ui/progress/progress-value.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"span">
const { class: className, ...props } = Astro.props
---

<span
  data-slot="progress-value"
  class={cn("text-sm tabular-nums", className)}
  {...props}
>
  <slot />
</span>
src/components/ui/progress/progress-indicator.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---

<div
  data-slot="progress-indicator"
  class={cn(
    "h-full w-(--progress-percentage) bg-primary transition-[width] duration-500 motion-reduce:transition-none in-data-indeterminate:w-1/3 in-data-indeterminate:animate-pulse motion-reduce:in-data-indeterminate:animate-none",
    className,
  )}
  {...props}
></div>
src/components/ui/progress/progress-track.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---

<div
  data-slot="progress-track"
  class={cn(
    "block h-1.5 w-full overflow-hidden rounded-full bg-input",
    className,
  )}
  {...props}
>
  <slot />
</div>
src/components/ui/progress/progress-label.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"span">
const { class: className, ...props } = Astro.props
---

<span
  data-slot="progress-label"
  class={cn("font-medium text-sm", className)}
  {...props}
>
  <slot />
</span>
src/components/ui/progress/index.ts
export { default as Progress } from './progress.astro'
export { default as ProgressLabel } from './progress-label.astro'
export { default as ProgressTrack } from './progress-track.astro'
export { default as ProgressIndicator } from './progress-indicator.astro'
export { default as ProgressValue } from './progress-value.astro'

Usage

import { Progress } from "@/components/ui/progress"

Use the Code tab above for the complete Astro composition.

API reference

Read the registry source for the exported parts and prop types.

When to use Progress

Pass a value between min and max for measured progress. Omit value or pass null for an indeterminate task. The root supplies a default track and indicator; use the exported parts when you need a custom label layout.

Keyboard and behavior

Give the root aria-label or connect it to visible text with aria-labelledby. Values are clamped to the range, and indeterminate progress omits aria-valuenow. The component renders the initial value; live updates must keep the ARIA value and --progress-percentage in step.

Theme and layout

The indicator uses the primary token and the track uses input color. A reduced-motion preference removes its animation. Never invent a percentage when the task cannot report progress.