aiellie

Search

Find a component, template or page

GitHub
Design

Meter

A reading inside a known range, like a context window or a credit balance, with a label, a formatted value and a fill that can turn to a status tone.

Context window
x
Monthly credits
x
Storage almost full
x
Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/meter"

shadcn needs the @aiellie registry in components.json first. npx aiellie add meter adds it on its first run, then installs the same way.

Meter shows how much of something is used: a context window, monthly credits, storage. It is Base UI's meter, so it is a measurement rather than a progress bar, and screen readers read it as one. A label and a value sit over a thin track, and a tone turns the fill green or red when the amount means something.

Getting started

  1. Add a meter with its label and value

    value is out of max, 100 by default. The track is drawn for you; the label and the value are children, so you choose what shows.

    components/credits.tsx
    import { Meter, MeterLabel, MeterValue } from "@/components/ui/meter"
    
    export function Credits({ used }: { used: number }) {
      return (
        <Meter value={used}>
          <MeterLabel>Monthly credits</MeterLabel>
          <MeterValue />
        </Meter>
      )
    }
  2. Format the value

    format takes Intl.NumberFormat options. MeterValue takes a function for text of your own, and getAriaValueText says the same to a screen reader.

    <Meter
      value={64_000}
      max={200_000}
      format={{ notation: "compact" }}
      getAriaValueText={(tokens) => `${tokens} of 200K tokens`}
    >
      <MeterLabel>Context window</MeterLabel>
      <MeterValue>{(tokens) => `${tokens} / 200K`}</MeterValue>
    </Meter>

Anatomy

<div data-slot="meter" data-tone="default" role="meter">
  <span data-slot="meter-label" />
  <span data-slot="meter-value" /> {/* pushed to the end */}
  <div data-slot="meter-track"> {/* added for you, under them */}
    <div data-slot="meter-indicator" />
  </div>
</div>

The track is 4px tall on muted, and the fill is foreground, destructive, or emerald for success, since a shadcn theme has no success token. Only the fill changes with the tone, so the label stays readable. A change of value slides the fill over 280ms.

Examples

A tone that follows the value

Turn the fill red once the window is nearly full.

Context window
x

Without a label

With no children, give the meter an aria-label so it still has a name.

<Meter value={40} aria-label="Storage used" className="w-24" />

API reference

Meter

Base UI's Meter.Root, with the track inside it.

PropTypeDefaultDescription
valuenumberrequiredHow much is used.
min / maxnumber0 / 100The range.
tone"default" | "success" | "destructive""default"Recolours the fill only.
formatIntl.NumberFormatOptionsHow MeterValue and screen readers write the number.
getAriaValueText(formatted: string, value: number) => stringWhat a screen reader says for the value.
classNamestringMerged onto the root.

Parts

ExportRendersNotes
MeterLabelspanIts name, in muted-foreground.
MeterValuespanThe formatted value at the end. Takes a function of the formatted value for text of your own.
MeterTrack / MeterIndicatordivThe track and its fill. Meter adds them; export them to build a meter of your own from Base UI's root.