Meter
A share of a whole as a hairline bar, with an optional tick for where the value should be, so it answers “ahead or behind?” without a second chart.
Preview
Monthly budget42%
Search64%
Direct22%
Referral14%
Install
npx shadcn@latest add @dxbh-ui/meter
Add the @dxbh-ui registry to components.json once:
{
"registries": {
"@dxbh-ui": "https://ui.dxbh.dev/r/{name}.json"
}
}Usage
"use client";
import { useState } from "react";
import { Meter } from "@/components/ui/meter/meter";
export default function MeterDemo() {
const [spent, setSpent] = useState(42);
return (
<div className="grid w-full max-w-sm gap-6">
<div className="grid gap-2">
<div className="flex items-baseline justify-between text-sm">
<span className="text-muted-foreground">Monthly budget</span>
<span className="font-medium tabular-nums">{spent}%</span>
</div>
{/* The tick marks where spend should be by today. */}
<Meter value={spent} marker={60} size="md" tone={spent > 60 ? "negative" : "neutral"} label="Monthly budget spent" />
<div className="flex gap-2">
{[-10, 10].map((step) => (
<button
key={step}
type="button"
onClick={() => setSpent((v) => Math.min(100, Math.max(0, v + step)))}
className="h-8 rounded-md border px-3 text-sm font-medium transition-transform duration-(--duration-press) ease-out hover:bg-muted active:scale-[0.97]"
>
{step > 0 ? `+${step}` : step}
</button>
))}
</div>
</div>
<div className="grid gap-2 text-sm">
{[
["Search", 64],
["Direct", 22],
["Referral", 14],
].map(([name, share]) => (
<div key={name} className="flex items-center gap-3">
<Meter value={share as number} size="xs" />
<span className="text-muted-foreground">{name}</span>
<span className="ml-auto tabular-nums">{share}%</span>
</div>
))}
</div>
</div>
);
}Props
| Prop | Type | Default |
|---|---|---|
value*0–100, clamped. | number | — |
markerExpected position, 0–100, drawn as a tick. | number | — |
toneFill colour. | "neutral" | "positive" | "negative" | "neutral" |
sizexs is a 32px inline bar; sm and md span the container. | "xs" | "sm" | "md" | "sm" |
labelAccessible name. Omit only when the figure is beside it. | string | — |
valueTextSpoken value. Defaults to the percentage. | string | — |
Polish
- Fill is a full-width bar scaled with scaleX from origin-left, never an animated width.
- Updates ease with the modal duration token on transform only; first paint doesn't animate.
- Reduced motion makes updates instant.
- Brand inks replaced with foreground, --positive and --negative on the shadcn muted track.
- role="meter" with aria-valuetext when labelled; hidden when the figure is printed beside it.
Source
import { clampPercent } from "@/lib/chart-geometry";
import { cn } from "@/lib/utils";
export type MeterTone = "neutral" | "positive" | "negative";
export type MeterProps = {
/** 0–100. Clamped. */
value: number;
/** An expected position, 0–100, drawn as a tick ("where spend should be today"). */
marker?: number;
tone?: MeterTone;
size?: "xs" | "sm" | "md";
/** Accessible name. Omit only when the figure is printed right beside it. */
label?: string;
/** Spoken value, e.g. "$4,200 of $6,000". Defaults to the percentage. */
valueText?: string;
className?: string;
};
const TONE: Record<MeterTone, string> = {
neutral: "bg-foreground",
positive: "bg-positive",
negative: "bg-negative",
};
const SIZE = { xs: "h-1 w-8", sm: "h-1 w-full", md: "h-1.5 w-full" } as const;
/**
* A share of a whole as a hairline bar. `xs` is the inline 32px bar beside a
* row label; `sm`/`md` span their container.
*
* The fill is a full-width bar scaled from its left edge, never a width: when
* the value updates, only `transform` moves, on the compositor. First paint
* doesn't animate (there is nothing to transition from).
*/
export function Meter({ value, marker, tone = "neutral", size = "sm", label, valueText, className }: MeterProps) {
const pct = clampPercent(value);
const tick = marker === undefined ? null : clampPercent(marker);
return (
<span
data-slot="meter"
role={label ? "meter" : undefined}
aria-label={label}
aria-valuenow={label ? Math.round(pct) : undefined}
aria-valuemin={label ? 0 : undefined}
aria-valuemax={label ? 100 : undefined}
aria-valuetext={label ? (valueText ?? `${Math.round(pct)}%`) : undefined}
aria-hidden={label ? undefined : true}
className={cn("relative block shrink-0", SIZE[size], className)}
>
<span className="absolute inset-0 overflow-hidden rounded-full bg-muted">
<span
data-slot="meter-fill"
className={cn(
"block h-full w-full origin-left",
"transition-transform duration-(--duration-modal,240ms) ease-out motion-reduce:transition-none",
TONE[tone],
)}
style={{ transform: `scaleX(${pct / 100})` }}
/>
</span>
{tick !== null ? (
<span
data-slot="meter-marker"
className="absolute -inset-y-1 w-px -translate-x-1/2 bg-foreground"
style={{ left: `${tick}%` }}
/>
) : null}
</span>
);
}Extracted from Vesrelle/components/data/meter.tsx at 4619651.