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

76 lines
1.8 KiB
React
Raw Normal View History

2018-05-12 01:42:29 +02:00
import React, { PureComponent, Fragment } from 'react'
2018-05-23 00:03:46 +02:00
import PropTypes from 'prop-types'
2018-05-12 01:42:29 +02:00
import Helmet from 'react-helmet'
import { FadeIn } from '../atoms/Animations'
2018-06-19 22:48:33 +02:00
// import { ReactComponent as Day } from '../../images/day.svg'
// import { ReactComponent as Night } from '../../images/night.svg'
2018-06-11 19:48:38 +02:00
import styles from './ThemeSwitch.module.scss'
2018-05-10 16:06:17 +02:00
2018-06-19 22:48:33 +02:00
const ThemeToggle = () => {
2018-05-23 00:03:46 +02:00
return (
<span
id="toggle"
2018-06-11 19:48:38 +02:00
className={styles.checkboxContainer}
2018-05-23 00:03:46 +02:00
aria-live="assertive"
>
2018-06-19 22:48:33 +02:00
{/* <Day className={props.dark ? null : 'active'} /> */}
2018-06-11 19:48:38 +02:00
<span className={styles.checkboxFake} />
2018-06-19 22:48:33 +02:00
{/* <Night className={props.dark ? 'active' : null} /> */}
2018-05-23 00:03:46 +02:00
</span>
)
}
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
}
2018-05-12 01:42:29 +02:00
componentDidMount() {
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
2018-05-10 16:06:17 +02:00
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() })
}
render() {
return (
2018-05-12 01:42:29 +02:00
<Fragment>
<Helmet>
<body className={this.state.dark ? 'dark' : null} />
</Helmet>
<FadeIn>
2018-06-11 19:48:38 +02:00
<aside className={styles.themeSwitch}>
<label className={styles.checkbox}>
<span className={styles.label}>Toggle Night Mode</span>
2018-05-12 01:42:29 +02:00
<input
onChange={this.handleChange}
type="checkbox"
name="toggle"
value="toggle"
aria-describedby="toggle"
checked={this.state.dark}
/>
2018-05-23 00:03:46 +02:00
<ThemeToggle dark={this.state.dark} />
2018-05-12 01:42:29 +02:00
</label>
</aside>
</FadeIn>
</Fragment>
2018-05-10 16:06:17 +02:00
)
}
}
2018-05-23 00:03:46 +02:00
ThemeToggle.propTypes = {
dark: PropTypes.bool
}
2018-05-10 16:06:17 +02:00
export default ThemeSwitch