Meter

Slant / Data

Usage meters as bars or pixel rows.

Demo

Meters

Usage

@tuttn/slant
import { Meter } from '@tuttn/slant'

<Meter value={72} label="Upload" />
<Meter value={18} label="Storage" variant="pixels" />
<Meter value={92} max={100} label="Quota" tone="warning" segments={25} />

// Out-of-range values clamp: this one reads and draws as full.
<Meter value={130} max={100} label="Seats" tone="danger" />

// Thresholds: a mark at each, and once the value reaches one, the fill takes
// the tone of the highest reached (warning unless it says otherwise).
<Meter value={used} label="Storage" thresholds={[
  { value: 80, label: 'Nearly full' },
  { value: 95, label: 'Full', tone: 'danger' },
]} />

// Estimated: drawn hatched, and read as "$365 of $500, estimated".
<Meter value={365} max={500} label="Spend forecast" estimated valueText="$365 of $500" />

Props

PropTypeDefaultDescription
valuenumber—The measured value. Outside 0 to `max` it is clamped, for the fill and for `aria-valuenow`.
maxnumber100The full scale.
labelstring—The accessible name, e.g. "Storage".
variant'bar' | 'pixels''bar'`bar` is a thin continuous track; `pixels` lights a row of slanted segments.
segmentsnumber20Number of pixels in the `pixels` variant.
toneTone'accent'The fill's colour, until the value reaches a threshold.
thresholdsreadonly MeterThreshold[]—Marks across the track, each at its value. Once the value reaches one, the fill takes the tone of the highest reached. They are read in the meter's description.
estimatedbooleanfalseDraws the fill hatched, for a value that is a forecast or a guess, and reads it as estimated ("62%, estimated").
valueTextstring—The value as read (`aria-valuetext`), e.g. "62 of 100 GB". By default none, so the number is read; with `estimated`, the percent.
MeterThreshold{ value: number; label?: string; tone?: Tone }—One entry of thresholds: where it sits (0 to max), its name in the description, and the fill’s tone once reached (default warning).
…restHTMLAttributes<HTMLDivElement>—className, style, other attributes and the ref go on the root, the element with role="meter". Your aria-describedby is kept, ahead of the thresholds’ description.

Accessibility

  • The root has role="meter", named by label, with aria-valuemin, aria-valuemax and aria-valuenow; a value outside 0 to max is clamped for the fill and for aria-valuenow.
  • valueText sets aria-valuetext, the value as read (“68 GB of 100 GB”). With estimated it becomes messages.estimatedValue, “$365 of $500, estimated”, or the percent when there is no valueText (“74%, estimated”).
  • With thresholds, the meter is described (aria-describedby, a visually hidden span) by messages.meterThresholds: “Thresholds at Nearly full and Full”, each by its label or, without one, its percent of max.
  • The tone that follows a threshold is never the only sign: the threshold marks sit on the track, and the description names them.

A meter is read-only: it shows the value you pass and holds no state. The tone colours the bar and the lit pixels.

Thresholds

Each threshold draws a hairline across the bar at its share of max, standing a little past the track on both sides; in the pixels variant, the cell that starts at a threshold has its leading edge marked (one at the very end marks the last cell’s far side). Once value reaches a threshold, the fill takes that threshold’s tone, and the root’s data-tone follows: with several reached, the highest wins. Below them all, the fill keeps tone.

Estimated

estimated draws the fill, or each lit pixel, as a repeating diagonal stripe of the tone over its soft tint instead of a flat fill, for a forecast or a guess, and reads the value as estimated. Under dir="rtl" the track fills from the right and the thresholds mirror with it.

Full source