Skip to content
Atoms

Meter

How much of a limit is used, split into parts.

Installation

  1. Run the CLI

    Run pnpm dlx asta-ui@latest init once, then this. The file lands in components/astabyte/ and the colour tokens go into your CSS.

    $ pnpm dlx asta-ui@latest add meter

    Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/meter.json

  2. Or install the package

    Don’t want the file? Import from asta-ui/meter and update with pnpm update asta-ui. Add the stylesheet once. More.

    $ pnpm add asta-ui
    import { Meter } from "asta-ui/meter";
  3. Or copy it by hand

    Copy this file. Keep the paths, or fix the @/ imports. Then add the tokens from globals.css.

    "use client";
    
    import type { ReactNode } from "react";
    
    const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" ");
    
    const KEYFRAMES = `@keyframes asta-meter-in { from { transform: scaleX(0) } }
    @keyframes asta-meter-fade { from { opacity: 0; transform: translateY(2px) } }
    @media (prefers-reduced-motion: reduce) { .asta-meter-a { animation: none !important; transition: none !important } }`;
    
    // Segment fills, strongest first: brand, then softer brand, then greys. Tokens only.
    const FILLS = [
      "var(--color-brand)",
      "color-mix(in srgb, var(--color-brand) 55%, var(--color-paper))",
      "color-mix(in srgb, var(--color-ink) 62%, var(--color-paper))",
      "color-mix(in srgb, var(--color-ink) 28%, var(--color-paper))",
      "color-mix(in srgb, var(--color-ink) 14%, var(--color-paper))",
    ];
    
    export interface MeterSegment {
      label: string;
      value: number;
    }
    
    export interface MeterProps {
      label: string;
      /** Total used. Ignored when `segments` is given (their sum is used). */
      value?: number;
      max: number;
      /** Split the bar into parts, e.g. photos, video and documents. */
      segments?: MeterSegment[];
      /** Formats every number shown. Defaults to plain grouping. */
      format?: (n: number) => string;
      /** From this share of max (0–1) the bar turns ink and a note shows. Default 0.9. */
      warnAt?: number;
      /** Note shown past `warnAt`. */
      warning?: ReactNode;
      /** Something on the right of the label row, e.g. an Upgrade link. */
      action?: ReactNode;
      size?: "sm" | "md";
      /** Show the legend under the bar (segments only). Default true. */
      legend?: boolean;
      className?: string;
    }
    
    /**
     * How much of a limit is used: storage, seats, API calls. One bar, optionally split into parts with a legend.
     * The bar sweeps in from the left on first paint and its parts glide when the values change. Past the warning
     * level it turns ink and says so, in words rather than red.
     */
    export function Meter({ label, value = 0, max, segments, format = (n) => n.toLocaleString("en-US"), warnAt = 0.9, warning = "Almost at your limit", action, size = "md", legend = true, className }: MeterProps) {
      const parts = segments?.length ? segments : [{ label, value }];
      const used = parts.reduce((a, p) => a + Math.max(0, p.value), 0);
      const share = max > 0 ? Math.min(1, used / max) : 0;
      const warn = share >= warnAt;
      // Where each part starts, as a share of max.
      const starts = parts.map((_, i) => parts.slice(0, i).reduce((a, p) => a + Math.max(0, p.value), 0) / (max || 1));
    
      return (
        <div className={cx("flex w-full flex-col", size === "sm" ? "gap-2" : "gap-2.5", className)}>
          <style href="asta-meter" precedence="asta">{KEYFRAMES}</style>
          <div className="flex items-baseline justify-between gap-3">
            <p className={cx("font-medium text-ink", size === "sm" ? "text-[13px]" : "text-[14px]")}>{label}</p>
            {action}
          </div>
          <div
            role="meter"
            aria-label={label}
            aria-valuemin={0}
            aria-valuemax={max}
            aria-valuenow={Math.min(used, max)}
            aria-valuetext={`${format(used)} of ${format(max)}${warn ? `. ${typeof warning === "string" ? warning : ""}` : ""}`}
            className={cx("relative w-full overflow-hidden rounded-full bg-paper-2 shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_6%,transparent)]", size === "sm" ? "h-1.5" : "h-2.5")}
          >
            <div className="asta-meter-a absolute inset-0 origin-left animate-[asta-meter-in_1s_cubic-bezier(.16,1,.3,1)_both]">
              {parts.map((p, i) => {
                const w = max > 0 ? Math.max(0, p.value) / max : 0;
                const x = starts[i];
                return (
                  <span
                    key={p.label}
                    className="asta-meter-a absolute inset-y-0 left-0 w-full origin-left transition-[transform,background-color] duration-700 ease-[cubic-bezier(.16,1,.3,1)]"
                    style={{ transform: `translateX(${Math.min(1, x) * 100}%) scaleX(${Math.min(w, Math.max(0, 1 - x))})`, background: warn && parts.length === 1 ? "var(--color-ink)" : FILLS[i % FILLS.length] }}
                  />
                );
              })}
            </div>
          </div>
          <div className={cx("flex flex-wrap items-center justify-between gap-x-4 gap-y-1", size === "sm" ? "text-[12px]" : "text-[12.5px]")}>
            <p className="tabular-nums text-ink-2">
              <span className="font-medium text-ink">{format(used)}</span>
              <span className="text-mute"> of {format(max)}</span>
            </p>
            {warn ? (
              <p key="warn" className="asta-meter-a flex animate-[asta-meter-fade_.4s_cubic-bezier(.16,1,.3,1)_both] items-center gap-1.5 font-medium text-ink">
                <svg viewBox="0 0 24 24" className="h-3.5 w-3.5" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round" aria-hidden>
                  <path d="M10.3 4.6 3 17.5A2 2 0 0 0 4.7 20.5h14.6a2 2 0 0 0 1.7-3L13.7 4.6a2 2 0 0 0-3.4 0Z" />
                  <path d="M12 9.5v4" />
                  <path d="M12 17h.01" />
                </svg>
                {warning}
              </p>
            ) : (
              <p className="tabular-nums text-mute">{Math.round(share * 100)}% used</p>
            )}
          </div>
          {legend && segments && segments.length > 1 && (
            <ul className="flex flex-wrap gap-x-4 gap-y-1.5 pt-0.5">
              {segments.map((p, i) => (
                <li key={p.label} className="flex items-center gap-1.5 text-[12px] text-mute">
                  <span aria-hidden className="h-2 w-2 rounded-full" style={{ background: FILLS[i % FILLS.length] }} />
                  <span className="text-ink-2">{p.label}</span>
                  <span className="tabular-nums">{format(p.value)}</span>
                </li>
              ))}
            </ul>
          )}
        </div>
      );
    }
    
    export default Meter;
    
  4. Or ask your AI agent

    Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.

    Add the Astabyte UI Meter component to my project.
    
    How much of a limit is used, split into parts.
    It includes: segments, legend, warnAt, format, action, sm, md.
    
    Install it:
    ```sh
    pnpm dlx asta-ui@latest add meter --radix
    ```
    Or with the shadcn CLI:
    ```sh
    pnpm dlx shadcn@latest add https://ui.astabyte.com/r/meter.json
    ```
    If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4).
    
    Import it like this:
    ```tsx
    import { Button } from "@/components/astabyte/button";
    import { Meter } from "@/components/astabyte/meter";
    ```
    
    Usage example and props: https://ui.astabyte.com/meter
    Full catalogue for context: https://ui.astabyte.com/llms.txt
    
    Rules:
    - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS.
    - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback.
    - Keep keyboard, focus and ARIA behaviour.
    - Use fake data and fake brand names in examples.
    
    Use it to: [describe where it goes and what it should do]
  5. Or use plain HTML + CSS

    Not on React? Use it from Django, Rails, Laravel, Go html/template, htmx or a static page. Load asta.css and asta.js once (the head.html tab), then paste the markup. The ui.astabyte.com/cdn link is temporary: pin a version from jsDelivr for production. More in HTML + CSS.

    <!-- Static: set --value (0 to 1) on each fill, and --start for the parts of a split bar. Add data-warn near the limit. -->
    <div style="display:grid;gap:24px;max-width:420px">
    <div class="asta-meter" role="meter" aria-label="Storage" aria-valuenow="74" aria-valuemin="0" aria-valuemax="100">
    <div class="asta-meter-head"><span class="asta-meter-label">Storage</span><a class="asta-meter-action" href="#">Upgrade</a></div>
    <div class="asta-meter-track"><span class="asta-meter-fill" style="--value:.42"></span><span class="asta-meter-fill asta-meter-fill-2" style="--start:.42;--value:.2"></span><span class="asta-meter-fill asta-meter-fill-3" style="--start:.62;--value:.12"></span></div>
    <div class="asta-meter-foot"><span><b>74 GB</b> of 100 GB</span><span>74% used</span></div>
    <div class="asta-meter-legend"><span><span class="asta-meter-legend-dot"></span>Media 42 GB</span><span><span class="asta-meter-legend-dot" style="background:var(--ink)"></span>Docs 20 GB</span><span><span class="asta-meter-legend-dot" style="background:color-mix(in srgb,var(--brand) 40%,var(--paper))"></span>Backups 12 GB</span></div>
    </div>
    <div class="asta-meter" role="meter" aria-label="API requests" aria-valuenow="9420" aria-valuemin="0" aria-valuemax="10000" data-warn>
    <div class="asta-meter-head"><span class="asta-meter-label">API requests</span></div>
    <div class="asta-meter-track"><span class="asta-meter-fill" style="--value:.942"></span></div>
    <div class="asta-meter-foot"><span><b>9,420</b> of 10,000</span><span>Almost at your limit</span></div>
    </div>
    <div class="asta-meter asta-meter-sm" role="meter" aria-label="Seats" aria-valuenow="8" aria-valuemin="0" aria-valuemax="10">
    <div class="asta-meter-head"><span class="asta-meter-label">Seats</span></div>
    <div class="asta-meter-track"><span class="asta-meter-fill" style="--value:.8"></span></div>
    <div class="asta-meter-foot"><span><b>8</b> of 10</span><span>80% used</span></div>
    </div>
    </div>

Usage

The demo above, as code.

"use client";

import { useState } from "react";
import { Button } from "@/components/astabyte/button";
import { Meter } from "@/components/astabyte/meter";

const gb = (n: number) => `${n.toFixed(1)} GB`;

const FULL = [
  { label: "Photos", value: 38.4 },
  { label: "Videos", value: 27.9 },
  { label: "Documents", value: 9.6 },
  { label: "Other", value: 4.1 },
];

export default function MeterDemo() {
  const [cleaned, setCleaned] = useState(false);
  const [calls, setCalls] = useState(8420);
  const storage = cleaned ? FULL.map((s) => (s.label === "Videos" ? { ...s, value: 11.2 } : s.label === "Other" ? { ...s, value: 0.8 } : s)) : FULL;

  return (
    <div className="flex min-h-[460px] w-full items-center justify-center p-6 sm:p-10">
      <div className="flex w-full max-w-[520px] flex-col gap-8">
        <Meter
          label="Storage"
          max={100}
          segments={storage}
          format={gb}
          warnAt={0.85}
          warning="Running low on space"
          action={
            <Button size="xs" variant="ghost" onClick={() => setCleaned((c) => !c)}>
              {cleaned ? "Undo clean-up" : "Clean up"}
            </Button>
          }
        />
        <Meter
          label="API requests this month"
          value={calls}
          max={10_000}
          action={
            <Button size="xs" variant="ghost" onClick={() => setCalls((c) => (c >= 9800 ? 6200 : c + 700))}>
              {calls >= 9800 ? "Reset" : "Send 700"}
            </Button>
          }
        />
        <div className="grid gap-6 sm:grid-cols-2">
          <Meter label="Seats" value={7} max={10} size="sm" format={(n) => String(n)} />
          <Meter label="Projects" value={3} max={5} size="sm" format={(n) => String(n)} />
        </div>
      </div>
    </div>
  );
}