# Progress

A pill shaded in as the work gets done; indeterminate slides a patch of shading along.

Docs: https://ballpoint.st9wd.com/docs/progress

```bash
npx shadcn@latest add @ballpoint/progress
```

```tsx
import { Progress, ProgressLabel, ProgressValue } from "@/components/ui/progress"

<Progress value={40}>
  <ProgressLabel>Uploading</ProgressLabel>
  <ProgressValue />
</Progress>
```

Props:

- `value`: `number | null`. null for indeterminate: a patch of shading slides along.
- `size`: `"sm" | "default" | "lg"` (default `"default"`). Track height: 8, 12 or 18px.
- `seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `roughness`, `passes`, `radius`, `fill`, `weight`, `speed` (see Pen settings).

Every other prop goes to Base UI's Progress: https://base-ui.com/react/components/progress

## Example

```tsx
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>
  );
}
```

## Controlled

Drive the value from state and watch the shading follow.

```tsx
"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.

```tsx
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.

```tsx
"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.

```tsx
"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>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `radius`: `number | "full"` (default `0`). Corner radius in px; "full" draws a pill.
- `fill`: `"shade" | "hatch" | "scribble" | "flat"`. How an area is coloured in. Solid parts default to shade, light ones to hatch.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
