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.
pnpm dlx shadcn@latest add @caliper/battery-meterWhen 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
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.
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.
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.
Related
- MeterA measurement within a known range, with thresholds marked on the track and a word for whether it is fine.
- Progress ringDeterminate progress in a ring that eases to each value, draws a check when done and a cross when it fails.
- Usage meterA segmented bar showing how an allowance is used, with a warning near the limit.