Skip to content

Commit

Permalink
Added darkmode component
Browse files Browse the repository at this point in the history
  • Loading branch information
vb committed Jul 14, 2016
1 parent ca106e2 commit 24771e7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Settings = require('./Settings')
var StoryStore = require('./stores/StoryStore')
var UpdatesStore = require('./stores/UpdatesStore')
var SettingsStore = require('./stores/SettingsStore')
var CurrentColor = require('./Darkmode')

var App = React.createClass({
getInitialState() {
Expand All @@ -26,7 +27,11 @@ var App = React.createClass({

componentDidMount() {
// Empty the prebooted HTML and hydrate using live results from Firebase
this.setState({ prebootHTML: '', showChildren: true })
this.setState({
prebootHTML: '',
showChildren: true,
darkMode: SettingsStore.darkMode
})
},

componentWillUnmount() {
Expand All @@ -48,6 +53,10 @@ var App = React.createClass({
this.setState({showSettings: !this.state.showSettings})
},

toggleDarkMode() {
this.setState({darkMode: !this.state.darkMode})
},

render() {
return <div className="App" onClick={this.state.showSettings && this.toggleSettings}>
<div className="App__wrap">
Expand All @@ -62,7 +71,7 @@ var App = React.createClass({
<a className="App__settings" tabIndex="0" onClick={this.toggleSettings} onKeyPress={this.toggleSettings}>
{this.state.showSettings ? 'hide settings' : 'settings'}
</a>
{this.state.showSettings && <Settings key="settings"/>}
{this.state.showSettings && <Settings key="settings" toggleDarkMode={this.toggleDarkMode}/>}
</div>
<div className="App__content">
<div dangerouslySetInnerHTML={{ __html: this.state.prebootHTML }}/>
Expand All @@ -71,6 +80,7 @@ var App = React.createClass({
<div className="App__footer">
<a href="https://github.com/insin/react-hn">insin/react-hn</a>
</div>
<CurrentColor darkMode={this.state.darkMode} />
</div>
</div>
}
Expand Down
27 changes: 27 additions & 0 deletions src/Darkmode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var React = require('react')
var SettingsStore = require('./stores/SettingsStore')

var CurrentColor = React.createClass({
propTypes: {
darkMode: React.PropTypes.bool
},
getDefaultProps() {
return {
darkMode: SettingsStore.darkMode
}
},
componentDidMount() {
document.documentElement.classList.toggle('App--dark', this.props.darkMode)
},
componentWillReceiveProps(nextProps) {
document.documentElement.classList.toggle('App--dark', nextProps.darkMode)
},
componentWillUnmount() {
document.documentElement.classList.remove('App--dark')
},
render() {
return <div></div>
}
})

module.exports = CurrentColor

0 comments on commit 24771e7

Please sign in to comment.