Gauge
Read one clamped measurement against a known range, with named threshold bands and an optional native adjustment control.
pnpm dlx shadcn@latest add @haze/gauge---
import { cn } from "../../../lib/haze/utils";
interface Band {max:number;label:string;tone?:"brand"|"success"|"warning"|"destructive"}
interface Props {label?:string;value?:number;min?:number;max?:number;unit?:string;bands?:Band[];adjustable?:boolean;class?:string}
const {label="Storage capacity",value=64,min=0,max=100,unit="%",adjustable=false,bands=[{max:70,label:"Within capacity",tone:"success"},{max:90,label:"Filling up",tone:"warning"},{max:100,label:"Near capacity",tone:"destructive"}],class:className}=Astro.props;
let floor=Number.isFinite(min)?min:0;
let ceiling=Number.isFinite(max)&&max>floor?max:(floor<0?0:floor+1);
if(!Number.isFinite(ceiling)||ceiling<=floor){floor=0;ceiling=100;}
const current=Number.isFinite(value)?Math.max(floor,Math.min(ceiling,value)):floor;
const magnitude=Math.max(Math.abs(floor),Math.abs(ceiling),1);
const ratio=(current/magnitude-floor/magnitude)/(ceiling/magnitude-floor/magnitude);
const ordered=bands.filter(item=>Number.isFinite(item.max)).sort((a,b)=>a.max-b.max),band=ordered.find(item=>current<=item.max)??ordered.at(-1);
const colors={brand:"stroke-brand",success:"stroke-success",warning:"stroke-warning",destructive:"stroke-destructive"};
const text=(n:number)=>new Intl.NumberFormat("en",{maximumFractionDigits:2}).format(n)+unit;
---
<figure data-haze-gauge data-bands={JSON.stringify(ordered)} data-unit={unit} class={cn("grid min-w-0 justify-items-center gap-3 rounded-xl border border-border bg-card p-4.5 text-card-foreground",className)}>
<figcaption class="text-[0.9375rem]/5 font-medium tracking-[-0.01em] wrap-anywhere">{label}</figcaption>
<div class="relative grid w-full max-w-44 place-items-center">
<svg aria-hidden="true" viewBox="0 0 120 120" class="aspect-square w-full -rotate-90"><circle cx="60" cy="60" r="49" fill="none" stroke-width="9" class="stroke-border"/><circle data-gauge-ring cx="60" cy="60" r="49" fill="none" stroke-width="9" pathLength="100" stroke-dasharray="100" stroke-dashoffset={100-ratio*100} class={cn("transition-[stroke-dashoffset] duration-200 ease-snappy",colors[band?.tone??"brand"])}/></svg>
<div aria-hidden="true" class="absolute inset-4 grid content-center justify-items-center gap-1 text-center"><span data-gauge-value class="max-w-full text-2xl/8 font-semibold tracking-tight tabular-nums wrap-anywhere">{text(current)}</span><span data-gauge-band class="text-xs/5 text-muted-foreground wrap-anywhere">{band?.label??"Current value"}</span></div>
</div>
<meter data-gauge-meter min={floor} max={ceiling} value={current} aria-label={label} aria-valuetext={text(current)+(band?", "+band.label:"")} class="sr-only">{text(current)}</meter>
<p class="text-xs/5 text-muted-foreground tabular-nums">{text(floor)} – {text(ceiling)}</p>
{adjustable&&<label hidden data-gauge-control class="grid w-full gap-2 text-xs/5 text-muted-foreground">Set value<input data-gauge-input aria-label={"Set "+label} type="range" min={floor} max={ceiling} step="any" value={current} class="h-10 w-full min-w-0 cursor-pointer appearance-none rounded-md bg-transparent focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring disabled:cursor-not-allowed [&::-webkit-slider-runnable-track]:h-1.5 [&::-webkit-slider-runnable-track]:rounded-full [&::-webkit-slider-runnable-track]:bg-input [&::-webkit-slider-thumb]:-mt-2 [&::-webkit-slider-thumb]:size-5.5 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:border-0 [&::-webkit-slider-thumb]:bg-foreground [&::-webkit-slider-thumb]:shadow-key [&::-webkit-slider-thumb]:transition-[scale] [&::-webkit-slider-thumb]:duration-150 [&::-webkit-slider-thumb]:ease-snappy enabled:hover:[&::-webkit-slider-thumb]:scale-110 enabled:active:[&::-webkit-slider-thumb]:scale-95 [&::-moz-range-track]:h-1.5 [&::-moz-range-track]:rounded-full [&::-moz-range-track]:bg-input [&::-moz-range-thumb]:size-5.5 [&::-moz-range-thumb]:rounded-full [&::-moz-range-thumb]:border-0 [&::-moz-range-thumb]:bg-foreground [&::-moz-range-thumb]:shadow-key [&::-moz-range-thumb]:transition-[scale] [&::-moz-range-thumb]:duration-150 [&::-moz-range-thumb]:ease-snappy enabled:hover:[&::-moz-range-thumb]:scale-110 enabled:active:[&::-moz-range-thumb]:scale-95 forced-colors:appearance-auto"/></label>}
</figure>
<script>
function initGauge(){document.querySelectorAll<HTMLElement>("[data-haze-gauge]:not([data-enhanced])").forEach(root=>{
root.dataset.enhanced="";const input=root.querySelector<HTMLInputElement>("[data-gauge-input]"),meter=root.querySelector<HTMLMeterElement>("[data-gauge-meter]")!,ring=root.querySelector<SVGCircleElement>("[data-gauge-ring]")!;
const bands:{max:number;label:string;tone?:string}[]=JSON.parse(root.dataset.bands||"[]"),tones=["stroke-brand","stroke-success","stroke-warning","stroke-destructive"];root.querySelector("[data-gauge-control]")?.removeAttribute("hidden");
input?.addEventListener("input",()=>{const value=Number(input.value),scale=Math.max(Math.abs(meter.min),Math.abs(meter.max),1),ratio=(value/scale-meter.min/scale)/(meter.max/scale-meter.min/scale),band=bands.find(item=>value<=item.max)??bands.at(-1),text=new Intl.NumberFormat("en",{maximumFractionDigits:2}).format(value)+(root.dataset.unit||"");ring.style.strokeDashoffset=String(100-ratio*100);ring.classList.remove(...tones);ring.classList.add(({brand:"stroke-brand",success:"stroke-success",warning:"stroke-warning",destructive:"stroke-destructive"} as Record<string,string>)[band?.tone||"brand"]);root.querySelector("[data-gauge-value]")!.textContent=text;root.querySelector("[data-gauge-band]")!.textContent=band?.label||"Current value";meter.value=value;meter.setAttribute("aria-valuetext",text+(band?", "+band.label:""));});
});}
initGauge();if(!document.documentElement.hasAttribute("data-haze-gauge-listener")){document.documentElement.setAttribute("data-haze-gauge-listener","");document.addEventListener("astro:page-load",initGauge);}
</script>When to use
- Capacity or health has a meaningful minimum and maximum.
- A named threshold explains when a reading needs attention.
When not to use
- Several independent measurements need comparison. Use bar-chart.
Installation
Add the Haze registry to components.json once, then install Gauge with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/gaugenpx shadcn@latest add @haze/gaugeyarn dlx shadcn@latest add @haze/gaugebunx --bun shadcn@latest add @haze/gaugeFirst 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 |
|---|---|---|---|
| label | string | Storage capacity | Visible caption and accessible name of the native meter. |
| value | number | 64 | Finite reading, clamped to the valid range; nonfinite values use the minimum. |
| min | number | 0 | Lower bound. A nonfinite bound becomes zero. |
| max | number | 100 | Upper bound; invalid ranges receive a positive fallback span. |
| unit | string | % | Suffix for the visible value and accessible reading. |
| bands | { max: number; label: string; tone?: string }[] | – | Upper thresholds sorted ascending; semantic tones are brand, success, warning and destructive. |
| adjustable | boolean | false | Show a native range control after enhancement. |
| class | string | – | Additional classes on the chart figure. |
Keyboard interactions
- Arrow keysHomeEnd
- Adjust the value when the optional range is enabled.
Accessibility
- The native meter exposes its bounds, value and threshold label; the visual ring duplicates that information.
- The optional range changes the value and threshold text together, with ring travel removed by reduced motion.
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 "Gauge" with `npx shadcn@latest add @haze/gauge`. 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 Gauge from "@/components/haze/ui/gauge.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/gauge.md
Customize with real content
Adapt the Haze gauge ui for our product. Replace the demo values of these props with our real content: label, value, min, max, unit, bands, adjustable, class. Keep the layout, the heading level rules and these accessibility details: The native meter exposes its bounds, value and threshold label; the visual ring duplicates that information. The optional range changes the value and threshold text together, with ring travel removed by reduced motion. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using gauge, check it fits: use it when capacity or health has a meaningful minimum and maximum.; a named threshold explains when a reading needs attention.. Avoid it when several independent measurements need comparison. use bar-chart.. Alternatives: @haze/progress, @haze/bar-chart.
Dependencies
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.