Skip to content

Instantly share code, notes, and snippets.

@klipach
Created February 29, 2016 14:13
Show Gist options
  • Save klipach/2e4f37df6bc037d2c584 to your computer and use it in GitHub Desktop.
Save klipach/2e4f37df6bc037d2c584 to your computer and use it in GitHub Desktop.
var React = require('react');
module.exports = React.createClass({
getInitialState: function () {
return {
battery: 0
};
},
componentDidMount: function () {
navigator.getBattery().then(
function (battery) {
this.setState({battery: battery});
battery.addEventListener('chargingchange', function () {
this.setState({battery: battery});
}.bind(this));
battery.addEventListener('levelchange', function () {
this.setState({battery: battery});
}.bind(this));
}.bind(this)
)
},
render: function () {
return (<div className="battery">
<div className={ this.state.battery.charging ? 'green' : 'red' }>{ Math.floor(this.state.battery.level * 100) }%</div>
</div>)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment