Docs
useBattery

Track the battery status of a user's device with useBattery.

Loading...

Installation

pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-battery

Usage

"use client"
 
import { useBattery } from "@/hooks/use-battery"
 
import Battery from "./Battery"
 
export function Component() {
  const { loading, level, charging, chargingTime, dischargingTime } =
    useBattery()
 
  return (
    <div>
      <h1>useBattery</h1>
      {loading ? (
        <p>Loading...</p>
      ) : (
        <div>
          <h2>Level: {level}</h2>
          <h2>Charging: {charging ? "Yes" : "No"}</h2>
          <h2>Charging time: {chargingTime}</h2>
          <h2>Discharging time: {dischargingTime}</h2>
        </div>
      )}
    </div>
  )
}

API Reference

Return Values

NameTypeDescription
supportedbooleanIndicates whether the Battery Status API is supported in the user's browser.
loadingbooleanRepresents the level of the system's battery. 0.0 means that the system's battery is completely discharged, and 1.0 means the battery is completely charged.
levelnumberFunction to set the boolean state directly.
chargingbooleanRepresents whether the system's battery is charging. true means the battery is charging, false means it's not.
chargingTimenumberRepresents the time remaining in seconds until the system’s battery is fully charged.
dischargingTimenumberRepresents the time remaining in seconds until the system’s battery is completely discharged and the system is about to be suspended.