# Meter

Show a measurement within a known range, such as storage used or an account quota.

Use a Meter for a value within a known range, such as storage used, a battery level or an account quota. It fills a track to show the value, and screen readers announce the same value.

```angular-ts title="basic.ts"
import { Component } from "@angular/core"
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/angular/meter"

@Component({
  imports: [Meter, MeterIndicator, MeterLabel, MeterTrack, MeterValue],
  template: `
    <div uiMeter [value]="68">
      <span uiMeterLabel>Storage used</span>
      <span uiMeterValue></span>
      <div uiMeterTrack>
        <div uiMeterIndicator></div>
      </div>
    </div>
  `,
})
class Example {}

export { Example }
```

Read the storage meter. The label names it, and both the visible value and screen reader value report 68%.

## Installation

```bash
npm install @ui-sdk/angular @ui-sdk/core
```

## Composition

Use the following composition to build Meter:

```text
uiMeter
├── uiMeterLabel
├── uiMeterValue
└── uiMeterTrack
    └── uiMeterIndicator
```

- `uiMeter` holds the value and range.
- `uiMeterLabel` gives it a visible name. You can use `aria-label` on the root instead.
- `uiMeterValue` displays the formatted value.
- `uiMeterTrack` gives the bar its size, and `uiMeterIndicator` fills it.

The value is already part of the root's accessible description. The visible value is hidden from screen readers to avoid reading it twice.

## Usage

Import the parts and put them together:

```ts
import { Component } from "@angular/core"
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/angular/meter"

@Component({
  selector: "app-example",
  imports: [Meter, MeterIndicator, MeterLabel, MeterTrack, MeterValue],
  template: `
    <div uiMeter [value]="40">
      <span uiMeterLabel></span>
      <span uiMeterValue></span>
      <div uiMeterTrack>
        <div uiMeterIndicator></div>
      </div>
    </div>
  `,
})
class Example {}

export { Example }
```

## Build Your Own

`@ui-sdk/angular/meter` comes with a ready-made Meter. If you'd like to build your own, with your own names, start from the `meter` primitive in `@ui-sdk/core`:

```ts title="quota.ts"
import { Directive, inject, input, numberAttribute } from "@angular/core"
import { meter } from "@ui-sdk/core/meter"
import { usePart, usePrimitive } from "@ui-sdk/angular/primitive"

@Directive({ selector: "[appQuota]" })
class Quota {
  readonly value = input.required({ transform: numberAttribute })
  readonly handle = usePrimitive(meter, () => ({ value: this.value() }))
  readonly state = usePart(this.handle, "root").state
}

@Directive({ selector: "[appQuotaLabel]" })
class QuotaLabel {
  readonly state = usePart(inject(Quota).handle, "label").state
}

@Directive({ selector: "[appQuotaValue]", exportAs: "appQuotaValue" })
class QuotaValue {
  readonly state = usePart(inject(Quota).handle, "value").state
}

@Directive({ selector: "[appQuotaTrack]" })
class QuotaTrack {
  readonly state = usePart(inject(Quota).handle, "track").state
}

@Directive({ selector: "[appQuotaFill]" })
class QuotaFill {
  readonly state = usePart(inject(Quota).handle, "indicator").state
}

export { Quota, QuotaFill, QuotaLabel, QuotaTrack, QuotaValue }
```

```ts
import { Component } from "@angular/core"
import { Quota, QuotaFill, QuotaLabel, QuotaTrack, QuotaValue } from "./quota"

@Component({
  selector: "app-quota-example",
  imports: [Quota, QuotaFill, QuotaLabel, QuotaTrack, QuotaValue],
  template: `
    <div appQuota [value]="68">
      <span appQuotaLabel>Storage</span>
      <span appQuotaValue #quotaValue="appQuotaValue">{{
        quotaValue.state().text
      }}</span>
      <div appQuotaTrack>
        <div appQuotaFill></div>
      </div>
    </div>
  `,
})
export class QuotaExample {}
```

`uiMeterValue` fills an empty element with the formatted value. A directive of your own leaves its content to you, so show `state().text` in it, as above.

Connect the other slots the same way, each with `usePart`. Each part keeps the behavior, state and data attributes of its ready-made counterpart: the root is the `meter`, and the indicator sets its width from the value. See [Rendering Primitives](/docs/angular/rendering) and [Custom Parts](/docs/angular/custom-parts) for more.

## Bounds

`min` and `max` set the range. They default to 0 and 100. A value outside the range is clamped for the fill, the percentage and `aria-valuenow`, but the state's `value` and a format other than percent keep it as you passed it. Here, 720 sits about 76% of the way from 300 to 850. Decimal formatting keeps the visible value as 720.

```angular-ts title="min-max.ts"
import { Component } from "@angular/core"
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/angular/meter"

@Component({
  imports: [Meter, MeterIndicator, MeterLabel, MeterTrack, MeterValue],
  template: `
    <div
      uiMeter
      [value]="720"
      [min]="300"
      [max]="850"
      [format]="{ style: 'decimal' }"
    >
      <span uiMeterLabel>Credit score</span>
      <span uiMeterValue></span>
      <div uiMeterTrack>
        <div uiMeterIndicator></div>
      </div>
    </div>
  `,
})
class Example {}

export { Example }
```

Compare the score with the filled track. The number shows 720; the fill shows its position between 300 and 850.

## Format and Locale

Pass `Intl.NumberFormat` options to `format` for units, currency, or decimal places. A function can return custom text, such as the number of occupied seats.

```angular-ts title="format.ts"
import { Component } from "@angular/core"
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/angular/meter"

@Component({
  imports: [Meter, MeterIndicator, MeterLabel, MeterTrack, MeterValue],
  template: `
    <div data-slot="stack">
      @for (item of meters; track item.label) {
        <div
          uiMeter
          [value]="item.value"
          [max]="item.max"
          [format]="item.format"
          [locale]="item.locale"
        >
          <span uiMeterLabel>{{ item.label }}</span>
          <span uiMeterValue></span>
          <div uiMeterTrack>
            <div uiMeterIndicator></div>
          </div>
        </div>
      }
    </div>
  `,
})
class Example {
  readonly meters = [
    {
      label: "Storage",
      value: 120,
      max: 250,
      format: { style: "unit", unit: "gigabyte" },
    },
    {
      label: "Budget",
      value: 1840,
      max: 2500,
      format: { style: "currency", currency: "USD", maximumFractionDigits: 0 },
    },
    {
      label: "Seats",
      value: 18,
      max: 25,
      format: (seats: number) => `${seats} of 25 seats`,
    },
    { label: "Auslastung", value: 40, locale: "de-DE" },
  ] satisfies {
    label: string
    value: number
    max?: number
    format?: Intl.NumberFormatOptions | ((value: number) => string)
    locale?: string
  }[]
}

export { Example }
```

Compare the readings. Each meter formats its own value as a unit, currency, custom text, or localized percentage.

A percent format reports the value's position between `min` and `max`. Other formats use the value itself.

`locale` controls digits and separators. If you omit it, the component reads the page's language after it mounts, with the browser's language as a fallback. The initial server and client render use English.

Set `dir="rtl"` on the surrounding content to fill the track from the right.

## Value Label

Use `getValueLabel` when a screen reader needs more context than the visible number. It receives the value and details containing `formatted`, `percent`, `min`, and `max`.

```angular-ts title="value-label.ts"
import { Component } from "@angular/core"
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
  type MeterOptions,
} from "@ui-sdk/angular/meter"

@Component({
  imports: [Meter, MeterIndicator, MeterLabel, MeterTrack, MeterValue],
  template: `
    <div uiMeter [value]="750" [max]="1000" [getValueLabel]="getValueLabel">
      <span uiMeterLabel>API requests</span>
      <span uiMeterValue>750 / 1,000</span>
      <div uiMeterTrack>
        <div uiMeterIndicator></div>
      </div>
    </div>
  `,
})
class Example {
  readonly getValueLabel: MeterOptions["getValueLabel"] = (value, details) =>
    `${value} of ${details.max} requests, ${details.formatted}`
}

export { Example }
```

Read API requests with a screen reader. It announces 750 of 1000 requests and 75%, while the visible value stays compact.

## Changing the Value

Pass a new `value` when your reading changes. Meter follows your state and has no `(beforeValueChange)` callback.

Use [Progress](/docs/angular/components/progress) for a task that is working toward completion, such as an upload.

## Custom Rendering

See [Parts and Composition](/docs/angular/parts-and-composition) for changing a part's element or reading its state. Use `format` for text that should be shared by the visible value and screen readers.

## Styling

Give the track a height, background, and width. The indicator sets its width as an inline percentage of the track. Both styling options are shown in each example's Code tab.

Each part has a `data-slot`, such as `meter-indicator`, and no state attributes: the reading is in `--meter-value` and the indicator's width.

### Meter
The root exposes a CSS variable for positioning other content alongside the reading.

- `--meter-value`: Where the value sits between `min` and `max`, as a percentage, like 40%.

## API Reference

Each option below is an input on the directive.

### Meter

- `min?` (`number`): The lowest value.
- `max?` (`number`): The highest value.
- `format?` (`NumberFormat`): How the value reads, for screen readers and the Value part: `Intl.NumberFormat` options, or a function of the value. A percent style shows where the value sits between `min` and `max`; any other style shows the value itself, like `{ style: "unit", unit: "gigabyte" }`.
- `locale?` (`string`): The locale the value is formatted in. Defaults to the page's `lang`, or the browser's language without one, once the page is interactive. Server markup uses English until then, so it never disagrees with the client.
- `getValueLabel?` (`((value: number, details: GaugeLabelDetails<number>) => string)`): The text screen readers announce for the value, when the formatted value says too little, like "3 of 12 files". It receives the value, null while a progress bar is indeterminate, and details: the `formatted` value, its `percent`, `min` and `max`.
- `value` (`number`): The reading, between `min` and `max`. Outside them, the fill, the percentage and `aria-valuenow` are clamped, while the state keeps the value as given. A meter always has a value, since there's no busy state.
- `debug?` (`boolean`): Logs every state change with its reason to the console, in development builds.

### State

What each part's `state()` signal holds. Every part gets the same state.

- `value` (`number`): The value, as given.
- `percent` (`number`): Where the value sits between `min` and `max`, from 0 to 100.

`uiMeterValue` adds the formatted text:

- `value` (`number`): The value, as given.
- `percent` (`number`): Where the value sits between `min` and `max`, from 0 to 100.
- `text` (`string`): The formatted value, like "40%".

## Accessibility

Name the component with `uiMeterLabel` or `aria-label`. It exposes the range and formatted value through the `meter` role. Use `getValueLabel` when that value needs more explanation.

### Tested Behavior

Every line below is an automated test.

- A meter is named by its label and reads its value.
- The value part shows the formatted value.
- The indicator is as wide as the value.
- A new value updates the meter.
- Min and max set where the value sits.
- A unit format reads the value itself.
- A value label replaces the formatted value for screen readers.
- A value above the maximum reads as the maximum and fills the track.

Runs in React, Vue and web components.
