# Meter

> A bar that shows a measured value within a known range.

```tsx title="hero.tsx"
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from '@langgenius/dify-ui/meter'

export function MeterHeroDemo() {
  return (
    <Meter
      value={1240}
      max={5000}
      locale="en-US"
      format={{ maximumFractionDigits: 0 }}
      getAriaValueText={(formattedValue) => `${formattedValue} of 5,000 message credits`}
      className="flex w-full max-w-80 flex-col gap-2"
    >
      <div className="flex items-center justify-between gap-2">
        <MeterLabel className="truncate">Message credits</MeterLabel>
        <MeterValue className="shrink-0">
          {(formattedValue) => `${formattedValue} / 5,000`}
        </MeterValue>
      </div>
      <MeterTrack>
        <MeterIndicator />
      </MeterTrack>
    </Meter>
  )
}
```

## Usage guidelines

- **Use Meter for a measurement, not a task**: A meter shows where a value sits in a known range, such as quota usage, storage or a score, and that value can rise or fall at any time. A value that only moves toward done, such as an upload or an indexing run, uses `Progress`. Both draw the same bar, but assistive technology announces them as `meter` and `progressbar`, so choose by meaning, not by look.
- **Name the meter**: Use `MeterLabel` for a visible label; it names the root. Without one, give `Meter` an `aria-label`.
- **Set the real range**: Pass `min` and `max` in the unit being measured instead of converting to a percentage. The value text defaults to a percentage of the range; pass `format` to show the number itself, and `getAriaValueText` when the number alone does not say what is counted.
- **Choose the tone from the value**: `MeterIndicator` takes `tone="warning"` or `tone="error"`. The caller decides the thresholds, and the visible text still states the value.

## Anatomy

```tsx
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from '@langgenius/dify-ui/meter'
```

```tsx
<Meter>
  <MeterLabel />
  <MeterValue />
  <MeterTrack>
    <MeterIndicator />
  </MeterTrack>
</Meter>
```

`Meter` renders no layout of its own, so the caller places the label and value around the track.

## Examples

### Tones

`neutral` (the default), `warning` and `error` color the indicator. This example switches at 80% and at the limit.

```tsx title="tones.tsx"
import type { MeterTone } from '@langgenius/dify-ui/meter'
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from '@langgenius/dify-ui/meter'

const quotas = [
  { label: 'Apps', used: 4, limit: 10 },
  { label: 'Documents', used: 43, limit: 50 },
  { label: 'Team members', used: 5, limit: 5 },
]

function toneFor(used: number, limit: number): MeterTone {
  if (used >= limit) return 'error'
  if (used / limit >= 0.8) return 'warning'
  return 'neutral'
}

export function MeterTonesDemo() {
  return (
    <div className="flex w-full max-w-80 flex-col gap-4">
      {quotas.map((quota) => (
        <Meter
          key={quota.label}
          value={quota.used}
          max={quota.limit}
          locale="en-US"
          format={{ maximumFractionDigits: 0 }}
          getAriaValueText={(formattedValue) => `${formattedValue} of ${quota.limit}`}
          className="flex flex-col gap-2"
        >
          <div className="flex items-center justify-between gap-2">
            <MeterLabel className="truncate">{quota.label}</MeterLabel>
            <MeterValue className="shrink-0">
              {(formattedValue) => `${formattedValue} / ${quota.limit}`}
            </MeterValue>
          </div>
          <MeterTrack>
            <MeterIndicator tone={toneFor(quota.used, quota.limit)} />
          </MeterTrack>
        </Meter>
      ))}
    </div>
  )
}
```

### Formatted value

`format` takes `Intl.NumberFormat` options for both `MeterValue` and `aria-valuetext`. Without it, `MeterValue` displays a percentage of the range (`84%` here); passing `style: 'unit'` formats the measured value directly, and `locale` keeps the unit and decimal separator consistent across browsers.

```tsx title="format.tsx"
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from '@langgenius/dify-ui/meter'

export function MeterFormatDemo() {
  return (
    <Meter
      value={4.2}
      max={5}
      locale="en-US"
      format={{ style: 'unit', unit: 'gigabyte', maximumFractionDigits: 1 }}
      getAriaValueText={(formattedValue) => `${formattedValue} of 5 GB`}
      className="flex w-full max-w-80 flex-col gap-2"
    >
      <div className="flex items-center justify-between gap-2">
        <MeterLabel className="truncate">Vector storage (5 GB)</MeterLabel>
        <MeterValue className="shrink-0" />
      </div>
      <MeterTrack>
        <MeterIndicator />
      </MeterTrack>
    </Meter>
  )
}
```

## API reference

### Meter

Displays a numeric value within a known range, such as quota usage, capacity, or a score. Renders a `<div>` element.

Use `Progress` instead for task completion; `role="meter"` and `role="progressbar"` are not interchangeable.

Inherits `aria-valuetext`, `format`, `getAriaValueText`, `locale`, `max`, `min`, `value`, `className`, `render`, `style` from [Base UI Meter.Root](https://base-ui.com/react/components/meter#root).

### MeterIndicator

Visualizes the position of the value along the range. Renders a `<div>` element.

**Props**

| Prop    | Type                                | Default     | Description                                                               |
| ------- | ----------------------------------- | ----------- | ------------------------------------------------------------------------- |
| `tone?` | `'error' \| 'neutral' \| 'warning'` | `'neutral'` | The color of the indicator, conveying how critical the measured value is. |

Inherits `className`, `render`, `style` from [Base UI Meter.Indicator](https://base-ui.com/react/components/meter#indicator).

### MeterLabel

An accessible label for the meter. Renders a `<span>` element.

Inherits `className`, `render`, `style` from [Base UI Meter.Label](https://base-ui.com/react/components/meter#label).

### MeterTrack

Contains the meter indicator and represents the entire range of the meter. Renders a `<div>` element.

Inherits `className`, `render`, `style` from [Base UI Meter.Track](https://base-ui.com/react/components/meter#track).

### MeterValue

A text element displaying the current value. Renders a `<span>` element.

Inherits `children`, `className`, `render`, `style` from [Base UI Meter.Value](https://base-ui.com/react/components/meter#value).
