# Meter

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.

- Entry point: `menhir/meter`
- Size: 1.1 kB, minified and gzipped, Solid left out

> To add Meter to a project, install the packages below with the project's package manager, then copy the example that matches how the project is styled, Tailwind CSS or plain CSS, and adapt its markup and class names to the project.

## Install

```sh
pnpm add menhir solid-js @solidjs/web
```

## Anatomy

```tsx
<Meter.Root>
  <Meter.Value />
</Meter.Root>
```

## Example

### Tailwind CSS

`MeterExample.tsx`:

```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>
  );
}
```

### CSS

`MeterExample.tsx`:

```tsx
import { Meter } from "menhir/meter";

import "./meter.css";

export default function MeterExample() {
  return (
    <Meter.Root
      value={120}
      max={500}
      format={{ style: "unit", unit: "gigabyte" }}
      aria-labelledby="meter-label"
      class="meter"
    >
      <span id="meter-label">Storage used</span>
      <Meter.Value />
      <div class="meter-track">
        <div class="meter-fill" />
      </div>
    </Meter.Root>
  );
}
```

`meter.css`:

```css
/* The least CSS that makes the Meter visible: a track, and a fill as wide
   as --meter, which the Root sets. Everything else is the browser's.
   Colours are Tailwind's stone palette; light-dark() picks the second one
   in dark mode (color-scheme: dark). */
.meter {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.5rem;
  width: 16rem;
  font:
    0.875rem system-ui,
    sans-serif;
}

.meter-track {
  grid-column: 1 / -1;
  height: 0.5rem;
  border-radius: 9999px;
  overflow: hidden;
  background: light-dark(
    oklch(92.3% 0.003 48.717) /* stone-200 */,
    oklch(26.8% 0.007 34.298) /* stone-800 */
  );
}

.meter-fill {
  width: var(--meter);
  height: 100%;
  background: light-dark(
    oklch(26.8% 0.007 34.298) /* stone-800 */,
    oklch(86.9% 0.005 56.366) /* stone-300 */
  );
}
```

## 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).

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `number` | — | The measurement, from min to max. A value outside the range is clamped to it. |
| `min` | `number` | `0` | The lowest value the measurement can take. |
| `max` | `number` | `100` | The highest value the measurement can take. |
| `format` | `Intl.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. |
| `locale` | `Intl.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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the value with your own component. Read once. |

## Keyboard

| Keys | Action |
| --- | --- |
| `Tab` | Moves past the meter, which is not focusable: the user cannot change its value. |
