h3/use's logo

h3/use

Command Palette

Search for a command to run...

useBatteryStatus

A React hook to access system battery information via the Battery Status API

useBatteryStatus
Battery Status API is not supported in this browser.

Installation

Props

The hook returns an object with the following properties:

PropertyTypeDescription
supportedbooleanWhether the Battery Status API is supported
levelnumberCurrent battery level (0 to 1)
chargingbooleanWhether the battery is currently charging
chargingTimenumberTime in seconds until the battery is fully charged
dischargingTimenumberTime in seconds until the battery is fully discharged

Key Features & Details

  • Real-time battery information with automatic updates via the Battery Status API events.
  • Graceful fallback when the API is not supported.
  • Simple and declarative interface for easy integration.

Examples

Basic Usage

const { supported, level, charging } = useBatteryStatus();
 
if (supported) {
  console.log(`Battery level: ${level * 100}%`);
} else {
  console.warn('Battery Status API not supported');
}

Full Example

import React from 'react';
 
import { useBatteryStatus } from '@/registry/hooks/use-battery-status';
 
export default function BatteryComponent() {
  const { supported, level, charging, chargingTime, dischargingTime } =
    useBatteryStatus();
 
  if (!supported) {
    return <p>Battery API not supported</p>;
  }
 
  return (
    <div>
      <p>Level: {Math.round(level * 100)}%</p>
      <p>Status: {charging ? 'Charging' : 'Discharging'}</p>
      <p>Charging Time: {chargingTime}s</p>
      <p>Discharging Time: {dischargingTime}s</p>
    </div>
  );
}