Progress
A pill shaded in as the work gets done; indeterminate slides a patch of shading along.
Uploading
Hatched
Done
Working on it
import { Progress, ProgressLabel, ProgressValue } from "@/components/ui/progress";
export default function ProgressDemo() {
return (
<div className="flex w-full max-w-md flex-col gap-8">
<Progress value={30} seed="pr-30">
<ProgressLabel>Uploading</ProgressLabel>
<ProgressValue />
</Progress>
<Progress value={66} fill="hatch" seed="pr-66">
<ProgressLabel>Hatched</ProgressLabel>
<ProgressValue />
</Progress>
<Progress value={100} seed="pr-100">
<ProgressLabel>Done</ProgressLabel>
<ProgressValue />
</Progress>
<Progress value={null} seed="pr-indeterminate">
<ProgressLabel>Working on it</ProgressLabel>
</Progress>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ progress"use client";
import { createContext, useContext, useId, useMemo, type CSSProperties } from "react";
import { Progress as ProgressPrimitive } from "@base-ui/react/progress";
import { penStyle, useInkFrame, useInkSeed, usePen, type InkFill, type Pen } from "@/hooks/use-ink-box";
import { inkClassName, InkSvg, Stroke } from "@/lib/ink";
import { hatchStrokes, penBoxStrokes, roundedRectPath, scribbleFill, shadeFill } from "@/lib/ink-sketch";
type TrackInk = { w: number; h: number; r: number; s: number; pen: Pen };
const TrackContext = createContext<TrackInk | null>(null);
function Progress({ className, children, value, size, seed, roughness, passes, radius, fill, draw, weight, speed, ...props }: ProgressPrimitive.Root.Props & TrackPen) {
return (
<ProgressPrimitive.Root value={value} data-slot="progress" className={inkClassName("group/progress flex flex-wrap gap-x-3 gap-y-2", className)} {...props}>
{children}
<ProgressTrack size={size} seed={seed} roughness={roughness} passes={passes} radius={radius} fill={fill} draw={draw} weight={weight} speed={speed}>
<ProgressIndicator />
</ProgressTrack>
</ProgressPrimitive.Root>
);
}
type TrackPen = Pick<Pen, "roughness" | "passes" | "radius" | "fill" | "draw" | "weight" | "speed"> & { seed?: string | number; size?: keyof typeof heights };
// Track heights, px: a hairline for a page, a fat bar to fill in.
const heights = { sm: 8, default: 12, lg: 18 } as const;
/** A pill pulled once along the full length; the indicator inside shades it in. */
function ProgressTrack({ className, children, size = "default", seed, roughness, passes, radius, fill, draw, weight, speed, ...props }: ProgressPrimitive.Track.Props & TrackPen) {
const pen = usePen({ roughness, passes, radius, fill, draw, weight, speed });
const s = useInkSeed(seed);
const { ref, w, h, frame } = useInkFrame([320, heights[size]], { pad: 5 });
const r = pen.radius === undefined || pen.radius === "full" ? h / 2 : Math.min(pen.radius, h / 2);
const outline = useMemo(
() => penBoxStrokes(s, w, h, { roughness: pen.roughness ?? 1, passes: pen.passes ?? 1, corners: "joined", radius: r }),
[s, w, h, pen.roughness, pen.passes, r],
);
const mode = pen.draw ?? "auto";
const track = useMemo(() => ({ w, h, r, s, pen }), [w, h, r, s, pen]);
return (
<ProgressPrimitive.Track data-slot="progress-track" className={inkClassName("relative flex w-full items-center", className)} style={{ height: heights[size] }} {...props}>
<InkSvg ref={ref} {...frame} pending={mode === "auto"} className="text-ink-line" style={{ ...frame.style, ...penStyle(pen) }}>
{outline.map((d, i) => (
<Stroke key={i} d={d} draw={mode} delay={i * 240} duration={520} width={[1.3, 1, 0.9][i]} opacity={[1, 0.7, 0.5][i]} />
))}
</InkSvg>
{/* The indicator moves inside a clip shaped like the track, so the
shading (and the indeterminate patch sliding in and out) never
spills past it. The track itself can't clip: its outline overshoots. */}
<div data-slot="progress-clip" className="absolute inset-0 overflow-hidden" style={{ borderRadius: r }}>
<TrackContext value={track}>{children}</TrackContext>
</div>
</ProgressPrimitive.Track>
);
}
/**
* The done part, shaded in. The shading is drawn once for the whole track
* and the indicator only uncovers it, so the strokes hold still as the value
* grows. Indeterminate progress slides a patch of shading along.
*/
function ProgressIndicator({ className, style, ...props }: ProgressPrimitive.Indicator.Props) {
const track = useContext(TrackContext);
// Rounded like the track, so the shading ends in a pill, not a cut.
const round = track ? { borderRadius: track.r } : undefined;
return (
<ProgressPrimitive.Indicator
data-slot="progress-indicator"
style={typeof style === "function" ? (state) => ({ ...round, ...style(state) }) : { ...round, ...style }}
className={inkClassName(
// .ink-progress (base.css) slides it along when indeterminate.
"ink-progress absolute inset-y-0 left-0 overflow-hidden transition-[width] duration-(--dur-state) ease-out motion-reduce:transition-none",
className,
)}
{...props}
>
{track && <Shading {...track} />}
</ProgressPrimitive.Indicator>
);
}
function Shading({ w, h, r, s, pen }: TrackInk) {
const id = useId().replace(/[^\w-]/g, "");
const fill: InkFill = pen.fill ?? "shade";
const d = useMemo(() => {
if (fill === "flat") return "";
if (fill === "hatch") return hatchStrokes(s + 30, w, h, { gap: 2.4, angle: -50, jitter: 0.3 }).join("");
if (fill === "scribble") return scribbleFill(s + 30, w, h, { gap: 2.2 });
return shadeFill(s + 30, w, h, { gap: 1.8, angle: -10, overrun: 2 });
}, [fill, s, w, h]);
return (
<InkSvg box={[0, 0, w, h]} className="top-0 left-0 text-ink" style={{ width: w, height: h, ...penStyle(pen) } as CSSProperties}>
<defs>
<clipPath id={`${id}-clip`}>
<path d={roundedRectPath(w, h, r)} />
</clipPath>
</defs>
<g clipPath={`url(#${id}-clip)`}>{d ? <Stroke d={d} width={1.2} /> : <path d={roundedRectPath(w, h, r)} className="ink-fill" style={{ opacity: 1 }} />}</g>
</InkSvg>
);
}
function ProgressLabel({ className, ...props }: ProgressPrimitive.Label.Props) {
return <ProgressPrimitive.Label className={inkClassName("text-base", className)} data-slot="progress-label" {...props} />;
}
function ProgressValue({ className, ...props }: ProgressPrimitive.Value.Props) {
return <ProgressPrimitive.Value className={inkClassName("ml-auto text-sm text-ink-3 tabular-nums", className)} data-slot="progress-value" {...props} />;
}
export { Progress, ProgressTrack, ProgressIndicator, ProgressLabel, ProgressValue };Usage
import { Progress, ProgressLabel, ProgressValue } from "@/components/ui/progress"
<Progress value={40}>
<ProgressLabel>Uploading</ProgressLabel>
<ProgressValue />
</Progress>Examples
Controlled
Drive the value from state and watch the shading follow.
Chapter progress
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Progress, ProgressLabel, ProgressValue } from "@/components/ui/progress";
export default function ProgressControlled() {
const [value, setValue] = useState(25);
return (
<div className="flex w-full max-w-md flex-col gap-6">
<Progress value={value} seed="pc-progress">
<ProgressLabel>Chapter progress</ProgressLabel>
<ProgressValue />
</Progress>
<div className="flex gap-3">
<Button variant="outline" size="sm" onClick={() => setValue((v) => Math.max(0, v - 15))} seed="pc-less">
Less
</Button>
<Button size="sm" onClick={() => setValue((v) => Math.min(100, v + 15))} seed="pc-more">
More
</Button>
<Button variant="ghost" size="sm" onClick={() => setValue(0)} seed="pc-reset">
Reset
</Button>
</div>
</div>
);
}Sizes
A hairline, the default, and a fat bar to fill in.
sm
default
lg
import { Progress, ProgressLabel, ProgressValue } from "@/components/ui/progress";
const sizes = [
["sm", 30],
["default", 55],
["lg", 80],
] as const;
export default function ProgressSizes() {
return (
<div className="flex w-full max-w-md flex-col gap-8">
{sizes.map(([size, value]) => (
<Progress key={size} size={size} value={value} seed={`psz-${size}`}>
<ProgressLabel>{size}</ProgressLabel>
<ProgressValue />
</Progress>
))}
</div>
);
}Upload
Waits indeterminate, then fills in uneven bursts, and is done.
notebook.pdf
"use client";
import { useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import { Progress, ProgressLabel, ProgressValue } from "@/components/ui/progress";
export default function ProgressUpload() {
const [value, setValue] = useState<number | null>(null);
const [run, setRun] = useState(0);
useEffect(() => {
if (!run) return;
// Waits (indeterminate), then fills in uneven bursts, the way a real upload does.
let v = 0;
const timers: ReturnType<typeof setTimeout>[] = [];
const step = () => {
v = Math.min(100, v + 6 + Math.random() * 16);
setValue(v);
if (v < 100) timers.push(setTimeout(step, 180 + Math.random() * 260));
};
timers.push(setTimeout(step, 900));
return () => timers.forEach(clearTimeout);
}, [run]);
const done = value === 100;
return (
<div className="flex w-full max-w-md flex-col gap-5">
<Progress value={run ? value : 0} size="lg" seed="pu-bar">
<ProgressLabel>{!run ? "notebook.pdf" : value === null ? "Waiting for the server…" : done ? "Uploaded" : "Uploading notebook.pdf"}</ProgressLabel>
{run > 0 && value !== null && <ProgressValue>{(formatted) => `${formatted ?? 0}%`}</ProgressValue>}
</Progress>
<Button variant={done ? "outline" : "default"} className="w-fit" seed="pu-go" onClick={() => {
setValue(null);
setRun((n) => n + 1);
}} disabled={run > 0 && !done}>
{done ? "Upload again" : "Upload"}
</Button>
</div>
);
}Steps
A step counter that moves the bar a quarter at a time.
Step 1 of 4: Pick a pen
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Progress, ProgressLabel, ProgressValue } from "@/components/ui/progress";
const steps = ["Pick a pen", "Pick a paper", "Write something", "Done"];
export default function ProgressSteps() {
const [step, setStep] = useState(1);
return (
<div className="flex w-full max-w-md flex-col gap-5">
<Progress value={(step / steps.length) * 100} size="sm" seed="ps-steps">
<ProgressLabel>
Step {step} of {steps.length}: {steps[step - 1]}
</ProgressLabel>
<ProgressValue />
</Progress>
<div className="flex gap-3">
<Button variant="outline" size="sm" seed="ps-back" disabled={step === 1} onClick={() => setStep((s) => s - 1)}>
Back
</Button>
<Button size="sm" seed="ps-next" disabled={step === steps.length} onClick={() => setStep((s) => s + 1)}>
Next
</Button>
</div>
</div>
);
}API reference
- value
number | nullnull for indeterminate: a patch of shading slides along.
- sizedefault
"default" "sm" | "default" | "lg"Track height: 8, 12 or 18px.
- seed
string | numberPins the drawing.
Every other prop goes to Base UI's Progress.
Pen settings
The drawing also takes draw, roughness, passes, radius, fill, weight and speed, as props or from the nearest InkProvider. What each one does.