Meter for Astro
Astro range meters for capacity, usage, and other bounded measurements.
Installation
bunx --bun shadcn@latest add @gnostic/meternpx shadcn@latest add @gnostic/meterpnpm dlx shadcn@latest add @gnostic/meterComplete the Installation guide first. Then copy the component files below.
No extra packages are required.
Copy the files
src/components/ui/meter/meter-label.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"span">
const { class: className, ...props } = Astro.props
---
<span
data-slot="meter-label"
class={cn("font-medium text-foreground text-sm", className)}
{...props}
>
<slot />
</span>
src/components/ui/meter/meter-track.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="meter-track"
class={cn("block h-2 w-full overflow-hidden bg-input", className)}
{...props}
>
<slot />
</div>
src/components/ui/meter/index.ts
export { default as Meter } from './meter.astro'
export { default as MeterLabel } from './meter-label.astro'
export { default as MeterTrack } from './meter-track.astro'
export { default as MeterIndicator } from './meter-indicator.astro'
export { default as MeterValue } from './meter-value.astro'
src/components/ui/meter/meter.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import Track from "./meter-track.astro"
import Indicator from "./meter-indicator.astro"
type Props = HTMLAttributes<"div"> & {
value: number
min?: number
max?: number
}
const {
class: className,
value,
min = 0,
max = 100,
style,
...props
} = Astro.props
if (
!Number.isFinite(min) ||
!Number.isFinite(max) ||
max <= min ||
(value !== null && !Number.isFinite(value))
)
throw new RangeError(
"Meter needs finite bounds with max greater than min, and a finite value.",
)
const current = value === null ? null : Math.min(max, Math.max(min, value))
const percent = current === null ? 0 : ((current - min) / (max - min)) * 100
const mergedStyle =
typeof style === "string"
? `${style};--meter-percentage:${percent}%`
: { ...style, "--meter-percentage": percent + "%" }
---
<div
data-slot="meter"
role="meter"
aria-valuemin={min}
aria-valuemax={max}
aria-valuenow={current ?? undefined}
style={mergedStyle}
class={cn("flex w-full flex-col gap-2", className)}
{...props}
>
<slot>
<Track>
<Indicator />
</Track>
</slot>
</div>
src/components/ui/meter/meter-value.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"span">
const { class: className, ...props } = Astro.props
---
<span
data-slot="meter-value"
class={cn("text-foreground text-sm tabular-nums", className)}
{...props}
>
<slot />
</span>
src/components/ui/meter/meter-indicator.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, ...props } = Astro.props
---
<div
data-slot="meter-indicator"
class={cn(
"h-full w-(--meter-percentage) bg-primary transition-[width] duration-500",
className,
)}
{...props}
></div>
Usage
import { Meter } from "@/components/ui/meter"
Use the Code tab above for the complete Astro composition.
API reference
Read the registry source for the exported parts and prop types.
When to use Meter
Pass a finite value and bounds to Meter. Use a meter for a measurement such as storage used, not a task that is still completing. The root can render its default track or accept the exported parts through its slot.
Keyboard and behavior
Give the meter an accessible name with aria-label or aria-labelledby. Min and max must define a valid range. Label and Value parts display your text; they do not generate labels or formatted percentages automatically.
Theme and layout
The fill width comes from the clamped value and shared primary color. A custom status color still needs explanatory text. This is a server-rendered measurement; application scripts must update both the ARIA value and CSS percentage for live data.