1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2024-06-30 05:31:44 +02:00
portfolio/src/components/molecules/ThemeSwitch.jsx

73 lines
1.7 KiB
React
Raw Normal View History

import React, { PureComponent } from 'react'
import { FadeIn } from '../atoms/Animations'
2018-05-10 16:06:17 +02:00
import { ReactComponent as Day } from '../../images/day.svg'
import { ReactComponent as Night } from '../../images/night.svg'
import './ThemeSwitch.scss'
class ThemeSwitch extends PureComponent {
2018-05-10 16:06:17 +02:00
constructor(props) {
super(props)
2018-05-10 17:58:45 +02:00
this.state = { dark: false }
2018-05-10 16:06:17 +02:00
}
componentWillMount() {
2018-05-10 17:58:45 +02:00
const now = new Date().getHours()
if (now >= 19 || now <= 7) {
this.setState({ dark: true })
}
}
2018-05-10 17:58:45 +02:00
componentDidMount() {
2018-05-10 16:06:17 +02:00
this.toggleTheme()
}
componentDidUpdate() {
this.toggleTheme()
}
isDark = () => this.state.dark === true
2018-05-10 17:58:45 +02:00
handleChange = () => {
2018-05-10 16:06:17 +02:00
this.setState({ dark: !this.isDark() })
}
toggleTheme = () => {
document
.getElementsByClassName('app')[0]
.classList.toggle('dark', this.state.dark)
2018-05-10 16:06:17 +02:00
}
render() {
return (
<FadeIn>
<aside className="themeswitch">
<label className="checkbox">
<span className="checkbox__label">Toggle Night Mode</span>
<input
onChange={this.handleChange}
type="checkbox"
name="toggle"
value="toggle"
aria-describedby="toggle"
checked={this.state.dark}
/>
<span
id="toggle"
className="checkbox__faux-container"
aria-live="assertive"
>
<Day className={this.state.dark ? 'icon' : 'icon active'} />
<span className="checkbox__faux" />
<Night className={this.state.dark ? 'icon active' : 'icon'} />
</span>
</label>
</aside>
</FadeIn>
2018-05-10 16:06:17 +02:00
)
}
}
export default ThemeSwitch