Domphy

Ring Progress

A circular progress indicator rendered via CSS conic-gradient masked into a donut shape. Progress advances clockwise from 12 o'clock. Sets role="progressbar" with aria-valuenow/min/max. Apply to a <div>.

Props

PropTypeDefaultDescription
valueValueOrState<number>0Progress percentage 0–100.
colorValueOrState<ThemeColor>"primary"Theme color for the filled arc.
trackColorValueOrState<ThemeColor>"neutral"Theme color for the background track.
sizenumber16Diameter in themeSpacing units (e.g. 164em).
thicknessnumber0.25Ring stroke as a fraction of the radius. 0 = no ring, 0.5 = solid disc.

Example

import { toState } from "@domphy/core";
import { ringProgress } from "@domphy/ui";

const progress = toState(72);

const App = {
  div: null,
  $: [ringProgress({ value: progress, color: "success", size: 20 })],
};
Customization

Must see the source of patch at the bottom of each patch page to understand the structure then code it still code as html native element.

There are four levels of customization, in increasing order of effort:

  1. Patch props. Each patch exposes a small, stable set of props—typically fewer than five. Lowest friction.
  2. Context attributes. Use dataTone, dataSize, and dataDensity on a container to shift tone, size, or density for an entire subtree without touching individual elements.
  3. Inline override. Native-wins merge strategy: any property set directly on the element overrides the patch value.
  4. Create a variant. Clone a similar patch and edit it. Use this only when you need a reusable custom version.
Formulas

Unit - U = fontSize / 4 - convert final values with themeSpacing(n).

Size - n = intrinsic text lines, w = wrapping level, d = density factor:

height        = (n * 6 + 2 * d * w) * U
paddingBlock  = d * w * U
paddingInline = ceil(3 / w) * d * w * U
radius        = d * w * U

Base density d = 1.5:

Uw=0w=1w=2w=3
height (n = 1)691215
paddingBlock01.534.5
paddingInline34.564.5
radius01.534.5

Tone - K = N / 2 where N is the palette length. For N = 18, K = 9.

RoleShiftn=0
Backgroundparent +/- n0
Textbg + K6
Borderbg + K/23
Hoverbg + 2K/34
Selected / Focusabove +/- K/32-4

State shift range: K/3 <= delta <= 2K/3.

import type { PartialElement, StyleObject } from "@domphy/core";
import { toState, type ValueOrState } from "@domphy/core";
import { type ThemeColor, themeColor, themeSpacing } from "@domphy/theme";

/**
 * Circular ring progress indicator rendered via CSS `conic-gradient` and a
 * circular `mask`. Progress starts at 12 o'clock and advances clockwise.
 * Exposes `role="progressbar"` with `aria-valuenow/min/max`. Apply to a `<div>`.
 *
 * @hostTag div
 * @param props.value - Progress percentage 0–100. Accepts a value or reactive state. Defaults to `0`.
 * @param props.color - Theme color for the filled arc. Accepts a value or reactive state. Defaults to `"primary"`.
 * @param props.trackColor - Theme color for the background track. Accepts a value or reactive state. Defaults to `"neutral"`.
 * @param props.size - Diameter in `themeSpacing` units. Defaults to `16` (= `4em`).
 * @param props.thickness - Ring stroke as a fraction of the radius (0–0.5). Defaults to `0.25`.
 * @example { div: null, $: [ringProgress({ value: 65 })] }
 * @example { div: null, $: [ringProgress({ value: loadingState, color: "success", size: 20 })] }
 */
function ringProgress(
  props: {
    value?: ValueOrState<number>;
    color?: ValueOrState<ThemeColor>;
    trackColor?: ValueOrState<ThemeColor>;
    size?: number;
    thickness?: number;
  } = {},
): PartialElement {
  const { size = 16, thickness = 0.25 } = props;
  const value = toState(props.value ?? 0, "value");
  const color = toState(props.color ?? "primary", "color");
  const trackColor = toState(props.trackColor ?? "neutral", "trackColor");

  // Mask cuts the center to create a donut shape.
  // Uses `closest-side` so the gradient circle matches the visible element circle
  // exactly, making `holePercent` directly represent the fraction of outer radius.
  const holePercent = Math.round((1 - thickness) * 100);
  const mask = `radial-gradient(circle closest-side, transparent ${holePercent}%, black ${holePercent}%)`;

  return {
    role: "progressbar",
    ariaValuenow: (l) => String(Math.round(value.get(l))),
    ariaValuemin: "0",
    ariaValuemax: "100",
    _onInsert: (node) => {
      if (node.tagName !== "div") {
        console.warn('"ringProgress" patch must use div tag');
      }
    },
    style: {
      display: "inline-flex",
      alignItems: "center",
      justifyContent: "center",
      width: themeSpacing(size),
      height: themeSpacing(size),
      borderRadius: "50%",
      // Fill arc from 12 o'clock clockwise; `from -90deg` rotates the start point.
      background: (l) =>
        `conic-gradient(from -90deg, ${themeColor(l, "shift-9", color.get(l))} ${value.get(l)}%, ${themeColor(l, "shift-3", trackColor.get(l))} 0%)`,
      WebkitMask: mask,
      mask,
    } as StyleObject,
  };
}

export { ringProgress };