fn

useBattery

v0.0.14testeddemo

Reactive Battery Status API. Tracks the device charging state, charge level, and the estimated charging/discharging times, keeping them in sync with the underlying BatteryManager events.

Examples

ts
const { charging, level, chargingTime, dischargingTime } = useBattery();
ts
// React to acquisition failures
const { isSupported } = useBattery({ onError: (e) => report(e) });

Demo

Loading demo…

Signature

ts
export function useBattery(options: UseBatteryOptions ={ ... }

Parameters

ParameterTypeDescription
options?UseBatteryOptionsOptions

Returns

UseBatteryReturnReactive support flag, charging state, charging/discharging times, and level
PropertyTypeDescription
isSupportedReadonly<Ref<boolean>>Whether the Battery Status API is supported in the current environment.
chargingReadonly<Ref<boolean>>Whether the device is currently charging.
chargingTimeReadonly<Ref<number>>Seconds remaining until the battery is fully charged, or 0 if already full / not charging. Infinity while charging time is unknown.
dischargingTimeReadonly<Ref<number>>Seconds remaining until the battery is fully discharged, or 0. Infinity while discharging time is unknown.
levelReadonly<Ref<number>>Battery charge level as a number between 0 and 1.