Skip to content
caliper ui docs

Components

Battery meter

A battery cell that fills and drains with the level. It stays in the foreground colour until 20%, turns amber at 20% and red at 10%, and while charging a soft light sweeps across the fill and a bolt appears.

Free
pnpm dlx shadcn@latest add @caliper/battery-meter

Devices

Colour appears only when a battery is low.

Front door sensor
Garage remote
Hallway camera
Installs components/ui/battery-meter.tsxDepends on none beyond ReactMotion none in component source

When to use

Use it for

  • Device, sensor, remote and wearable status.
  • Any small level that people glance at in a list.

Skip it when

  • Progress to a goal; use `Progress` or `ProgressRing`.
  • Levels measured in more than a percentage.

Decisions

Why it is built this way, so you know what to keep when you change your copy.

Behaviour

The fill slides, it does not resize
The fill moves in from the left inside a clipped cell, so its rounded end never stretches as the level changes.

Styling

Colour only at the extremes
Amber at 20% and red at 10% mean something, so nothing else is coloured; charging uses the success colour.

Usage

A meter with words
It is a meter with the percentage and charging state as its value text, named for what it belongs to.

Installation

terminal
pnpm dlx shadcn@latest add @caliper/battery-meter

Adds its 2 files to components/ui/, each with its own stylesheet, and installs their packages. No account needed. The first part also adds Caliper UI’s tokens; import them once as in Installation.

Usage

This is the code behind the preview above.

example.tsx
import { useState, type ReactNode } from "react"
import { BatteryMeter } from "@/components/ui/battery-meter"
import { Button } from "@/components/ui/button"

function Panel({ title, context, className = "max-w-md", children }: { title: string; context: string; className?: string; children: ReactNode }) {
  return (
    <section className={`grid w-full gap-5 rounded-overlay border border-border bg-background p-6 shadow-resting max-sm:p-4 ${className}`}>
      <div className="grid gap-1">
        <p className="font-medium">{title}</p>
        <p className="text-sm text-muted-foreground">{context}</p>
      </div>
      {children}
    </section>
  );
}

export function BatteryMeterDemo() {
  const [level, setLevel] = useState(64);
  const [charging, setCharging] = useState(false);
  return (
    <Panel title="Devices" context="Colour appears only when a battery is low.">
      <div className="grid gap-3 text-sm">
        {[
          ["Front door sensor", 9, false],
          ["Garage remote", 18, false],
          ["Hallway camera", 100, true],
        ].map(([name, value, on]) => (
          <div key={String(name)} className="flex items-center justify-between gap-3">
            <span>{name}</span>
            <BatteryMeter level={Number(value)} charging={Boolean(on)} label={String(name)} />
          </div>
        ))}
      </div>
      <div className="flex flex-wrap items-center gap-2 border-t border-border pt-4">
        <BatteryMeter level={level} charging={charging} label="Test battery" />
        <span className="flex-1" />
        <Button variant="secondary" size="sm" onClick={() => setLevel((value) => Math.max(0, value - 12))}>
          Drain
        </Button>
        <Button variant="secondary" size="sm" onClick={() => setLevel((value) => Math.min(100, value + 12))}>
          Charge
        </Button>
        <Button variant={charging ? "default" : "secondary"} size="sm" aria-pressed={charging} onClick={() => setCharging((value) => !value)}>
          Plugged in
        </Button>
      </div>
    </Panel>
  );
}

API reference

BatteryMeter

component

Also accepts the native props of its root element and a ref.

PropTypeDefaultDescription
level *number—Charge from 0 to 100.
chargingboolean—
showValuebooleantrueWrites the percentage beside the cell.
labelstring"Battery"What the battery belongs to, e.g. "Sensor battery".

Accessibility

It exposes meter semantics, with aria-label, aria-valuemin, aria-valuemax, aria-valuenow, aria-valuetext and aria-hidden kept in step with state.

No custom key handling; Tab and the native activation keys do the work.

Motion

It declares no animation in its component source. Any transition comes from its stylesheet, which uses the duration tokens that collapse to instant under prefers-reduced-motion.

All Changing values examples