显示已知范围内的测量值,比如已用存储或账户配额。
用 Meter 显示一个已知范围内的值,比如已用存储、电池电量或账户配额。它填充一条轨道来显示这个值,屏幕阅读器也会播报同一个值。
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>
)
}安装
npm install @ui-sdk/react @ui-sdk/core组合
用下面的组合构建一个 Meter:
Meter
├── MeterLabel
├── MeterValue
└── MeterTrack
└── MeterIndicatorMeter保存值和范围。MeterLabel给它一个可见的名称。也可以在根上使用aria-label。MeterValue显示格式化后的值。MeterTrack决定读数条的大小,MeterIndicator填充它。
值已经是根的无障碍描述的一部分。可见的值对屏幕阅读器隐藏,避免重复朗读。
用法
导入各个部件,把它们组合起来:
<Meter value={40}>
<MeterLabel />
<MeterValue />
<MeterTrack>
<MeterIndicator />
</MeterTrack>
</Meter>自己构建
@ui-sdk/react/meter 自带一个现成的 Meter。如果你想用自己的名字构建一个,从 @ui-sdk/core 里的 meter 原语(primitive)开始:
"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 }<Quota value={68}>
<QuotaLabel>Storage</QuotaLabel>
<QuotaValue>{(state) => state.text}</QuotaValue>
<QuotaTrack>
<QuotaFill />
</QuotaTrack>
</Quota>MeterValue 在没有 children 时显示格式化后的值。createParts 生成的部件只渲染它的 children,所以传入 (state) => state.text 来显示同样的文字。
每个部件都保留其现成对应部件的行为、状态和数据属性:根是 meter,指示器根据值设置它的宽度。更多内容见渲染原语和自定义部件。
范围
min 和 max 设置范围,默认是 0 和 100。超出这个范围的值在填充、百分比和 aria-valuenow 上会被钳制,但状态的 value 和百分比以外的格式会按你传入的值保留。这里,720 大约位于 300 到 850 之间 76% 的位置。小数格式让可见的值保持为 720。
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>
)
}格式与 locale
把 Intl.NumberFormat 选项传给 format,以显示单位、货币或小数位。函数可以返回自定义文字,比如已占座位的数量。
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 的详情。
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>
)
}改变值
读数变化时传入新的 value。Meter 跟随你的状态,没有 onValueChange 回调。
对于正在走向完成的任务,比如上传,用 Progress。
自定义渲染
要更改部件的元素或读取它的状态,见部件与组合。对于可见值和屏幕阅读器应该共用的文字,用 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.NumberFormatoptions, or a function of the value. A percent style shows where the value sits betweenminandmax; 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
formattedvalue, itspercent,minandmax. valuenumber- The reading, between
minandmax. Outside them, the fill, the percentage andaria-valuenoware 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
minandmax, from 0 to 100.
MeterValue 额外提供格式化后的文字:
valuenumber- The value, as given.
percentnumber- Where the value sits between
minandmax, from 0 to 100. textstring- 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.
运行于 React、Vue 和 web components。