Spending Ring

An interactive circular spending chart with spring rotation, drag-to-spin gestures, and category breakdowns.

Spending Ring

An interactive circular spending chart with spring rotation, drag-to-spin gestures, and category breakdowns.

The ring puts the selected category under the marker at 12 o'clock and shows its amount, share of the total, and month-over-month change in the center. Turn it by dragging, clicking a slice or a legend row, or using the arrow keys. A flick is projected ahead so the ring keeps moving before settling onto the category it was heading toward.

Installation

Install the component with the Craft-UI Blocks CLI — it drops spending-ring.tsx into your project and wires up its runtime dependencies:

npx mvpblocks add spending-ring
pnpm dlx mvpblocks add spending-ring
yarn dlx mvpblocks add spending-ring
bunx mvpblocks add spending-ring

File Structure

The CLI saves the component to your project's components/ui folder:

Import the default export from @/components/ui/spending-ring. The prop type is exported as SpendingRingProps, and the category shape as SpendingCategory.

Usage

SpendingRing is a client component and every prop is optional — with no props it renders six demo categories. Drop it into any card slot:

"use client";

import SpendingRing from "@/components/ui/spending-ring";

export function BudgetCard() {
  return <SpendingRing />;
}

Pass your own categories with a name, amount, optional percent change, and Tailwind classes for the ring stroke and legend dot. Amounts decide each slice's share of the circle:

const categories = [
  {
    name: "Rent",
    amount: 1800,
    change: 0,
    stroke: "stroke-blue-500 dark:stroke-blue-400",
    dot: "bg-blue-500 dark:bg-blue-400",
  },
  {
    name: "Groceries",
    amount: 620,
    change: -4.2,
    stroke: "stroke-green-500 dark:stroke-green-400",
    dot: "bg-green-500 dark:bg-green-400",
  },
  {
    name: "Dining",
    amount: 280,
    change: 12.5,
    stroke: "stroke-rose-500 dark:stroke-rose-400",
    dot: "bg-rose-500 dark:bg-rose-400",
  },
];

<SpendingRing
  title="Monthly budget"
  categories={categories}
  currency="EUR"
/>

currency is an ISO 4217 code passed to Intl.NumberFormat; the legend rounds to whole units while the header and center show two decimals. Use className to fit the card into a different surface:

<SpendingRing className="max-w-md" />

Theming

The card uses zinc tones: rounded-3xl border-zinc-200 bg-white in light mode, inverting to dark:border-zinc-800 dark:bg-zinc-950. Labels are text-zinc-500 (dark:text-zinc-400), the marker is fill-zinc-900 (dark:fill-zinc-100), and the selected legend row is bg-zinc-100 (dark:bg-zinc-900).

Each category owns its colors through two class strings: stroke for the SVG arc and dot for the legend. A positive change is rose, a negative change is emerald, and zero is muted zinc. The selected arc grows from a 22px to a 30px stroke while the others fade to 40% opacity.

The demo palette is sky, emerald, rose, amber, violet, and zinc. Change any of it in your own categories array:

<SpendingRing
  categories={[
    {
      name: "Savings",
      amount: 1200,
      stroke: "stroke-indigo-500 dark:stroke-indigo-400",
      dot: "bg-indigo-500 dark:bg-indigo-400",
    },
  ]}
/>

Animations

  • Spring: the ring angle uses a damped spring at 11 rad/s with 0.78 damping, so a flick settles with a small give.
  • Flick: release velocity is smoothed from pointer moves, capped at 1440°/s, and projected 0.2s ahead to choose the landing category.
  • Short way round: angle differences wrap into ±180° so the ring never spins the long way to a legend selection.
  • Sub-steps: spring integration is sliced to 1/240s steps to stay stable on slow frames.
  • Segment transition: stroke width and opacity ease over 200ms with cubic-bezier(0.23, 1, 0.32, 1).
  • Center readout: the new category fades and lifts in from 1px below using an @starting-style transition.
  • Reduced motion: selecting a category jumps straight to the target angle with no spin.

How it works

spans converts each amount into a slice of 360°, leaving a 2.4° gap between neighbors. The whole <g> is rotated by writing a transform attribute directly to the SVG node, so the RAF loop never re-renders the chart on every frame.

turnTo sets the spring target to the selected slice's midpoint wrapped to the nearest equivalent angle. While coasting, track() reads whichever slice currently sits under the fixed marker at the top, so the center readout follows the ring as it flies past.

Pointer down captures the pointer and cancels any running spin. A move under 5px is a tap; a larger move unwraps the angle around the center, accumulates a smoothed velocity, and rotates the ring live. A tap outside the inner radius picks the slice under the pointer, while a tap in the middle only updates the readout.

The ring is a focusable role="slider" with aria-valuemin, aria-valuemax, aria-valuenow, and a human-readable aria-valuetext. Arrow keys move one category, Home jumps to the first, and End to the last.

Props

Prop

Type

SpendingCategory

Prop

Type

Registry Bundle

Install the exact registry entry shown on the right when you want the component file and its declared runtime dependencies together. Dependencies: none — the chart is drawn with SVG and the spring physics is written directly with React.

Contact

If you find any bug or issue, feel free to raise an issue.

Last updated on