Data display
Meter
A known quantity within a range — storage, quota, strength — as a bar or segments, colored by thresholds.
react-aria-components
Preview
Storage42%
Enrichment quota2,780 of 3,000
AI credits96%
Password strengthStrong
"use client";
import { Meter } from "@/components/ui/meter";
export default function Example() {
return (
<div className="flex w-full max-w-sm flex-col gap-6">
<Meter label="Storage" value={42} />
<Meter label="Enrichment quota" value={2780} maxValue={3000} formatOptions={{ style: "decimal" }} valueLabel="2,780 of 3,000" />
<Meter label="AI credits" value={96} />
<Meter label="Password strength" variant="segments" segments={4} value={75} higherIsBetter valueLabel="Strong" />
</div>
);
}Installation
Components are copied into your project — no runtime package.
Install the following dependencies:
terminalnpm install react-aria-componentsCreate a
meter.tsxfile and paste the following code into it.src/components/ui/meter.tsx"use client"; import { Label, Meter as AriaMeter, type MeterProps as AriaMeterProps } from "react-aria-components"; import { cx } from "@/utils/cx"; export interface MeterProps extends Omit<AriaMeterProps, "className" | "children"> { label?: string; /** `bar` continuous track, `segments` discrete blocks (e.g. password strength). */ variant?: "bar" | "segments"; segments?: number; /** Thresholds as fractions of the range: below `warn` is ok, above `danger` is red. */ thresholds?: { warn: number; danger: number }; /** Invert colors when higher is better (e.g. strength). */ higherIsBetter?: boolean; showValue?: boolean; className?: string; } /** A known quantity within a range — storage, quota, strength. Color follows the thresholds. */ export function Meter({ label, variant = "bar", segments = 5, thresholds = { warn: 0.7, danger: 0.9 }, higherIsBetter, showValue = true, className, ...props }: MeterProps) { return ( <AriaMeter {...props} className={cx("flex w-full flex-col gap-2", className)}> {({ percentage, valueText }) => { const f = percentage / 100; const level = higherIsBetter ? (f >= 0.75 ? "ok" : f >= 0.4 ? "warn" : "danger") : f >= thresholds.danger ? "danger" : f >= thresholds.warn ? "warn" : "ok"; const color = level === "danger" ? "bg-error" : level === "warn" ? "bg-warning" : higherIsBetter ? "bg-success" : "bg-accent"; return ( <> {(label || showValue) && ( <div className="flex items-center justify-between text-sm"> {label && <Label className="font-medium text-fg">{label}</Label>} {showValue && <span className="tabular text-fg-secondary">{valueText}</span>} </div> )} {variant === "bar" ? ( <div className="h-2 overflow-hidden rounded-full bg-active"> <div className={cx("h-full rounded-full transition-[width]", color)} style={{ width: `${percentage}%` }} /> </div> ) : ( <div className="flex gap-1"> {Array.from({ length: segments }, (_, i) => ( <div key={i} className={cx("h-1.5 flex-1 rounded-full transition-colors", i < Math.round(f * segments) ? color : "bg-active")} /> ))} </div> )} </> ); }} </AriaMeter> ); }Add the theme tokens and the
cxhelper from Installation if you haven’t yet, then update the import paths to match your project setup.