# Meter

显示已知范围内的测量值，比如已用存储或账户配额。

用 Meter 显示一个已知范围内的值，比如已用存储、电池电量或账户配额。它填充一条轨道来显示这个值，屏幕阅读器也会播报同一个值。

```tsx title="basic.tsx"
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/react/meter"

export function Example() {
  return (
    <Meter value={68}>
      <MeterLabel>Storage used</MeterLabel>
      <MeterValue />
      <MeterTrack>
        <MeterIndicator />
      </MeterTrack>
    </Meter>
  )
}
```

读取存储用量条。它的标签为它命名，可见的值和屏幕阅读器读到的值都是 68%。

## 安装
```bash
npm install @ui-sdk/react @ui-sdk/core
```

## 组合
用下面的组合构建一个 `Meter`：

```text
Meter
├── MeterLabel
├── MeterValue
└── MeterTrack
    └── MeterIndicator
```

- `Meter` 保存值和范围。
- `MeterLabel` 给它一个可见的名称。也可以在根上使用 `aria-label`。
- `MeterValue` 显示格式化后的值。
- `MeterTrack` 决定读数条的大小，`MeterIndicator` 填充它。

值已经是根的无障碍描述的一部分。可见的值对屏幕阅读器隐藏，避免重复朗读。

## 用法
导入各个部件，把它们组合起来：

```tsx
<Meter value={40}>
  <MeterLabel />
  <MeterValue />
  <MeterTrack>
    <MeterIndicator />
  </MeterTrack>
</Meter>
```

## 自己构建
`@ui-sdk/react/meter` 自带一个现成的 Meter。如果你想用自己的名字构建一个，从 `@ui-sdk/core` 里的 `meter` 原语（primitive）开始：

```ts title="quota.ts"
"use client"

import { meter } from "@ui-sdk/core/meter"
import { createParts } from "@ui-sdk/react/primitive"

const {
  Root: Quota,
  Label: QuotaLabel,
  Value: QuotaValue,
  Track: QuotaTrack,
  Indicator: QuotaFill,
} = createParts(meter, {
  root: "div",
  label: "span",
  value: "span",
  track: "div",
  indicator: "div",
})

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

```tsx
<Quota value={68}>
  <QuotaLabel>Storage</QuotaLabel>
  <QuotaValue>{(state) => state.text}</QuotaValue>
  <QuotaTrack>
    <QuotaFill />
  </QuotaTrack>
</Quota>
```

`MeterValue` 在没有 children 时显示格式化后的值。`createParts` 生成的部件只渲染它的 children，所以传入 `(state) => state.text` 来显示同样的文字。

每个部件都保留其现成对应部件的行为、状态和数据属性：根是 `meter`，指示器根据值设置它的宽度。更多内容见[渲染原语](/docs/react/rendering)和[自定义部件](/docs/react/custom-parts)。

## 范围
`min` 和 `max` 设置范围，默认是 0 和 100。超出这个范围的值在填充、百分比和 `aria-valuenow` 上会被钳制，但状态的 `value` 和百分比以外的格式会按你传入的值保留。这里，720 大约位于 300 到 850 之间 76% 的位置。小数格式让可见的值保持为 720。

```tsx title="min-max.tsx"
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/react/meter"

export function Example() {
  return (
    <Meter value={720} min={300} max={850} format={{ style: "decimal" }}>
      <MeterLabel>Credit score</MeterLabel>
      <MeterValue />
      <MeterTrack>
        <MeterIndicator />
      </MeterTrack>
    </Meter>
  )
}
```

对比分数和填充的轨道。数字显示 720；填充显示它在 300 到 850 之间的位置。

## 格式与 locale
把 `Intl.NumberFormat` 选项传给 `format`，以显示单位、货币或小数位。函数可以返回自定义文字，比如已占座位的数量。

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

const 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 function Example() {
  return (
    <div data-slot="stack">
      {meters.map(({ label, ...options }) => (
        <Meter key={label} {...options}>
          <MeterLabel>{label}</MeterLabel>
          <MeterValue />
          <MeterTrack>
            <MeterIndicator />
          </MeterTrack>
        </Meter>
      ))}
    </div>
  )
}
```

对比这些读数。每个读数条都用单位、货币、自定义文字或本地化的百分比来格式化自己的值。

百分比格式报告值在 `min` 和 `max` 之间的位置。其他格式直接使用值本身。

`locale` 控制数字和分隔符。省略它时，组件在挂载后读取页面语言，并以浏览器语言作为回退。最初的服务器和客户端渲染使用英文。

在周围的内容上设置 `dir="rtl"`，让轨道从右侧填充。

## 值标签
用 `getValueLabel` 给屏幕阅读器比可见数字更多的上下文。它接收值和包含 `formatted`、`percent`、`min` 和 `max` 的详情。

```tsx title="value-label.tsx"
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/react/meter"

export function Example() {
  return (
    <Meter
      value={750}
      max={1000}
      getValueLabel={(value, details) =>
        `${value} of ${details.max} requests, ${details.formatted}`
      }
    >
      <MeterLabel>API requests</MeterLabel>
      <MeterValue>750 / 1,000</MeterValue>
      <MeterTrack>
        <MeterIndicator />
      </MeterTrack>
    </Meter>
  )
}
```

用屏幕阅读器读取 API requests。它播报 1000 个请求中的 750 个和 75%，而可见的值保持紧凑。

## 改变值
读数变化时传入新的 `value`。Meter 跟随你的状态，没有 `onValueChange` 回调。

对于正在走向完成的任务，比如上传，用 [Progress](/docs/react/components/progress)。

## 自定义渲染
要更改部件的元素或读取它的状态，见[部件与组合](/docs/react/parts-and-composition)。对于可见值和屏幕阅读器应该共用的文字，用 `format`。

## 样式
给轨道一个高度、背景和宽度。指示器以轨道的内联百分比设置自己的宽度。两种样式选项都显示在每个示例的代码标签页里。

每个部件都有一个 `data-slot`，例如 `meter-indicator`，并且没有状态属性：读数体现在 `--meter-value` 和指示器的宽度上。

### Meter
根暴露一个 CSS 变量，用来把其他内容和读数并排定位。

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

## API 参考
props 与下面相同，作为 React props 传入。

### 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.

### 状态
渲染函数、基于状态的类名和槽位收到的内容。每个部件收到相同的状态。

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

`MeterValue` 额外提供格式化后的文字：

- `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%".

## 无障碍
用 `MeterLabel` 或 `aria-label` 给组件命名。它通过 `meter` 角色暴露范围和格式化后的值。值需要更多解释时，用 `getValueLabel`。

### 经过测试的行为
下面每一行都是一条自动化测试。

- 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 和 web components.
