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
Name | Type | Description |
---|---|---|
supported | boolean | Indicates whether the Battery Status API is supported in the user's browser. |
loading | boolean | Represents 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. |
level | number | Function to set the boolean state directly. |
charging | boolean | Represents whether the system's battery is charging. true means the battery is charging, false means it's not. |
chargingTime | number | Represents the time remaining in seconds until the system’s battery is fully charged. |
dischargingTime | number | Represents the time remaining in seconds until the system’s battery is completely discharged and the system is about to be suspended. |