# Meter
A reading within a known range, such as storage used.
Source page: https://dinachi.dev/docs/components/meter

## Installation
```bash
npx @dinachi/cli@latest add meter
```
Adds to your project:
- `components/ui/meter/meter.tsx`
- `components/ui/meter/index.ts`
Installs:
- `@base-ui/react` ^1.9.0

## Usage
```tsx
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@/components/ui/meter"

export function StorageMeter() {
  return (
    <Meter value={72}>
      <div className="flex items-center justify-between">
        <MeterLabel>Storage used</MeterLabel>
        <MeterValue />
      </div>
      <MeterTrack>
        <MeterIndicator />
      </MeterTrack>
    </Meter>
  )
}
```

## Anatomy
```tsx
<Meter>
  <MeterLabel />
  <MeterValue />
  <MeterTrack>
    <MeterIndicator />
  </MeterTrack>
</Meter>
```
- `Meter`: Holds value and range; lays parts out in a grid.
- `MeterLabel`: The visible label; names the meter.
- `MeterValue`: Shows the formatted value.
- `MeterTrack`: The full-width bar behind the indicator.
- `MeterIndicator`: The filled part; width follows the value.

## Examples

### With label
A label and the formatted value above the track.
```tsx
import { Meter, MeterTrack, MeterIndicator, MeterLabel, MeterValue } from '@/components/ui/meter';

export function Example() {
  return (
    <Meter value={72} className="w-full max-w-sm">
      <div className="flex items-center justify-between">
        <MeterLabel>Storage Used</MeterLabel>
        <MeterValue />
      </div>
      <MeterTrack>
        <MeterIndicator />
      </MeterTrack>
    </Meter>
  );
}
```

### Default
A track filled to the value.
```tsx
import { Meter, MeterTrack, MeterIndicator } from '@/components/ui/meter';

export function Example() {
  return (
    <Meter value={40} className="w-full max-w-sm">
      <MeterTrack>
        <MeterIndicator />
      </MeterTrack>
    </Meter>
  );
}
```

### Levels
Indicator colours for low, middle and high readings.
```tsx
import { Meter, MeterTrack, MeterIndicator, MeterLabel } from '@/components/ui/meter';

export function Example() {
  return (
    <div className="w-full max-w-sm space-y-4">
      <Meter value={25} className="w-full">
        <div className="flex items-center justify-between">
          <MeterLabel>CPU Usage</MeterLabel>
          <span className="text-xs text-muted-foreground">25%</span>
        </div>
        <MeterTrack>
          <MeterIndicator className="bg-green-500" />
        </MeterTrack>
      </Meter>
      <Meter value={65} className="w-full">
        <div className="flex items-center justify-between">
          <MeterLabel>Memory</MeterLabel>
          <span className="text-xs text-muted-foreground">65%</span>
        </div>
        <MeterTrack>
          <MeterIndicator className="bg-yellow-500" />
        </MeterTrack>
      </Meter>
      <Meter value={90} className="w-full">
        <div className="flex items-center justify-between">
          <MeterLabel>Disk Space</MeterLabel>
          <span className="text-xs text-muted-foreground">90%</span>
        </div>
        <MeterTrack>
          <MeterIndicator className="bg-red-500" />
        </MeterTrack>
      </Meter>
    </div>
  );
}
```

## Notes

### Meter or Progress
Meter shows a measurement anywhere in its range, such as disk space. For a task moving towards done, use [Progress](/docs/components/progress).

### Agent
Meter does not register with the [agent bridge](/docs/components/agent). The bridge reads it from the accessibility tree as `meter`, with `value`, `min` and `max`, and no actions.

```ts
agent.snapshot()
// [{ component: "meter", label: "Storage used", value: 72, min: 0, max: 100, actions: [] }]
```

## API reference

### Meter
Holds value and range; lays parts out in a grid.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` **agent** | `number` | - | The current reading. Clamped to `min` and `max`. |
| `min` **agent** | `number` | `0` | The low end of the range. |
| `max` **agent** | `number` | `100` | The high end of the range. |
| `format` | `Intl.NumberFormatOptions` | - | Formats the value. Defaults to a percent of the range. |
| `locale` | `Intl.LocalesArgument` | - | Locale for formatting the value. |
| `getAriaValueText` | `(formattedValue, value) => string` | - | Text screen readers announce for the value. |

### MeterLabel
The visible label; names the meter.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` **agent** | `ReactNode` | - | Also the name the agent reads. |

### MeterValue
Shows the formatted value.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `(formattedValue, value) => ReactNode` | - | Renders the value yourself. Defaults to the formatted value. |

### MeterTrack
The full-width bar behind the indicator.

Takes the props of the element it renders.

### MeterIndicator
The filled part; width follows the value.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` | - | Recolour it, for example by level. |

## Accessibility
- The root has the meter role with `aria-valuenow`, `aria-valuemin` and `aria-valuemax`.
- `MeterLabel` names it via `aria-labelledby`; otherwise give the root an `aria-label`.
- `aria-valuetext` is the formatted value, or what `getAriaValueText` returns.
- Reduced motion turns off the indicator transition.

## Source
`components/ui/meter/meter.tsx`
```tsx
"use client"

import * as React from "react"
import { Meter as MeterPrimitive } from "@base-ui/react/meter"
import { cn } from "@/lib/utils"

const Meter = React.forwardRef<
  React.ComponentRef<typeof MeterPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof MeterPrimitive.Root>
>(({ className, ...props }, ref) => (
  <MeterPrimitive.Root data-slot="meter" ref={ref} className={cn("grid gap-2", className)} {...props} />
))
Meter.displayName = "Meter"

const MeterTrack = React.forwardRef<
  React.ComponentRef<typeof MeterPrimitive.Track>,
  React.ComponentPropsWithoutRef<typeof MeterPrimitive.Track>
>(({ className, ...props }, ref) => (
  <MeterPrimitive.Track
    data-slot="meter-track"
    ref={ref}
    className={cn("relative h-2 w-full overflow-hidden rounded-full bg-secondary", className)}
    {...props}
  />
))
MeterTrack.displayName = "MeterTrack"

const MeterIndicator = React.forwardRef<
  React.ComponentRef<typeof MeterPrimitive.Indicator>,
  React.ComponentPropsWithoutRef<typeof MeterPrimitive.Indicator>
>(({ className, ...props }, ref) => (
  <MeterPrimitive.Indicator
    data-slot="meter-indicator"
    ref={ref}
    className={cn("h-full rounded-full bg-primary transition-[width] duration-[var(--motion-duration-base,200ms)] ease-[var(--motion-ease-out,cubic-bezier(0.23,1,0.32,1))] motion-reduce:transition-none", className)}
    {...props}
  />
))
MeterIndicator.displayName = "MeterIndicator"

const MeterLabel = React.forwardRef<
  React.ComponentRef<typeof MeterPrimitive.Label>,
  React.ComponentPropsWithoutRef<typeof MeterPrimitive.Label>
>(({ className, ...props }, ref) => (
  <MeterPrimitive.Label
    data-slot="meter-label"
    ref={ref}
    className={cn("text-sm font-medium leading-none", className)}
    {...props}
  />
))
MeterLabel.displayName = "MeterLabel"

const MeterValue = React.forwardRef<
  React.ComponentRef<typeof MeterPrimitive.Value>,
  React.ComponentPropsWithoutRef<typeof MeterPrimitive.Value>
>(({ className, ...props }, ref) => (
  <MeterPrimitive.Value
    data-slot="meter-value"
    ref={ref}
    className={cn("text-xs text-muted-foreground", className)}
    {...props}
  />
))
MeterValue.displayName = "MeterValue"

export { Meter, MeterTrack, MeterIndicator, MeterLabel, MeterValue }
```
