Storage
x

Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/meter.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Meter } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    type MeterParts = Component.Headless.Parts<typeof Meter>
    
    const sizes = { sm: "sm", md: "md", lg: "lg" } as const
    const levels = { low: "low", medium: "medium", high: "high" } as const
    
    export type MeterSize = keyof typeof sizes
    export type MeterLevel = keyof typeof levels
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    /**
     * The `role="meter"` root: a two-column grid with the label and the value above
     * the track. `size` sets the track's height. The kit's meter has no states, so
     * pass a `level` computed from your value to colour the fill: `low` is the
     * success colour, `medium` the warning and `high` the destructive one.
     */
    export const MeterRoot = <Msg>(
      h: Html.H<Msg>,
      meter: MeterParts,
      options: ClassOption & {
        readonly size?: MeterSize | undefined
        readonly level?: MeterLevel | undefined
      } = {}
    ) =>
      meter.root(
        h.div
          .class(cn("group/meter grid w-full grid-cols-[1fr_auto] items-baseline gap-x-3 gap-y-2", options.class))
          .attrs({
            "data-size": sizes[options.size ?? "md"],
            "data-level": options.level === undefined ? undefined : levels[options.level]
          })
      )
    
    export const MeterLabel = <Msg>(h: Html.H<Msg>, meter: MeterParts, options: ClassOption = {}) =>
      meter.label(h.span.class(cn("text-[13px] font-medium text-foreground", options.class)))
    
    /** The value's text. The kit fills it in when it has no children. */
    export const MeterValue = <Msg>(h: Html.H<Msg>, meter: MeterParts, options: ClassOption = {}) =>
      meter.value(h.span.class(cn("text-[13px] text-muted-foreground tabular-nums", options.class)))
    
    export const MeterTrack = <Msg>(h: Html.H<Msg>, meter: MeterParts, options: ClassOption = {}) =>
      meter.track(
        h.div.class(cn(
          "col-span-2 h-2 overflow-hidden rounded-full bg-secondary group-data-[size=sm]/meter:h-1 " +
            "group-data-[size=lg]/meter:h-3",
          options.class
        ))
      )
    
    /** The fill, whose width the kit sets. */
    export const MeterIndicator = <Msg>(h: Html.H<Msg>, meter: MeterParts, options: ClassOption = {}) =>
      meter.indicator(
        h.div.class(cn(
          "h-full rounded-full bg-brand transition-[width] duration-300 ease-out " +
            "group-data-[level=low]/meter:bg-success group-data-[level=medium]/meter:bg-warning " +
            "group-data-[level=high]/meter:bg-destructive",
          options.class
        ))
      )
    
    /**
     * The meter as a ring: a root that draws the fill from `meter.percentage`
     * instead of an indicator. Put the value and the label inside it.
     */
    export const MeterRing = <Msg>(h: Html.H<Msg>, meter: MeterParts, options: ClassOption = {}) =>
      meter.root(
        h.div
          .class(cn(
            "relative grid size-28 place-content-center justify-items-center gap-0.5 rounded-full " +
              "bg-[conic-gradient(var(--brand)_calc(var(--meter-percentage)*1%),var(--secondary)_0)] " +
              "before:absolute before:inset-2 before:rounded-full before:bg-background *:relative",
            options.class
          ))
          .style({ "--meter-percentage": String(meter.percentage) })
      )
    
  3. Import it

    import { MeterRoot, MeterLabel, MeterValue, MeterTrack, MeterIndicator, MeterRing } from "#ui/meter"
    

Usage

Disk usage.ts
import { Html } from "@effect/ui/html"
import { Meter } from "@effect/ui-kit"
import { Schema } from "effect"
import { MeterIndicator, MeterLabel, MeterRoot, MeterTrack, MeterValue } from "#ui/meter"

export const MeterStorage = Html.component({
  name: "MeterStorage",
  model: Schema.Struct({ used: Schema.Number, quota: Schema.Number }),
  message: Schema.Never
}).make({
  init: () => ({ used: 12, quota: 16 }),
  update: ($) => $.model,
  view: (h, model) =>
    h.div.class("flex w-full max-w-sm flex-col gap-4").children(
      Meter(h, {
        props: { value: model.used, max: model.quota, valueText: `${model.used} of ${model.quota} GB` },
        view: (meter) =>
          MeterRoot(h, meter).children(
            MeterLabel(h, meter).children("Storage"),
            MeterValue(h, meter),
            MeterTrack(h, meter).children(
              MeterIndicator(h, meter)
            )
          )
      })
    )
})

Examples

Thresholds

The meter has no states, so the level option colours the fill from the Model's value.

CPU load
x

Below 60 is low, from 60 medium, from 85 high.

Sizes

Three track heights from the size option of the root.

sm
x
md
x
lg
x

Password strength

Five checks on what was typed, as a meter from 0 to 5 with a name for each score.

Strength
x

In a card

Several meters in a usage card. The ones near their quota change colour.

Usage this month

The plan renews on the 1st.

Requests
x
Bandwidth
x
Seats
x

Ring

MeterRing skips the indicator and draws a ring from meter.percentage.

Batteryx

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
rootThe <div role="meter">. Its aria-labelledby points at the label.
labelA <span> inside the root that names the meter.
valueA <span> that shows the value text when it has no children.
trackThe <div> the indicator fills.
indicatorThe <div> inside the track, whose width the kit sets.
percentage, valueTextPlain values, for a vertical meter or a ring.

API reference

Props

PropTypeDescription
valuenumberThe measurement. Required. A value outside the range is clamped to it.
minnumberThe start of the range. 0 by default.
maxnumberThe end of the range. 100 by default.
valueTextstringThe text for aria-valuetext and the value part. The rounded percentage when left out.

Data attributes

AttributeDescription
(none)A meter has no states to style. The ui file writes data-size and data-level on the root from its options.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.