Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useBattery.md
1---2category: Sensors3---45# useBattery67Reactive [Battery Status API](https://developer.mozilla.org/en-US/docs/Web/API/Battery_Status_API), more often referred to as the Battery API, provides information about the system's battery charge level and lets you be notified by events that are sent when the battery level or charging status change. This can be used to adjust your app's resource usage to reduce battery drain when the battery is low, or to save changes before the battery runs out in order to prevent data loss.89## Usage1011```ts12import { useBattery } from '@vueuse/core'1314const { isSupported, charging, chargingTime, dischargingTime, level } = useBattery()15```1617| State | Type | Description |18| --------------- | --------- | ----------------------------------------------------------------- |19| isSupported | `Boolean` | If the Battery Status API is supported in the current browser. |20| charging | `Boolean` | If the device is currently charging. |21| chargingTime | `Number` | The number of seconds until the device becomes fully charged. |22| dischargingTime | `Number` | The number of seconds before the device becomes fully discharged. |23| level | `Number` | A number between 0 and 1 representing the current charge level. |2425::: warning Browser Support26The Battery Status API has limited browser support. It is currently only available in Chromium-based browsers. Always check `isSupported` before using the values.27:::2829## Use-cases3031Our 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.3233- Trigger a special "dark-mode" battery saver theme settings.34- Stop auto playing videos in news feeds.35- Disable some background workers that are not critical.36- Limit network calls and reduce CPU/Memory consumption.3738## Component Usage3940```vue41<template>42<UseBattery v-slot="{ isSupported, charging, level }">43<div v-if="isSupported">44<p>Is Charging: {{ charging }}</p>45<p>Battery Level: {{ (level * 100).toFixed(0) }}%</p>46</div>47<div v-else>48Battery API not supported49</div>50</UseBattery>51</template>52```5354## Type Declarations5556```ts57export interface UseBatteryOptions extends ConfigurableNavigator {}58export interface UseBatteryReturn extends Supportable {59charging: ShallowRef<boolean>60chargingTime: ShallowRef<number>61dischargingTime: ShallowRef<number>62level: ShallowRef<number>63}64export interface BatteryManager extends EventTarget {65charging: boolean66chargingTime: number67dischargingTime: number68level: number69}70/**71* Reactive Battery Status API.72*73* @see https://vueuse.org/useBattery74*75* @__NO_SIDE_EFFECTS__76*/77export declare function useBattery(78options?: UseBatteryOptions,79): UseBatteryReturn80```81