Components
Meter
Shows where a measurement stands within a range, such as storage used or battery left, and changes color as it crosses thresholds.
Introduction
Meter shows where a value stands in a range: 72 GB of 100 used, a password that is strong, a battery at 18%. It is a measurement of something that exists, not the progress of a task that is running. For a task, such as an upload, use a progress bar.
- Announced as a
meterto assistive technology, with its value, its range and the value as text. - The value is written as a percentage, as a number with a unit, or in your own words.
- The fill takes a color, or changes color when the value crosses a
thresholdspoint, so a storage meter turns yellow, then red, as it fills. It works the other way for a battery. - The fill grows smoothly when the value changes, and jumps for people who prefer reduced motion.
Do not rely on color alone: keep the written value, so the meaning does not depend on telling green from red.
Installation
centerUI has no CLI or registry yet: components are copied into your project.
# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components tailwind-variants tailwind-merge clsxManual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/meter-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for Meter: a track, and a fill as wide as the value.
export const meterStyles = tv({
slots: {
root: "flex w-full flex-col gap-1.5",
header: "flex items-baseline justify-between gap-3 text-sm",
label: "font-medium",
value: "text-muted-fg tabular-nums",
track: "w-full overflow-hidden rounded-full bg-fg/10",
fill: "h-full rounded-full transition-[width,background-color] duration-500 ease-out motion-reduce:transition-none",
},
variants: {
size: {
sm: { track: "h-1.5" },
md: { track: "h-2" },
lg: { track: "h-3" },
},
variant: {
default: { fill: "bg-primary" },
success: { fill: "bg-success" },
warning: { fill: "bg-warning" },
danger: { fill: "bg-danger" },
},
},
defaultVariants: { size: "md", variant: "default" },
});2. Add the Meter file
src/components/ui/meter/meter.tsxShow code
"use client";
import type { ReactNode } from "react";
import { Meter as AriaMeter, Label } from "react-aria-components";
import { cn } from "@/lib/cn";
import { meterStyles } from "@/types/meter-styles";
type Variant = "default" | "success" | "warning" | "danger";
/**
* From this point on the meter takes this color. `at` is a fraction of the way
* from `minValue` to `maxValue`, from 0 to 1.
*/
export interface MeterThreshold {
at: number;
variant: Variant;
}
export interface MeterProps {
/** The measurement. Not a task's progress: for that, use a progress bar. */
value: number;
/** The lowest value it can have. Defaults to 0. */
minValue?: number;
/** The highest value it can have. Defaults to 100. */
maxValue?: number;
/** The name of what is measured, shown above the track. */
label?: ReactNode;
/** Show the value, at the right of the label. Defaults to `true`. */
showValue?: boolean;
/**
* How the value is written. The default is a percentage of the range. Use
* `{ style: "unit", unit: "gigabyte" }` and the like for a real unit.
*/
formatOptions?: Intl.NumberFormatOptions;
/**
* Replaces the written value with your own text, such as "72 of 100 GB
* used". It is also what a screen reader hears.
*/
valueLabel?: string;
/** The color of the fill. Defaults to the neutral one. */
variant?: Variant;
/**
* Change the color as the value moves through the range, for example
* `[{ at: 0.7, variant: "warning" }, { at: 0.9, variant: "danger" }]`. The
* last threshold the value has reached wins, and `variant` is the color
* before the first one.
*/
thresholds?: MeterThreshold[];
size?: "sm" | "md" | "lg";
/** Names the meter when it has no `label`. */
"aria-label"?: string;
className?: string;
}
/**
* Shows where a value stands within a range: storage used, the strength of a
* password, battery left. It is a measurement, not the progress of a task, so
* it is announced as a meter and not as a progress bar. The fill changes
* color when the value crosses a threshold.
*
* @example
* <Meter label="Storage" value={72} />
* <Meter
* label="Battery"
* value={18}
* thresholds={[
* { at: 0, variant: "danger" },
* { at: 0.2, variant: "warning" },
* { at: 0.5, variant: "success" },
* ]}
* />
*/
export function Meter({
label,
showValue = true,
valueLabel,
variant = "default",
thresholds,
size,
className,
...props
}: MeterProps) {
return (
<AriaMeter
{...props}
valueLabel={valueLabel}
className={cn(meterStyles().root(), className)}
>
{({ percentage, valueText }) => {
// The last threshold that the value has reached decides the color.
const reached = [...(thresholds ?? [])]
.sort((a, b) => a.at - b.at)
.filter((threshold) => percentage / 100 >= threshold.at)
.at(-1);
const styles = meterStyles({
size,
variant: reached?.variant ?? variant,
});
return (
<>
{label || showValue ? (
<div className={styles.header()}>
{label ? (
<Label className={styles.label()}>{label}</Label>
) : (
<span />
)}
{showValue ? (
<span className={styles.value()}>{valueText}</span>
) : null}
</div>
) : null}
<div className={styles.track()}>
<div
className={styles.fill()}
style={{ width: `${percentage}%` }}
/>
</div>
</>
);
}}
</AriaMeter>
);
}3. Use it
import { Meter } from "@/components/ui/meter/meter";Anatomy
A label and a value on top, and a track with a fill under them.
<Meter
label="Storage"
value={72}
thresholds={[{ at: 0.9, variant: "danger" }]}
/>| Part | Rendered as | Notes |
|---|---|---|
| Meter | div with role meter | Reports the value, the range and the written value to assistive technology. |
| Label | label | The name of what is measured, above the track. |
| Value | span | The value as text, at the right of the label. Turn it off with showValue={false}. |
| Track | div | The full range. |
| Fill | div | As wide as the value. It changes color when a threshold is crossed. |
Examples
Usage
Value format
A percentage of the range by default. formatOptions writes the number with a unit, and valueLabel replaces it with your own text, which a screen reader hears too.
Range
minValue and maxValue set the ends. The fill is as wide as the value's place between them.
Variants
The color of the fill: neutral, or the theme's success, warning and danger.
Thresholds
thresholds change the color as the value moves through the range. at is the place in the range, from 0 to 1. The last one the value has reached decides the color, and variant is the color before the first.
Low is bad
For a battery, the low end is the bad one. Start the thresholds at 0 with danger, and let it turn to warning and success as it rises.
Sizes
Without a label
Every meter needs a name. Without a visible label, give it an aria-label. showValue={false} hides the written value too.
Live value
The fill grows to a new value, and its color changes when a threshold is crossed.
Component API
Meter takes the props below.
| Prop | Type | Default |
|---|---|---|
| value | number | - (required) |
| minValue | number | 0 |
| maxValue | number | 100 |
| label | ReactNode | - |
| showValue | boolean | true |
| formatOptions | Intl.NumberFormatOptions | a percentage |
| valueLabel | string | - |
| variant | "default" | "success" | "warning" | "danger" | "default" |
| thresholds | { at: number; variant }[] | - |
| size | "sm" | "md" | "lg" | "md" |
| aria-label | string | - (when there is no label) |
| className | string | - |