Meter
Usage meters as bars or pixel rows.
Demo
Usage
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
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | — | The measured value. Outside 0 to `max` it is clamped, for the fill and for `aria-valuenow`. |
max | number | 100 | The full scale. |
label | string | — | The accessible name, e.g. "Storage". |
variant | 'bar' | 'pixels' | 'bar' | `bar` is a thin continuous track; `pixels` lights a row of slanted segments. |
segments | number | 20 | Number of pixels in the `pixels` variant. |
tone | Tone | 'accent' | The fill's colour, until the value reaches a threshold. |
thresholds | readonly 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. |
estimated | boolean | false | Draws the fill hatched, for a value that is a forecast or a guess, and reads it as estimated ("62%, estimated"). |
valueText | string | — | 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). |
…rest | HTMLAttributes<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 bylabel, witharia-valuemin,aria-valuemaxandaria-valuenow; a value outside 0 tomaxis clamped for the fill and foraria-valuenow. valueTextsetsaria-valuetext, the value as read (“68 GB of 100 GB”). Withestimatedit becomesmessages.estimatedValue, “$365 of $500, estimated”, or the percent when there is novalueText(“74%, estimated”).- With
thresholds, the meter is described (aria-describedby, a visually hidden span) bymessages.meterThresholds: “Thresholds at Nearly full and Full”, each by its label or, without one, its percent ofmax. - 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.