Skip to main content
On this pageOverview

Meter

Overview

Meter displays a scalar quantity within a known range, such as remaining storage, battery charge, or a health level. It is a stateless controlled view: your Model owns the value, and Meter provides the ARIA and styling attributes. Use Progress instead when the value represents completion of a task.

See it in an app

Check out how Meter is wired up in a real Foldkit app.

Examples

Basic

Pass the current value and render the provided attribute groups. Spread attributes.label onto the visible label, attributes.meter onto the element that carries the meter role, and attributes.fill onto the filled portion. The fill width reflects the value's position within the range.

Health75 / 100
import type { HtmlBuilder } from 'foldkit/html'

import { Meter } from '@foldkit/ui'

const view = (h: HtmlBuilder<Message>) =>
  Meter.view(
    {
      id: 'health',
      value: 75,
      max: 100,
      valueText: '75 of 100 health',
      toView: attributes =>
        h.div(
          [h.Class('flex flex-col gap-2')],
          [
            h.span([...attributes.label], ['Health']),
            h.div(
              [
                ...attributes.meter,
                h.Class('h-3 w-full rounded-full bg-gray-200'),
              ],
              [
                h.div([
                  ...attributes.fill,
                  h.Class('h-full rounded-full bg-emerald-600'),
                ]),
              ],
            ),
          ],
        ),
    },
    h,
  )

Thresholds

low, high, and optimum expose application-specific thresholds as data-low, data-high, and data-optimum. They do not change the Meter's ARIA attributes; use them to style the ranges your application considers low, high, or optimal.

Storage82%
low 30, high 80, optimum 20 as data attributes

Styling

Meter is headless. Your toView callback controls its markup and styling.

AttributeCondition
data-valuePresent on meter and fill with the clamped value.
data-minPresent on meter and fill with the normalized minimum.
data-maxPresent on meter and fill with the normalized maximum.
data-lowPresent on meter when low is set.
data-highPresent on meter when high is set.
data-optimumPresent on meter when optimum is set.

The fill attribute group carries an inline width matching the value's position within the normalized range. Meter intentionally has no loading or complete state because it describes a measurement, not task progress.

Accessibility

The meter element receives role="meter", aria-valuemin, aria-valuemax, and aria-valuenow. By default, it is named through aria-labelledby, which points to the id carried by the label attribute group. Use ariaLabel when there is no visible label, or ariaLabelledBy to reference a different labeling element. When both overrides are provided, ariaLabel takes precedence.

Use valueText when the number needs a natural-language equivalent. It accepts either a string or a (value, max) => string formatter and sets aria-valuetext from the clamped value.

Meter clamps value into [min, max]. If max is lower than min, it is normalized to min before any ARIA or data attributes are emitted, so the rendered range remains valid.

Meter is read-only and has no keyboard interaction.

API Reference

ViewConfig

Configuration object passed to Meter.view().

NameTypeDefaultDescription
idstring—Unique id for the Meter and its generated label id.
valuenumber—Current scalar value. Clamped into the normalized range.
minnumber0Lower bound of the range.
maxnumber100Upper bound of the range. Normalized to at least min.
lownumber—Value exposed as data-low for consumer styling.
highnumber—Value exposed as data-high for consumer styling.
optimumnumber—Value exposed as data-optimum for consumer styling.
valueTextstring | ((value: number, max: number) => string)—Natural-language value exposed through aria-valuetext.
ariaLabelstring—Accessible name used instead of the rendered label.
ariaLabelledBystring—Id of a different element that labels the Meter.
toView(attributes: MeterAttributes) => Html—Renders the Meter from the meter, fill, and label attribute groups.

MeterAttributes

Attribute groups provided to the toView callback.

NameTypeDefaultDescription
meterReadonlyArray<Attribute<Message>>—Spread onto the element carrying the meter role, ARIA value attributes, and data attributes.
fillReadonlyArray<Attribute<Message>>—Spread onto the filled portion. Includes the calculated inline width and range data attributes.
labelReadonlyArray<Attribute<Message>>—Spread onto the visible label. Includes the id referenced by aria-labelledby by default.