Usage meter
A plan's allowances as labelled bars, turning to warning near the limit and to destructive at it, with an upgrade button.
pnpm dlx shadcn@latest add @haze/usage-meter---
import { cn } from "../../../lib/haze/utils";
import Button from "./button.astro";
import Progress from "./progress.astro";
interface Meter {
label: string;
used: number;
/** Leave out for an unlimited allowance. */
limit?: number;
/** Unit after the numbers, such as "runs" or "GB". */
unit?: string;
}
interface Props {
/** Visible title, such as the plan's name. */
title?: string;
/** Line under the title: the billing period or renewal date. */
period?: string;
meters?: Meter[];
/** Share of a limit (0–1) from which a meter shows as nearly used up. */
warnAt?: number;
upgradeLabel?: string;
/** Leave empty to hide the upgrade button. */
upgradeHref?: string;
/** Locale used to format the numbers, such as "bs" or "de". */
locale?: string;
unlimitedLabel?: string;
headingLevel?: 2 | 3 | 4;
class?: string;
}
const {
title = "Team plan",
period = "Renews on 1 December",
meters = [
{ label: "Agent runs", used: 8240, limit: 10000, unit: "runs" },
{ label: "Seats", used: 4, limit: 5, unit: "seats" },
{ label: "Knowledge storage", used: 1.2, limit: 5, unit: "GB" },
{ label: "Connected inboxes", used: 3, unit: "inboxes" },
],
warnAt = 0.8,
upgradeLabel = "Upgrade plan",
upgradeHref = "#upgrade",
locale = "en",
unlimitedLabel = "Unlimited",
headingLevel = 3,
class: className,
} = Astro.props;
const H = `h${headingLevel}` as "h3";
const fmt = new Intl.NumberFormat(locale, { maximumFractionDigits: 1 });
---
<section aria-label={title} class={cn("overflow-hidden rounded-xl border border-border bg-card", className)}>
<div class="flex flex-wrap items-center justify-between gap-3 border-b border-border px-4.5 py-3.5">
<div class="min-w-0">
<H class="text-[0.9375rem] leading-[1.4] font-medium tracking-[-0.01em] text-foreground">{title}</H>
{period && <p class="text-[0.8125rem]/5 text-muted-foreground">{period}</p>}
</div>
{upgradeHref && <Button href={upgradeHref} variant="secondary" size="sm">{upgradeLabel}</Button>}
</div>
<ul role="list" class="flex flex-col gap-4 px-4.5 py-4.5">
{
meters.map((m) => {
const share = m.limit ? m.used / m.limit : 0;
const variant = !m.limit ? "default" : share >= 1 ? "destructive" : share >= warnAt ? "warning" : "default";
const unit = m.unit ? ` ${m.unit}` : "";
return (
<li>
<div class="flex flex-col gap-0.5 text-[0.8125rem]/5 sm:flex-row sm:items-baseline sm:justify-between sm:gap-3">
<span class="font-medium tracking-[-0.01em] text-foreground">{m.label}</span>
<span class={cn("text-xs/5 tabular-nums", variant === "default" ? "text-muted-foreground" : variant === "warning" ? "text-warning" : "text-destructive")}>
{m.limit ? `${fmt.format(m.used)} of ${fmt.format(m.limit)}${unit}` : `${fmt.format(m.used)}${unit} · ${unlimitedLabel}`}
</span>
</div>
{m.limit ? (
<Progress label={m.label} hideLabel value={Math.min(m.used, m.limit)} max={m.limit} variant={variant} size="sm" class="mt-2" />
) : null}
</li>
);
})
}
</ul>
</section>When to use
- A billing or settings page shows how much of each plan limit is used
- Customers should see a limit coming before they hit it
When not to use
- One task is in progress. Use progress.
- The numbers are business results, not allowances. Use stat or bar-list.
Installation
Add the Haze registry to components.json once, then install Usage meter with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/usage-meternpx shadcn@latest add @haze/usage-meteryarn dlx shadcn@latest add @haze/usage-meterbunx --bun shadcn@latest add @haze/usage-meterFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | "Team plan" | Visible title, usually the plan's name. |
| period | string | "Renews on 1 December" | Billing period or renewal date. |
| meters | { label: string; used: number; limit?: number; unit?: string }[] | – | Allowances; leave out limit for unlimited. |
| warnAt | number | 0.8 | Share of a limit from which a meter warns. |
| upgradeLabel | string | "Upgrade plan" | Label of the upgrade button. |
| upgradeHref | string | "#upgrade" | Target of the upgrade button; empty hides it. |
| locale | string | "en" | Locale used to format the numbers. |
| unlimitedLabel | string | "Unlimited" | Word shown for a meter with no limit. |
| headingLevel | 2 | 3 | 4 | 3 | Level of the title. |
| class | string | – | Additional classes on the panel. |
Keyboard interactions
- Tab
- Reach the upgrade button.
Accessibility
- Each bar is a native progress element named after its allowance, and the exact numbers are written next to it.
- Nearly used up and used up change the number's color, but the numbers themselves already say how close the limit is.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Usage meter" with `npx shadcn@latest add @haze/usage-meter`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import UsageMeter from "@/components/haze/ui/usage-meter.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/usage-meter.md
Customize with real content
Adapt the Haze usage-meter ui for our product. Replace the demo values of these props with our real content: title, period, meters, warnAt, upgradeLabel, upgradeHref, locale, unlimitedLabel, headingLevel, class. Keep the layout, the heading level rules and these accessibility details: Each bar is a native progress element named after its allowance, and the exact numbers are written next to it. Nearly used up and used up change the number's color, but the numbers themselves already say how close the limit is. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using usage-meter, check it fits: use it when a billing or settings page shows how much of each plan limit is used; customers should see a limit coming before they hit it. Avoid it when one task is in progress. use progress.; the numbers are business results, not allowances. use stat or bar-list.. Alternatives: @haze/progress, @haze/stat, @haze/bar-list.
Dependencies
- @haze/button
- @haze/progress
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.