---
title: Meter for Astro
description: "Astro range meters for capacity, usage, and other bounded measurements."
seo:
    title: Meter for Astro | Shadcn & Tailwind CSS
related:
    - /components/progress
    - /components/badge
sidebar:
    label: "Meter"
---

```astro
---
import {
  Meter,
  MeterLabel,
  MeterValue,
  MeterTrack,
  MeterIndicator,
} from "@/components/ui/meter"
---

<div class="grid w-full max-w-sm gap-6">
  <Meter value={7} max={10} aria-labelledby="storage-label">
    <div class="flex justify-between">
      <MeterLabel id="storage-label">Storage used</MeterLabel>
      <MeterValue>7 of 10 GB</MeterValue>
    </div>
    <MeterTrack>
      <MeterIndicator />
    </MeterTrack>
  </Meter>
  <Meter value={0} aria-label="Empty archive capacity" />
  <Meter value={100} aria-label="Full archive capacity" />
</div>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/meter
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/meter/meter-label.astro

```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

```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

```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

```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

```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

```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>
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Usage

```ts
import { Meter } from "@/components/ui/meter"
```

Use the Code tab above for the complete Astro composition.

## API reference

Read the [registry source](/ui/r/meter.json) 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.
