Skip to content

Conversation

@samithahansaka
Copy link

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)

  Implements useBattery hook that tracks device battery state using the Battery Status API.

  - Returns isSupported, fetched, charging, chargingTime, dischargingTime, level
  - Subscribes to battery change events (chargingchange, chargingtimechange, dischargingtimechange, levelchange)
  - SSR-safe with proper cleanup on unmount
  - Includes DOM and SSR tests

  Closes react-hookz#33 (partial - sensor hooks)
Copy link

Copilot AI left a comment

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.

  - Add mounted flag to prevent state updates after unmount
  - Prevent event listener registration if component unmounts before
    getBattery() resolves
  - Add .catch() handler to gracefully handle promise rejections
  - Fix misleading comment for fetched field
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.

2 participants