Skip to content

feat: add useBattery hook - #1667

Open
samithahansaka wants to merge 1 commit into
react-hookz:masterfrom
samithahansaka:feat/use-battery
Open

feat: add useBattery hook#1667
samithahansaka wants to merge 1 commit into
react-hookz:masterfrom
samithahansaka:feat/use-battery

Conversation

@samithahansaka

Copy link
Copy Markdown

Summary

  • Adds useBattery hook that tracks device battery state using the Battery Status API
  • Returns { isSupported, fetched, charging, chargingTime, dischargingTime, level }
  • Subscribes to battery change events
  • SSR-safe with proper cleanup

Test plan

  • DOM tests (8 tests)
  • SSR tests (5 tests)
  • All 540 project tests pass
  • Lint passes
  • Build succeeds

Partial implementation of #33 (sensor hooks from react-use)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new useBattery hook that tracks device battery state using the Battery Status API. The hook provides battery charge level, charging status, and time estimates, with proper SSR safety and event subscription handling.

Changes:

  • Adds useBattery hook with support for tracking battery charging state, level, and time estimates
  • Includes comprehensive test coverage with 8 DOM tests and 5 SSR tests
  • Adds Battery API mock setup for testing environment

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/useBattery/index.ts Core hook implementation that subscribes to Battery Status API events and returns battery state
src/useBattery/index.dom.test.ts DOM environment tests covering hook behavior, event subscriptions, and state updates
src/useBattery/index.ssr.test.ts SSR tests verifying safe fallback behavior when Battery API is unavailable
src/util/testing/setup/battery.test.ts Test setup file that mocks the Battery Status API for vitest
vitest.config.ts Configuration update to include battery test setup
src/index.ts Exports the new useBattery hook under the Navigator section

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/useBattery/index.ts Outdated
Comment thread src/useBattery/index.ts Outdated
Comment thread src/useBattery/index.ts Outdated
@samithahansaka
samithahansaka force-pushed the feat/use-battery branch 2 times, most recently from d2afe76 to 11be20c Compare January 12, 2026 10:48
Port `useBattery` from react-use, exposing charging state, charge level and
the charging/discharging time estimates from the Battery Status API.

The hook is SSR-safe: access to `navigator.getBattery` is guarded by
`isBrowser`, so a server render reports `isSupported: false` with undefined
readings rather than throwing. Listeners are not attached when the effect is
cleaned up while `getBattery()` is still pending, and a rejected `getBattery()`
falls back to the unfetched state instead of leaving an unhandled rejection.

Refs react-hookz#33
@samithahansaka

samithahansaka commented Aug 9, 2026

Copy link
Copy Markdown
Author

I have rebased this onto the latest master. It was out of date and had a conflict, because it changed vitest.config.ts and that file is gone now.

What I changed:

  • Moved to the new vp toolchain. The battery test setup is now registered in vite.config.ts.
  • Fixed the three review comments from January: cleanup on unmount, handling a failed getBattery() call, and the wording of the fetched doc.
  • Added the hook to the README list. I missed that before.
  • Put the four event names in one list, so on and off cannot get out of sync.
  • Squashed everything into one feat: commit.

Checks on my machine: yarn fmt:check, yarn lint and yarn build all pass, and 547 tests pass. Coverage for useBattery is 94.6% of statements and 77.8% of branches. Two lines are not covered: the early return when the API is missing, and the dev only warning. Both have a comment saying why.

CI has not run here yet. Fork PRs need a maintainer to approve the run.

Two things I would like your opinion on:

  • If getBattery() fails, the hook returns {isSupported: true, fetched: false}. That looks the same as still loading. An error field would fix it, but it adds to the public API. I left it out. I can add it if you want.
  • isSupported is false on the server and true on the first client render. useNetworkState behaves the same way and takes an optional initialState. I did not add that here. I can if you want.

useBattery is still unchecked in #33. #1394 adds the same hook. I am happy to close this one if you prefer that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants