Menhir

Meter

View as Markdown

Shows a measurement within a known range, such as a battery's charge or the storage a disk has used. It renders a div with role="meter" and the value in aria-valuenow, aria-valuetext and the CSS custom property --meter, and draws nothing: size an element inside it with --meter. For how far a task has got, such as an upload, use Progress instead, and for a value with no meaningful maximum, neither. The role requires a name: give it aria-label, or aria-labelledby naming an element.

Size
1.1 kB

Example

Styling
Storage used120 GB
MeterExample.tsx
import { Meter } from "menhir/meter";

export default function MeterExample() {
  return (
    <Meter.Root
      value={120}
      max={500}
      format={{ style: "unit", unit: "gigabyte" }}
      aria-labelledby="meter-label"
      class="grid w-64 grid-cols-[1fr_auto] gap-2 text-sm"
    >
      <span id="meter-label">Storage used</span>
      <Meter.Value />
      <div class="col-span-2 h-2 overflow-hidden rounded-full bg-stone-200 dark:bg-stone-800">
        <div class="h-full w-(--meter) bg-stone-800 dark:bg-stone-300" />
      </div>
    </Meter.Root>
  );
}

Anatomy

<Meter.Root>
  <Meter.Value />
</Meter.Root>

API reference

Root

<div role="meter">, with aria-valuemin, aria-valuemax, aria-valuenow and aria-valuetext. Its style sets --meter, the percentage of the range, such as 25%, which every element inside it inherits: draw the filled part with a plain element, such as width: var(--meter).

PropTypeDefaultDescription
valuenumber—The measurement, from min to max. A value outside the range is clamped to it.
minnumber0The lowest value the measurement can take.
maxnumber100The highest value the measurement can take.
formatIntl.NumberFormatOptions{ style: "percent" }How aria-valuetext and Meter.Value format the value. The percent style formats the percentage of the range; any other style formats the value itself, such as { style: "unit", unit: "gigabyte" } for "120 GB". For other text, such as "50% (6 hours) remaining", pass your own aria-valuetext.
localeIntl.LocalesArgument—The language to format in, else the runtime's default. Pass it when rendering on the server, so that the server and the browser format alike.
render(props) => JSX.Element—Renders the meter with your own component. Read once.

Value

<span> with the value as text, the same as the Root's aria-valuetext, yours if you pass one.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the value with your own component. Read once.

Keyboard

KeysAction
TabMoves past the meter, which is not focusable: the user cannot change its value.