Components / FeedbackStable

Segmented progress

Shows how far along something is as a row of small cells that ramp from a light to a deep accent blue. It reads at a glance when the amount counts whole things.

View as Markdown

Playground

5 left

Code

SegmentedProgress.tsx
'use client'
import { useState, type CSSProperties, type ReactNode } from 'react'

// Nacre SegmentedProgress: how far along something is, as a row of small cells. It reads faster than a bar when
// the amount counts whole things (screens audited, lessons done). Progress (Progress.tsx) is the plain bar for a
// stream such as an upload. React plus Tailwind classes only; needs nacre-theme.css. The filled cells ramp from a
// light to a deep accent: a fixed ramp of color-mix steps on the accent role, so it follows both themes.
// Motion: when the value changes, the new cells fade and scale in left to right, 14 ms apart (quick 160, out),
// by opacity and scale only. It is display only, so a key press never starts it. Reduced motion and
// [data-nacre-motion="off"] change at once.

export type SegmentedProgressProps = {
  label: string
  /** Cells filled, from 0 to cells. */
  value: number
  /** How many cells the track has. */
  cells?: number
  /** A short line after the track, such as 5 left. */
  trailing?: ReactNode
  className?: string
}

/** The accent mixed toward white on the first half of the track and toward black on the second half. */
function ramp(i: number, n: number): string {
  const t = n > 1 ? i / (n - 1) : 1
  const white = Math.round(55 * Math.max(0, 1 - t * 2))
  const black = Math.round(32 * Math.max(0, t * 2 - 1))
  return `color-mix(in oklab, black ${black}%, color-mix(in oklab, white ${white}%, var(--nx-accent)))`
}

export function SegmentedProgress({ label, value, cells = 24, trailing, className = '' }: SegmentedProgressProps) {
  const total = Math.max(1, Math.round(Number.isFinite(cells) ? cells : 24))
  const filled = Math.min(Math.max(Math.round(Number.isFinite(value) ? value : 0), 0), total)
  // The value before the last change decides which cells animate and in what order (stored in state, so render stays pure).
  const [seen, setSeen] = useState(filled)
  const [from, setFrom] = useState(filled)
  if (seen !== filled) {
    setFrom(seen)
    setSeen(filled)
  }
  return (
    <div data-part="root" className={`flex items-center gap-3.5 ${className}`}>
      <div
        role="progressbar"
        aria-label={label}
        aria-valuemin={0}
        aria-valuemax={total}
        aria-valuenow={filled}
        data-part="track"
        className="flex h-[26px] min-w-0 flex-1 gap-0.5 rounded-[10px] bg-sunken p-[3px]"
      >
        {Array.from({ length: total }, (_, i) => {
          const on = i < filled
          // Filling runs left to right from the first new cell; emptying runs right to left from the last one.
          const step = filled >= from ? i - from : from - 1 - i
          return (
            <i
              key={i}
              data-part="cell"
              className="relative flex-1 rounded-[4px] bg-[color-mix(in_oklab,var(--nx-fg)_8%,var(--nx-sunken))]"
            >
              <span
                data-part="fill"
                data-on={on ? '' : undefined}
                style={{ backgroundColor: ramp(i, total), '--step': Math.min(Math.max(step, 0), 20) } as CSSProperties}
                className={`absolute inset-0 rounded-[inherit] motion-safe:transition-[opacity,scale] motion-safe:duration-(--nx-dur-quick) motion-safe:ease-out motion-safe:[transition-delay:calc(var(--step)*14ms)] [[data-nacre-motion=off]_&]:[transition-delay:0s] ${on ? 'scale-100 opacity-100' : 'scale-50 opacity-0'}`}
              />
            </i>
          )
        })}
      </div>
      {trailing != null && <span data-part="trailing" className="shrink-0 text-body font-[500] whitespace-nowrap text-fg">{trailing}</span>}
    </div>
  )
}

Usage

When to use

  • Progress through a set of whole items: 19 of 24 screens audited.
  • A compact meter inside a card or a list row, with a short line after it such as 5 left.

Choose something else when

In the Nacre workspace

Audit coverage

The Checkout flow audit has 24 screens. 19 cells are filled and the line after the track says 5 left; each screen audited fills the next cell.

Brand tokens

A Brand panel row shows 21 of 24 cells for tokens in use and 3 unused at the end, so the gap is the story.

Do and don't

  • DoSet cells to the number of things being counted, up to about 24.

    Don'tUse 100 cells to fake a percentage; use Progress.

  • DoSay what is counted in label, and put the remainder in trailing.

    Don'tLeave the meaning to the colour alone.

Anatomy

5 left
  1. Track. 26 px sunken bar, radius 10 px, 3 px padding and a 2 px gap between cells.
  2. Cell. One equal slot, radius 4 px, a little darker than the track while empty.
  3. Fill. The colour of a filled cell: a fixed ramp of color-mix steps from a light to a deep accent, the same in both themes.
  4. Trailing line. Body 14/20 at weight 500 in fg, 14 px after the track.

API

labelType stringDefault -Required. Names the bar for assistive tech (aria-label): Checkout flow audit.
valueType numberDefault -Cells filled, rounded and clamped between 0 and cells.
cellsType numberDefault 24How many cells the track has. aria-valuemax follows it.
trailingType ReactNodeDefault -A short line after the track, such as 5 left. Not part of the accessible name.
classNameType stringDefault -Extra classes on the root.

Motion

Value changeProperty opacity and scale .5 to 1 of the cells that change, left to right, 14 ms apart (emptying runs right to left)Duration quick 160Easing outKeyboard Same: the bar only reports a value, a key press never starts it. Instant with reduced motion or Motion off.
5 left

Accessibility

Keyboard

Tab
Not focusable. It reports a value; it takes no input.

ARIA and semantics

  • role progressbar with aria-label, aria-valuenow, aria-valuemin 0 and aria-valuemax set to the cell count.
  • The cells are decorative: assistive tech hears one value, not 24 elements.
  • The filled share is also readable from the trailing line, so colour is never the only signal.

Contrast

Trailing linefg on surfaceLight 14.89:1Dark 12.33:1Needs 4.5:1
The deep end of the ramp on the track (a graphic, 3:1)accent on sunkenLight 4.97:1Dark 8.47:1Needs 3:1