# Meter

> How much of a fixed amount is used, such as disk space. It turns amber, then red, near full.

Page: https://uicontext.com/docs/components/meter

Group: Primitives.

## When to use

How much of a fixed amount is used, such as disk space. It turns amber, then red, near full. Use [Progress](https://uicontext.com/docs/components/progress.md) instead when the bar shows a task's progress.

## Usage

```ts
import { Meter } from "./ui/uicontext/components/Meter.ts"

export const Seats = <M>(h: Html.H<M>) => Meter(h, { label: "Seats", value: 31, max: 50, valueText: "31 of 50" })
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### A server's resources

The example the page opens with: Meter in “A server's resources”.

```ts title="src/demo.ts" source="www/src/examples/meter/demo.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Meter } from "./ui/uicontext/components/Meter.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({ wrap: { display: "grid", gap: space.xl, inlineSize: "min(100%, 26rem)" } })

// a server's resources on its detail page: plenty left, getting full, almost out
export const ServerResources = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(
		Meter(h, { key: "cpu", label: "CPU", value: 38 }),
		Meter(h, { key: "memory", label: "Memory", value: 13.1, max: 16, valueText: "13.1 of 16 GB", advice: "Restart the queue worker or move to a larger machine." }),
		Meter(h, { key: "disk", label: "Disk", value: 487, max: 500, valueText: "487 of 500 GB", advice: "Writes fail when it's full. Rotate the logs in /var/log." }),
	)
```

### What uses it

`MeterBreakdown` splits the bar by part, with a legend that names each and what's free.

```ts title="src/breakdown.ts" source="www/src/examples/meter/breakdown.ts"
import type { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { MeterBreakdown } from "./ui/uicontext/components/Meter.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"

const styles = stylex.create({ wrap: { inlineSize: "min(100%, 30rem)" } })

// a drive's storage by kind of file
export const StorageBreakdown = <M>(h: Html.H<M>) =>
	h.div.attrs(sx(styles.wrap)).children(
		MeterBreakdown(h, {
			label: "Storage",
			max: 200,
			unit: "GB",
			parts: [
				{ label: "Videos", value: 84.6 },
				{ label: "Photos", value: 41.3 },
				{ label: "Documents", value: 12.8 },
				{ label: "Other", value: 6.1 },
			],
		}),
	)
```

## API reference

### Meter (view function)

**Options**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `key` | `string` | — | Names the meter among others in the same view; needed for more than one. |
| `label` (required) | `string` | — | What's measured: "Disk", "Memory", "Battery". |
| `value` (required) | `number` | — | — |
| `max` | `number` | `100` | 100 by default. |
| `valueText` | `string` | — | The words beside the label and read out with it: "41.2 of 50 GB". A percentage by default. |
| `warn` | `number` | `0.8` | The share, 0 to 1, where it turns amber; 0.8 by default. |
| `critical` | `number` | `0.95` | The share where it turns red; 0.95 by default. |
| `advice` | `string` | — | What to do about it, under the bar once it's past `warn`: "Delete old recordings or upgrade." |
| `thin` | `boolean` | — | A thinner bar, for a row in a list. |
| `style` | `Style` | — | — |

### MeterBreakdown (view function)

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `key` | `string` | Names the meter among others in the same view; needed for more than one. |
| `label` (required) | `string` | — |
| `parts` (required) | `MeterPart[]` | What each part uses, largest first reads best; up to six. |
| `max` (required) | `number` | — |
| `unit` | `string` | "GB", "seats": written after each number. |
| `locale` | `string` | — |
| `style` | `Style` | — |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/meter
```

With npm: `npx shadcn@latest add checkfu/uicontext/meter`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/meter`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/meter`.

It writes these files:

- `src/ui/uicontext/components/Meter.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Meter.ts](https://github.com/checkfu/uicontext/blob/main/src/Meter.ts)

## Related

- [Button](https://uicontext.com/docs/components/button.md) (`button`): A button, and a link that looks like one, in every tone and three sizes.
- [Toggle](https://uicontext.com/docs/components/toggle.md) (`toggle`): A button that stays pressed, such as Bold or Pin.
- [Toggle Group](https://uicontext.com/docs/components/toggle-group.md) (`toggle-group`): Buttons that stay pressed and share one value, such as text styles.
- [Input](https://uicontext.com/docs/components/input.md) (`input`): Text inputs, selects and textareas in a labeled field with a hint and an error, and their styles for controls you lay out yourself.
- [Status](https://uicontext.com/docs/components/status.md) (`status`): Status badges and tags for runs and records.
- [Glyphs](https://uicontext.com/docs/components/glyphs.md) (`glyphs`): Small marks for status, priority, color and people.
