How it works
Every segment is the same circle path starting at 12 o'clock with pathLength="1", rotated to its start and revealed with stroke-dashoffset, with round caps and a 5px gap between neighbours. The entrance is one clockwise sweep on a single ease-out curve: each segment gets its own slice of it as an exact cubic-bezier, so it stays pure CSS. After the entrance, data changes move all segments together from where they are. The centre total and legend values count on the same curve.
Use it for
- What a whole is made of, in 2 to 5 parts with clear differences: spending by category, time by activity.
- Progress toward a goal on a card that also explains it: a course, a savings goal, a profile to complete.
Look elsewhere when
- More than 5 or 6 parts, or close values to compare: use ranked bars.
- Exactly used versus left with a legend: the gauge chart says it more directly.
- Shares over time: use a stacked bar chart.
Usage
import { DonutChart } from './DonutChart.jsx'
export function SpendingCard() {
return (
<DonutChart
title="Spending"
qualifier="November"
centerLabel="Spent"
formatValue={(n) => `$${n.toLocaleString('en-US')}`}
data={[
{ name: 'Housing', value: 1240 },
{ name: 'Food', value: 620 },
{ name: 'Transport', value: 310 },
{ name: 'Leisure', value: 186 },
{ name: 'Other', value: 124 },
]}
/>
)
}
export function CourseCard() {
return (
<DonutChart
title="Course progress"
tone={8}
size={120}
data={[{ name: 'Completed', value: 25 }]}
total={100}
subject="Watercolour basics"
description="Lesson 4 of 16"
link={{ label: 'Resume lesson', href: '/lessons/4' }}
/>
)
}Props
| Prop | Type | Default | What it does |
|---|---|---|---|
title / qualifier | string | — | Card title (15px semibold) and grey words after it. |
icon | ReactNode | — | An 18px icon in a 32px square tinted with the card colour. |
onAction / actionIcon / actionLabel / action | () => void / 'open' | 'more' / string / ReactNode | - / 'open' | A round chevron or three-dot button at the right of the title row, its accessible name, or your own element instead. |
data | { name: string, value: number, color?: string }[] | — | Parts clockwise from 12 o'clock. Several items are coloured --ds-chart-N in order starting at tone; a single item takes the tone colour. Zero is not drawn, negatives count as 0, beyond 8 items the rest merge into “Other”. |
total | number | — | The whole. When larger than the sum, the rest is a faint track: one item and total={100} is a progress ring. |
center | 'total' | 'percent' | 'percent' for one item, otherwise 'total' | What the centre shows at rest: the sum, or the sum as a share of total. |
centerLabel | string | — | Grey caption under the centre number. |
subject / description / link | string / string / { label, href?, onClick? } | — | Content beside the ring instead of the legend: a heading, a sentence and a link or button, as on a course card. |
showLegend | boolean | true | Shows the legend (dot, name, counted value, share) beside a donut of two or more items. |
tone | 1-8 | 1 | Card colour: the single-item ring, the icon tint and the link; several items start their colours here. |
size / thickness | number | 132 / 14 | Ring diameter and stroke width in px. |
formatValue / formatPercent | (n) => string | toLocaleString('en-US') / `${Math.round(p)}%` | Number and share formats for the centre, legend and hidden table. |
text | object | — | Replaces built-in words (total, other, open, more, ring, announce); shallow-merged over the English defaults. |
label | string | — | Accessible name of the ring and caption of the hidden table; defaults to the title. |
replayKey | any | — | Change it to play the sweep again from zero. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the card. |
Accessibility
- The ring is a focusable group whose name gives the chart name, total and number of parts; arrow keys step through parts, Home and End jump, Escape clears, and each step is announced.
- Legend rows are buttons; focusing or hovering one highlights its segment and shows it in the centre.
- A visually hidden table lists every part with its value and share, so colour and hover are never needed to read the data.
- With reduced motion the ring and numbers appear complete at once.
Motion
When 30% of the card is on screen the segments sweep clockwise in order within 900ms, as one ease-out stroke; the centre number and legend values count up on the same curve. Later changes move segments together in 480ms on cubic-bezier(0.22, 1, 0.36, 1); hovering softens the other segments in 160ms and fades the new centre text in.
Add it to your project
Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.
DonutChart.jsxdonut-chart.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.

