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

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

<script setup lang="ts">
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/vue/meter"
</script>

<template>
  <Meter :value="68">
    <MeterLabel>Storage used</MeterLabel>
    <MeterValue />
    <MeterTrack>
      <MeterIndicator />
    </MeterTrack>
  </Meter>
</template>
读取存储用量条。它的标签为它命名,可见的值和屏幕阅读器读到的值都是 68%。

安装

npm install @ui-sdk/vue @ui-sdk/core

组合

用下面的组合构建一个 Meter:

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

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

用法

导入各个部件,把它们组合起来:

<script setup lang="ts">
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/vue/meter"
</script>

<template>
  <Meter :value="40">
    <MeterLabel />
    <MeterValue />
    <MeterTrack>
      <MeterIndicator />
    </MeterTrack>
  </Meter>
</template>

自己构建

@ui-sdk/vue/meter 自带一个现成的 Meter。如果你想用自己的名字构建一个,从 @ui-sdk/core 里的 meter 原语(primitive)开始:

quota.ts
import { meter } from "@ui-sdk/core/meter"
import { createParts } from "@ui-sdk/vue/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 }
<template>
  <Quota :value="68">
    <QuotaLabel>Storage</QuotaLabel>
    <QuotaValue v-slot="{ state }">{{ state.text }}</QuotaValue>
    <QuotaTrack>
      <QuotaFill />
    </QuotaTrack>
  </Quota>
</template>

MeterValue 在没有槽位内容时显示格式化后的值。createParts 生成的部件只渲染它的默认槽位,所以在里面渲染 state.text 来显示同样的文字。

每个部件都保留其现成对应部件的行为、状态和数据属性:根是 meter,指示器根据值设置它的宽度。更多内容见渲染原语和自定义部件。

范围

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

<script setup lang="ts">
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/vue/meter"
</script>

<template>
  <Meter :value="720" :min="300" :max="850" :format="{ style: 'decimal' }">
    <MeterLabel>Credit score</MeterLabel>
    <MeterValue />
    <MeterTrack>
      <MeterIndicator />
    </MeterTrack>
  </Meter>
</template>
对比分数和填充的轨道。数字显示 720;填充显示它在 300 到 850 之间的位置。

格式与 locale

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

<script setup lang="ts">
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/vue/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
}[]
</script>

<template>
  <div data-slot="stack">
    <Meter
      v-for="{ label, ...options } in meters"
      :key="label"
      v-bind="options"
    >
      <MeterLabel>{{ label }}</MeterLabel>
      <MeterValue />
      <MeterTrack>
        <MeterIndicator />
      </MeterTrack>
    </Meter>
  </div>
</template>
对比这些读数。每个读数条都用单位、货币、自定义文字或本地化的百分比来格式化自己的值。

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

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

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

值标签

用 get-value-label 给屏幕阅读器比可见数字更多的上下文。它接收值和包含 formatted、percent、min 和 max 的详情。

<script setup lang="ts">
import {
  Meter,
  MeterIndicator,
  MeterLabel,
  MeterTrack,
  MeterValue,
} from "@ui-sdk/vue/meter"
</script>

<template>
  <Meter
    :value="750"
    :max="1000"
    :get-value-label="
      (value, details) =>
        `${value} of ${details.max} requests, ${details.formatted}`
    "
  >
    <MeterLabel>API requests</MeterLabel>
    <MeterValue>750 / 1,000</MeterValue>
    <MeterTrack>
      <MeterIndicator />
    </MeterTrack>
  </Meter>
</template>
用屏幕阅读器读取 API requests。它播报 1000 个请求中的 750 个和 75%,而可见的值保持紧凑。

改变值

读数变化时传入新的 value。Meter 跟随你的状态,没有 @value-change 事件。

对于正在走向完成的任务,比如上传,用 Progress。

自定义渲染

要更改部件的元素或读取它的状态,见部件与组合。对于可见值和屏幕阅读器应该共用的文字,用 format。

样式

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

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

Meter

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

变量内容
--meter-valueWhere the value sits between min and max, as a percentage, like 40%.

API 参考

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

状态

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

valuenumber
The value, as given.
percentnumber
Where the value sits between min and max, from 0 to 100.

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

valuenumber
The value, as given.
percentnumber
Where the value sits between min and max, from 0 to 100.
textstring
The formatted value, like "40%".

无障碍

用 MeterLabel 或 aria-label 给组件命名。它通过 meter 角色暴露范围和格式化后的值。值需要更多解释时,用 get-value-label。

经过测试的行为

下面每一行都是一条自动化测试。

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

运行于 React、Vue 和 web components。