# Progress Ring
A radial progress arc, determinate or indeterminate.
Source page: https://dinachi.dev/docs/components/progress-ring

## Installation
```bash
npx @dinachi/cli@latest add progress-ring
```
Adds to your project:
- `components/ui/progress-ring/progress-ring.tsx`
- `components/ui/progress-ring/index.ts`
Installs:
- `motion` ^12.23.6

## Usage
```tsx
import { ProgressRing } from "@/components/ui/progress-ring"

export function Upload() {
  return <ProgressRing value={64} label="Upload" />
}
```

## Examples

### Default
Press the buttons quickly and watch the spring re-target.
```tsx
import { ProgressRing } from '@/components/ui/progress-ring';

export function Example() {
  return <ProgressRing value={64} label="Upload" />;
}
```

### Indeterminate
Omit value when there is nothing honest to report.
```tsx
// No value, so no number and no claim about how far along it is.
<ProgressRing label="Fetching" />
```

### Sizes
The stroke scales with the ring unless you pin it.
```tsx
<ProgressRing value={68} size={128} label="Large" />
<ProgressRing value={68} label="Default" />
<ProgressRing value={68} size={56} label="Compact" />
<ProgressRing value={68} size={56} thickness={3} showValue={false} label="Hairline" />
```

## Notes

### Motion tier
Needs [motion](https://motion.dev). `add --all` skips it; install with `add --motion` or by name.

### Value changes
The arc springs to each new value from wherever it is, so rapid updates merge into one sweep. The centre number follows the same spring, shared with [number ticker](/docs/components/number-ticker).

### Indeterminate
Omit `value` for an arc that lengthens and shortens while it rotates.

### When to use it
- One operation being waited on, such as an upload, import or quota, or a compact spot with no room for a bar.
- For progress per row in a list, use [Progress](/docs/components/progress). The arc animates `stroke-dashoffset`, which repaints.
- Avoid it under about 40px, where the number stops fitting.

### Agent
`ProgressRing` does not register with the [agent bridge](/docs/components/agent). The bridge reads it from the accessibility tree as `progress` with `value`, `min` and `max`, and no actions. While indeterminate it is a status and is not listed.

```ts
agent.snapshot()
// [{ component: "progress", label: "Upload", value: 64, min: 0, max: 100, actions: [] }]
```

## API reference

### ProgressRing
An SVG arc with the percentage in the middle.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` **agent** | `number` | - | 0 to 100. Omit it for the indeterminate arc. |
| `label` **agent** | `string` | `"Progress" or "Loading"` | The accessible name, also read by the agent. |
| `size` | `number` | `96` | Outer diameter, in pixels. |
| `thickness` | `number` | - | Stroke width in pixels. Scales with size by default. |
| `showValue` | `boolean` | `true` | Shows the percentage. Ignored when indeterminate. |

## Accessibility
- A determinate ring is a progressbar with `aria-valuenow`, `aria-valuemin`, `aria-valuemax` and `aria-valuetext`, such as "64 percent".
- An indeterminate ring is a status with no value.
- `label` is the accessible name; defaults to "Progress" or "Loading".
- The centre number is aria-hidden; the ring announces the value.
- Reduced motion jumps to the value and shows a static gap instead of the sweep.

## Source
`components/ui/progress-ring/progress-ring.tsx`
```tsx
"use client"

import * as React from "react"
import {
  motion,
  MotionConfigContext,
  useMotionValue,
  useReducedMotion,
  useSpring,
  useTransform,
} from "motion/react"
import { cn } from "@/lib/utils"

/** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
 *  app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
 *  reaches this component. */
function useReducedMotionConfig() {
  const config = React.useContext(MotionConfigContext)
  const prefersReduced = useReducedMotion()
  if (config.reducedMotion === "always") return true
  if (config.reducedMotion === "never") return false
  return prefersReduced ?? false
}

/** Apple's move-and-reposition spring, the same one the number ticker uses, so a ring and
 *  a count beside it travel on one clock. */
const SETTLE = { duration: 400, bounce: 0 } as const

const DEFAULT_SIZE = 96

export interface ProgressRingProps
  extends Omit<React.ComponentProps<"div">, "children"> {
  /** 0–100. Omit it for the indeterminate arc. */
  value?: number
  /** Accessible name. Defaults to "Progress", or "Loading" when indeterminate. */
  label?: string
  /** Outer diameter in px. */
  size?: number
  /** Stroke width in px. Scales with `size` unless given. */
  thickness?: number
  /** Show the percentage in the middle. Ignored when indeterminate — there is no number
   *  to show. */
  showValue?: boolean
}

/**
 * A radial progress arc.
 *
 * The value is driven through a spring rather than a tween, which matters more here than it
 * looks: progress arrives in jumps, and a tween restarted on every jump either stutters or
 * has to be cancelled and rebuilt. A spring re-targets from wherever the arc currently is
 * and carries its velocity across, so a fast run of updates reads as one accelerating sweep
 * instead of a series of separate moves.
 *
 * One honest caveat: the arc is `stroke-dashoffset`, which is not a compositor property. At
 * this size that is fine — one small element repainting a stroke. Forty of them on a page
 * would be a different conversation, and the alternative is a rotated half-disc mask that
 * animates on `transform` but cannot do a rounded cap cleanly.
 */
const ProgressRing = React.forwardRef<HTMLDivElement, ProgressRingProps>(
  // Two independent contracts, determinate-or-not and reduced-motion-or-not, and every
  // branch is one of the four combinations. Splitting the render in two would duplicate
  // the ring to separate the arc.
  // fallow-ignore-next-line complexity
  (
    {
      value,
      label,
      size = DEFAULT_SIZE,
      thickness,
      showValue = true,
      className,
      ...props
    },
    ref
  ) => {
    const reducedMotion = useReducedMotionConfig()
    const indeterminate = value === undefined

    // A ring twice the size with the same 8px stroke reads as a thinner ring, not a bigger
    // one. Proportional is the default; an explicit `thickness` wins.
    const stroke = thickness ?? Math.max(3, Math.round(size * 0.085))
    const radius = (size - stroke) / 2
    const circumference = 2 * Math.PI * radius

    const offsetFor = (v: number) =>
      circumference - (Math.min(Math.max(v, 0), 100) / 100) * circumference

    const target = useMotionValue(value ?? 0)
    React.useEffect(() => {
      if (value !== undefined) target.set(value)
    }, [value, target])

    const eased = useSpring(target, SETTLE)
    const offset = useTransform(eased, offsetFor)
    const readout = useTransform(eased, (v) => `${Math.round(v)}%`)

    // The arc starts at twelve o'clock either way. Rotating the whole SVG is a transform,
    // so the sweep costs nothing, unlike re-deriving the path.
    const spin =
      indeterminate && !reducedMotion
        ? {
            animate: { rotate: [-90, 270] },
            transition: {
              duration: 1.4,
              repeat: Infinity,
              ease: "linear" as const,
            },
          }
        : { animate: { rotate: -90 }, transition: { duration: 0 } }

    // An indeterminate ring has no value to report, so it is a live region rather than a
    // progressbar. A `progressbar` with no `aria-valuenow` is announced as 0%, which is a
    // worse answer than "loading".
    const rounded = Math.round(value ?? 0)
    const reported = indeterminate
      ? { role: "status" as const }
      : {
          role: "progressbar" as const,
          "aria-valuenow": rounded,
          "aria-valuemin": 0,
          "aria-valuemax": 100,
          "aria-valuetext": `${rounded} percent`,
        }

    return (
      <div
        ref={ref}
        data-slot="progress-ring"
        className={cn("relative", className)}
        style={{ width: size, height: size }}
        {...props}
      >
        <motion.svg
          width={size}
          height={size}
          {...spin}
          {...reported}
          aria-label={label ?? (indeterminate ? "Loading" : "Progress")}
        >
          <circle
            cx={size / 2}
            cy={size / 2}
            r={radius}
            fill="none"
            strokeWidth={stroke}
            className="stroke-muted"
          />

          {indeterminate ? (
            <IndeterminateArc
              cx={size / 2}
              cy={size / 2}
              r={radius}
              stroke={stroke}
              circumference={circumference}
              reducedMotion={reducedMotion}
            />
          ) : (
            <motion.circle
              cx={size / 2}
              cy={size / 2}
              r={radius}
              fill="none"
              strokeWidth={stroke}
              strokeLinecap="round"
              strokeDasharray={circumference}
              // Reduced motion means the arc reports the value rather than travelling to
              // it. The spring is still constructed — it is a hook — but nothing reads it,
              // so the arc and the number land together in one frame.
              strokeDashoffset={reducedMotion ? offsetFor(value ?? 0) : offset}
              className="stroke-primary"
            />
          )}
        </motion.svg>

        {!indeterminate && showValue ? (
          <div className="absolute inset-0 flex items-center justify-center">
            {/* The number reads the same spring as the arc, so the two can never
                disagree. */}
            <motion.span
              aria-hidden
              className="font-semibold tabular-nums text-foreground"
              style={{ fontSize: Math.round(size * 0.19) }}
            >
              {reducedMotion ? `${rounded}%` : readout}
            </motion.span>
          </div>
        ) : null}
      </div>
    )
  }
)
ProgressRing.displayName = "ProgressRing"

/**
 * The indeterminate arc grows and shrinks as it sweeps.
 *
 * A fixed arc spinning at a constant rate is a loading spinner, and it says only "still
 * running". Letting the arc lengthen and shorten under the rotation is what makes it read
 * as *work happening* rather than as a wheel turning: the sweep and the rotation are
 * deliberately different lengths, so the pattern never lands in the same place twice in a
 * row.
 */
function IndeterminateArc({
  cx,
  cy,
  r,
  stroke,
  circumference,
  reducedMotion,
}: {
  cx: number
  cy: number
  r: number
  stroke: number
  circumference: number
  reducedMotion: boolean
}) {
  if (reducedMotion) {
    // No rotation, no sweep. A static gap still reads as "in progress" without anything
    // moving — and an endlessly repeating animation is the one thing the preference most
    // clearly means to stop.
    return (
      <circle
        cx={cx}
        cy={cy}
        r={r}
        fill="none"
        strokeWidth={stroke}
        strokeLinecap="round"
        strokeDasharray={`${circumference * 0.25} ${circumference}`}
        className="stroke-primary"
      />
    )
  }

  return (
    <motion.circle
      cx={cx}
      cy={cy}
      r={r}
      fill="none"
      strokeWidth={stroke}
      strokeLinecap="round"
      className="stroke-primary"
      initial={{
        strokeDasharray: `${circumference * 0.06} ${circumference}`,
        strokeDashoffset: 0,
      }}
      animate={{
        strokeDasharray: [
          `${circumference * 0.06} ${circumference}`,
          `${circumference * 0.65} ${circumference}`,
          `${circumference * 0.06} ${circumference}`,
        ],
        strokeDashoffset: [0, -circumference * 0.2, -circumference * 0.95],
      }}
      transition={{
        duration: 1.6,
        repeat: Infinity,
        ease: "easeInOut",
        times: [0, 0.5, 1],
      }}
    />
  )
}

export { ProgressRing }
```
