The Battery Status component provides access to the Battery Status API, enabling your Progressive Web App to monitor the device's battery level, charging status, and remaining time. This allows you to create power-aware applications that can adapt their behavior based on the device's battery condition.
This component is particularly useful for:
Power-saving modes that reduce functionality when battery is low
Alerting users before battery-intensive operations
Adjusting video/image quality based on battery level
Deferring background tasks when battery is low
Displaying battery status in system monitoring dashboards
Warning users about low battery during important tasks
Disabling power-intensive features when battery is critical
Optimizing app performance based on charging status
Browser Support
The Battery Status API has limited browser support and is primarily available on Chromium-based browsers. It requires HTTPS for security reasons.
Supported Browsers:
Chrome/Edge (Desktop and Android): Full support
Opera: Full support
Firefox: Removed in version 52 (privacy concerns)
Safari: Not supported
Always provide fallback behavior for browsers that don't support the Battery Status API. The component will dispatch an unsupported event when the API is not available.
Usage
Basic Battery Display
Visual Battery Indicator
Power-Saving Mode
Low Battery Warning
Adaptive Video Quality
Battery Monitor Dashboard
Integration with Symfony Live Components
For applications using Symfony UX Live Components, here's an advanced integration example:
Then use the component in your templates:
Parameters
None
Actions
None - This component automatically monitors battery status and dispatches events when it changes.
Targets
None
Events
pwa--battery:updated
Dispatched when the battery status changes or when the controller is initially connected.
Payload:
charging (boolean): Whether the battery is currently charging
level (number): Battery level from 0 to 1 (multiply by 100 for percentage)
chargingTime (number|null): Seconds until battery is fully charged (Infinity if not charging or unknown)
dischargingTime (number|null): Seconds until battery is fully discharged (Infinity if charging or unknown)
Example:
pwa--battery:unsupported
Dispatched when the Battery Status API is not supported by the browser.
No payload
Example:
Best Practices
Always provide fallbacks: Not all browsers support the Battery API
Respect user privacy: Don't share battery information with third parties
Use sparingly: Battery information is primarily for power optimization
Test on devices: Battery behavior varies significantly between devices
Handle edge cases: Battery values can be null or Infinity
Don't be intrusive: Subtle power-saving adjustments are better than aggressive changes
Inform users: Tell users when you're adapting behavior based on battery
Combine with other signals: Use battery status alongside other performance indicators
Monitor updates: Battery status can change frequently - be prepared
Graceful degradation: Core functionality should work regardless of battery level
Common Use Cases
Power-Saving Mode Activation
Background Task Scheduling
Critical Operation Warning
Complete Example: Adaptive Application
Troubleshooting
API not supported
Issue: Battery Status API not available
Solution: Always handle the unsupported event and provide fallback behavior:
Values are null or undefined
Issue: Battery properties return null
Cause: Some properties may not be available on all devices
Solution: Always check for null/undefined:
Inaccurate remaining time
Issue: Charging/discharging time estimates are inaccurate
Cause: Time estimates are calculated by the OS and can be unreliable
Solution: Treat time values as estimates, not precise measurements
HTTPS requirement
Issue: Battery API not working on HTTP
Solution: The Battery Status API requires a secure context (HTTPS or localhost)
Browser Compatibility
Browser
Support
Chrome/Edge (Desktop)
β Full support
Chrome/Edge (Android)
β Full support
Firefox
β Removed (v52+)
Safari
β Not supported
Opera
β Full support
Due to limited browser support and privacy concerns, always provide comprehensive fallback behavior when the Battery Status API is unavailable.
document.addEventListener('pwa--battery:unsupported', () => {
console.log('Battery Status API not supported');
// Show fallback UI or disable battery-dependent features
document.getElementById('battery-widget').style.display = 'none';
});