Usage Meter.
A labelled thin meter for quotas such as an AI allowance: label, percent, bar and an optional reset time. Turns yellow and then red as it fills.
import { UsageMeter } from "@/components/ui/usage-meter";
export default function UsageMeterDemo() {
return (
<div className="grid w-full max-w-xs gap-5">
<UsageMeter label="5-hour window" value={32} reset="Resets at 3:40 pm" />
<UsageMeter label="This week" value={78} reset="Resets Monday" />
<UsageMeter label="Relay bandwidth" value={46} max={50} reset="46 of 50 GB" />
</div>
);
}Installation#
With the CLI
$npx lanterncn add usage-meterThis also installs the Lantern theme and any Lantern UI components it depends on. The shadcn CLI works directly too: npx shadcn@latest add httptim/lantern-ui/usage-meter. See Installation if your project is not set up yet.
Manually
Copy the source into your project
components/ui/usage-meter.tsx"use client"; import * as React from "react"; import { cn } from "@/lib/utils"; import { Progress } from "@/components/ui/progress"; type UsageMeterSize = "default" | "compact"; /** Bar color by how much is used: orange, yellow from `warnAt`, red from `dangerAt`. */ function toneClass(percent: number, warnAt: number, dangerAt: number) { if (percent >= dangerAt) return "bg-destructive"; if (percent >= warnAt) return "bg-warning"; return undefined; } /** * A labelled thin meter for quotas and allowances: label and percent on top, a bar, * and an optional reset time underneath. */ function UsageMeter({ className, label, value, max = 100, reset, size = "default", warnAt = 75, dangerAt = 90, ...props }: Omit<React.ComponentProps<"div">, "children"> & { label: React.ReactNode; /** Amount used, from 0 to `max`. */ value: number; max?: number; /** When the allowance refills, e.g. "resets 3:40 pm". Hidden in the compact size. */ reset?: React.ReactNode; size?: UsageMeterSize; /** Percent at which the bar turns yellow. */ warnAt?: number; /** Percent at which the bar turns red. */ dangerAt?: number; }) { const id = React.useId(); const percent = Math.round(Math.min(100, Math.max(0, (value / max) * 100))); const compact = size === "compact"; return ( <div data-slot="usage-meter" data-size={size} className={cn("flex min-w-0 flex-col", compact ? "w-[5.5rem] gap-1" : "w-full gap-1.5", className)} {...props} > <div className={cn( "flex items-baseline justify-between gap-2 font-mono tabular-nums", compact ? "text-[10px] text-muted-foreground" : "text-[11px]", )} > <span id={id} className={cn("truncate", !compact && "tracking-[0.12em] text-muted-foreground uppercase")}> {label} </span> <span data-slot="usage-meter-value" className={compact ? undefined : "text-foreground"}> {percent}% </span> </div> <Progress value={percent} size="default" aria-labelledby={id} getValueLabel={() => `${percent}% used`} indicatorClassName={toneClass(percent, warnAt, dangerAt)} /> {reset != null && !compact && ( <span data-slot="usage-meter-reset" className="font-mono text-[10px] text-muted-foreground"> {reset} </span> )} </div> ); } /** The bordered "AI allowance" chip: a label and compact meters in a row. */ function UsageMeterGroup({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="usage-meter-group" role="group" className={cn( "flex w-fit max-w-full flex-wrap items-center gap-x-3.5 gap-y-2 rounded-md border px-3 py-1.5", className, )} {...props} /> ); } function UsageMeterGroupLabel({ className, ...props }: React.ComponentProps<"span">) { return ( <span data-slot="usage-meter-group-label" className={cn( "font-mono text-[10px] leading-tight tracking-[0.2em] whitespace-nowrap text-muted-foreground uppercase", className, )} {...props} /> ); } export { UsageMeter, UsageMeterGroup, UsageMeterGroupLabel };Update the import paths to match your project
The source imports
cnfrom@/lib/utilsand uses progress.
Usage#
import { UsageMeter, UsageMeterGroup, UsageMeterGroupLabel } from "@/components/ui/usage-meter";<UsageMeter label="5-hour window" value={32} reset="Resets at 3:40 pm" />
<UsageMeterGroup aria-label="AI allowance">
<UsageMeterGroupLabel>AI allowance</UsageMeterGroupLabel>
<UsageMeter size="compact" label="5-hour" value={32} />
<UsageMeter size="compact" label="Week" value={18} />
</UsageMeterGroup>value is the amount used, out of max (100 by default). The percent is rounded and read out by screen readers as "32% used". The bar turns yellow at warnAt (75) and red at dangerAt (90).
size="compact" is the 88px meter used in top bars; it hides the reset line. UsageMeterGroup wraps compact meters in the bordered allowance chip. Give it an aria-label, since it has role="group".
Examples#
Allowance chip
import { UsageMeter, UsageMeterGroup, UsageMeterGroupLabel } from "@/components/ui/usage-meter";
export default function UsageMeterAllowance() {
return (
<UsageMeterGroup aria-label="AI allowance">
<UsageMeterGroupLabel>AI allowance</UsageMeterGroupLabel>
<UsageMeter size="compact" label="5-hour" value={32} />
<UsageMeter size="compact" label="Week" value={18} />
</UsageMeterGroup>
);
}