Skip to content

Instantly share code, notes, and snippets.

@GalindoSVQ
Created January 7, 2024 11:26
Show Gist options
  • Select an option

  • Save GalindoSVQ/02e022ce08c2a7ac096d49b4aaf9cba8 to your computer and use it in GitHub Desktop.

Select an option

Save GalindoSVQ/02e022ce08c2a7ac096d49b4aaf9cba8 to your computer and use it in GitHub Desktop.
import * as React from 'react';
export function useBattery() {
const [state, setState] = React.useState({
supported: true,
loading: true,
level: null,
charging: null,
chargingTime: null,
dischargingTime: null,
});
React.useEffect(() => {
if (!navigator.getBattery) {
setState((s) => ({
...s,
supported: false,
loading: false,
}));
return;
}
let battery = null;
const handleChange = () => {
setState({
supported: true,
loading: false,
level: battery.level,
charging: battery.charging,
chargingTime: battery.chargingTime,
dischargingTime: battery.dischargingTime,
});
};
navigator.getBattery().then((b) => {
battery = b;
handleChange();
b.addEventListener('levelchange', handleChange);
b.addEventListener('chargingchange', handleChange);
b.addEventListener('chargingtimechange', handleChange);
b.addEventListener('dischargingtimechange', handleChange);
});
return () => {
if (battery) {
battery.removeEventListener('levelchange', handleChange);
battery.removeEventListener('chargingchange', handleChange);
battery.removeEventListener('chargingtimechange', handleChange);
battery.removeEventListener('dischargingtimechange', handleChange);
}
};
}, []);
return state;
}
@GalindoSVQ

Copy link
Copy Markdown
Author

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