Skip to content

useBattery

Category
Export Size
482 B
Last Changed
5 days ago

Reactive Battery Status API

Demo

Usage

tsx
import { 
useBattery
} from '@reause/core'
const {
isSupported
,
charging
,
chargingTime
,
dischargingTime
,
level
} =
useBattery
()
StateTypeDescription
isSupportedbooleanIf the Battery Status API is supported in the current browser.
chargingbooleanIf the device is currently charging.
chargingTimenumberThe number of seconds until the device becomes fully charged.
dischargingTimenumberThe number of seconds before the device becomes fully discharged.
levelnumberA number between 0 and 1 representing the current charge level.

Browser Support

The Battery Status API has limited browser support. It is currently only available in Chromium-based browsers. Always check isSupported before using the values.

Use-cases

Our applications normally are not empathetic to battery level, we can make a few adjustments to our applications that will be more friendly to low battery users.

  • Trigger a special "dark-mode" battery saver theme settings.
  • Stop auto playing videos in news feeds.
  • Disable some background workers that are not critical.
  • Limit network calls and reduce CPU/Memory consumption.

Type Declarations

ts
export interface UseBatteryOptions extends ConfigurableNavigator {
}

export interface UseBatteryReturn {
    isSupported: boolean;
    charging: boolean;
    chargingTime: number;
    dischargingTime: number;
    level: number;
}

export interface BatteryManager extends EventTarget {
    charging: boolean;
    chargingTime: number;
    dischargingTime: number;
    level: number;
}

export interface ConfigurableNavigator {
    navigator?: Navigator;
}

export interface UseUserMediaOptions extends ConfigurableNavigator {
    enabled?: boolean;
    autoSwitch?: boolean;
    constraints?: MediaStreamConstraints;
}

export function useBattery(options?: UseBatteryOptions): UseBatteryReturn

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
067f8 - feat(core): add useBattery (#1)

Released under the MIT License. v0.1.8