Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Donut Pill Legend Stats Block for React

A traffic-source donut over a row of pill toggles: switch a source off and its arc leaves the ring, the others grow to fill it, and the shares and the total in the middle are worked out again.

Framework
UI library

Preview

Preview width

Installation

Part of Pro
Pro unlocks the code and one-line install of every Pro button and text effect, plus the MCP for your AI editor.
Have Pro already? Sign in to open it.Opens here as soon as you're Pro.

Overview

A donut with a toggling legend for React with Base UI. A centred header ("45% of visits came from organic search") sits over one framed panel (a hairline frame with a + on each corner): a donut of the last 28 days' visits by source, and under it a band of five pills. Each pill is a toggle showing its weight swatch, the source's name and its share. Switch one off and its arc leaves the ring, the remaining arcs grow to fill the circle, every share is worked out again (whole numbers that add up to 100) and the total in the middle rolls to the visits that remain. One source always stays on, and a polite line says what changed. The arcs start at twelve o'clock and run clockwise in five solid foreground weights, so the chart reads in any theme without a hue. It's built from the toggle group, SlidingNumber and Guides, with SVG circles and a dash pattern for the ring, and the same numbers are in a table for screen readers. The pills wrap onto a second row on a phone.

FAQ

How do I change the sources?

Edit SOURCES at the top of the file: each source is an id, a label, its visits and one of the five WEIGHTS. The arcs, the shares, the total, the pills, the table and the header's headline are all computed from that list. A source keeps its weight whatever is switched off, so a swatch always means the same source.

What changes when I switch a source off?

slicesOf takes the sources that are on and works out each one's arc from its share of the visits that remain, so the ring is always a whole circle. The pill shows "off" instead of a share and its swatch turns hollow. The shares of the sources that remain are rounded by the largest-remainder method, so they add up to exactly 100, and the total in the middle rolls to the new sum. The last source cannot be switched off, which keeps the ring from emptying.

Which control is it, and how does it work with a keyboard?

A toggle group in multiple mode: each pill is a button with a pressed state. Tab reaches the group, the arrow keys move between the pills and Space or Enter toggles the focused one. The ring, the shares and the total follow at once. The pressed pill has a foreground outline and a filled swatch; an off pill is quiet, with a hollow swatch and "off" in place of its share.

Is it accessible?

The pills are named by their text ("Social 15%"), the donut is hidden from assistive technology and a table of the visits and shares sits beside it for screen readers. A polite status line says what changed ("4 of 5 sources on: 83,200 visits") and, if you try to switch off the last source, why nothing happened. The total in the middle is real text.

What moves, and what happens with reduced motion?

The arcs sweep in once when the block loads and grow or shrink with a short CSS transition when you toggle a source; the digits in the middle roll. With reduced motion everything changes at once.

How does it fit on a phone?

The panel keeps its donut (about 240 px) and the pills wrap onto as many rows as they need, centred, with no sideways scroll at 320 px.

  • Pro

    Donut Breakdown Stats

    Parts of a whole

    A budgeting app's month of spending as a framed donut with its total rolling up in the middle, beside a legend of five buckets with amount and share, the shares adding up to exactly 100%.

  • Pro

    An ad-spend donut whose middle follows your choice: it shows the total until you press a channel, then that channel's spend and share while its arc lifts out of the ring.

  • Pro

    A city's electricity mix as a framed donut with four callouts standing round it at twelve, three, six and nine o'clock, each joined to its arc by a thin leader line; on a phone, a two by two grid.

  • Pro

    One thick bar of a consultancy's week split into four segments at four weights with labels inside the wide ones, and a band of four legend cells under it; pointing at or focusing a cell brackets its segment.

  • Pro

    Twelve weeks of support tickets stacked by priority in three foreground weights, with toggles that double as the legend: switch a priority off and the stack, the scale and every total re-read.

  • Pro

    Half Donut Card Stats

    Parts of a whole

    A team's weekly capacity as a 180-degree arc of four segments with the total in its foot and a four-cell legend band, with no needle: one whole, shared out.