Skip to main content

battery

Reads device battery level and charging state via the Battery Status API.

Import: @rtorcato/browser-common/battery

๐Ÿ“– MDN: Battery Status API

The Battery Status API is Chromium-only and the spec is dormant โ€” getBatteryManager() resolves to undefined where unsupported, so guard with isBatteryApiAvailable() first.

Browser supportโ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
Navigator.getBatteryโœ… 38+โœ… 79+โŒ removed in 52โŒโŒLimited

Needs: secure context (HTTPS)

โœ… supported ยท โ— partial ยท โŒ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: Navigator.getBattery

Exampleโ€‹

import {
isBatteryApiAvailable,
getBatteryManager,
onBatteryLevelChange,
} from '@rtorcato/browser-common/battery'

if (isBatteryApiAvailable()) {
const battery = await getBatteryManager()
const off = battery && onBatteryLevelChange(battery, () => console.log(battery.level))
off?.()
}

Exportsโ€‹

  • isBatteryApiAvailable() โ€” feature check
  • getBatteryManager() โ€” resolves to the BatteryManager, or undefined
  • onBatteryLevelChange(battery, callback) โ€” subscribes to level changes, returns an unsubscribe function
  • onBatteryChargingChange(battery, callback) โ€” subscribes to charging state changes, returns an unsubscribe function

See the API reference for full signatures.