Meter unstable
A meter that shows a measurement within a known range, such as disk usage or a battery level, whose value lives in the consumer's Model. It is a headless component: the label needs an id for the meter to point at. It keeps no state of its own, and emits nothing.
Use a meter for a quantity that stands still, and Progress for a task
that moves toward done.
Anatomy
| Part | Element |
|---|---|
root |
<div role="meter"> |
label |
<span> inside the root |
value |
<span> inside the root |
track |
<div> inside the root |
indicator |
<div> inside the track |
Each part takes the consumer's builder and returns it, so the consumer
goes on chaining and adds children with .children(...). root
appends a visually hidden, non-selectable <span role="presentation">
after all its children so NVDA reads the label without leaking into
copied text. value fills in the value
text when the consumer gives it no children. The other parts add no
markup.
The root's aria-labelledby always points at the label's id. When you
draw no label, name the meter with aria-label on the root: a screen
reader skips an id that nothing has.
Data attributes
None: a meter has no states to style.
Keyboard
None: the meter isn't focusable and handles no events.
Indicator width
indicator adds inset-inline-start:0;height:inherit;width:<percentage>%
after the consumer's inline style, so its width wins and other declarations
stay. Only the CSS percentage is rounded, to at most 3 decimal places. To
draw the meter vertically or as a ring, skip indicator and use the
unrounded meter.percentage.
Example A labelled storage meter
import { Html, HtmlServer } from "effect/ui/html"import { Meter } from "effect/ui/kit"import { Effect, Schema } from "effect"
const Storage = Html.component({ name: "Storage", model: Schema.Struct({ used: Schema.Number, quota: Schema.Number }), message: Schema.Never}).make({ init: () => ({ used: 12, quota: 16 }), update: ($) => $.model, view: (h, model) => Meter(h, { props: { value: model.used, max: model.quota, valueText: `${model.used} of ${model.quota} GB` }, view: (meter) => meter.root(h.div.class("meter")).children( meter.label(h.span).children("Storage"), meter.value(h.span), meter.track(h.div.class("track")).children(meter.indicator(h.div.class("fill"))) ) })})
// The root keeps the consumer's class and reports the value, and the indicator is 75% wide.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Storage, {}))if (!/<div class="meter"[^>]* role="meter"[^>]* aria-valuenow="12" aria-valuetext="12 of 16 GB"/.test(html)) { throw new Error("the root isn't a meter at 12 of 16")}if (!/<div class="fill"[^>]* style="[^"]*width:75%"/.test(html)) throw new Error("the indicator isn't 75% wide")Components
A headless meter. Call it with the consumer's h, the props, and a
view closure that draws the root with meter.root(h.div), usually with
a label, the value and a track holding the indicator inside it.
Details
The parts also return:
percentage: the value's share of the range, from 0 to 100valueText: the textvalueshows
The meter's own attributes win over the ones the consumer set on the builder before the part:
root:role,aria-valuemin,aria-valuemax,aria-valuenow(the clamped value) andaria-valuetext.aria-labelledbyis a token list: the label's id is added to the consumer's own.label:idandrole="presentation"value:aria-hidden="true", as the root already reports the value
A part writes them when it is applied, so an attribute the consumer chains after the part is the consumer's to answer for. The indicator's width and the root's hidden span are added when the element is built, after everything the consumer chained.
Signature
declare const Meter: Headless<{ readonly max?: number; readonly min?: number; readonly value: number; readonly valueText?: string;}, never, { indicator: Part<"div", [], never>; label: Part<"span", [], never>; percentage: number; root: Part<"div", [], never>; track: Part<"div", [], never>; value: Part<"span", [], never>; valueText: string;}, never, never, never, never, never, never, {}, never, QueryEntries<{}, {}>>Models
The props of a Meter.
Signature
type MeterProps = typeof MeterProps.TypeSchemas
MeterProps
unstableProps for the value of a Meter.
Details
value: the measurement, betweenminandmax. Required: the consumer owns it. A value outside the range is clamped to it, andNaNcounts asmin.minandmax: the range,0and100by default.valueText: the text that describes the value, as the root'saria-valuetextand as whatvalueshows. Left out, it is the percentage, rounded to a whole number, as"40%", the same on the server and in the browser. Build your own text, such as"12 of 16 GB", in the view from your Model.
Signature
declare const MeterProps: Struct<{ readonly max: optional<Number>; readonly min: optional<Number>; readonly value: Number; readonly valueText: optional<String>;}>