1
0
mirror of https://github.com/kremalicious/portfolio.git synced 2025-02-14 21:10:41 +01:00

dark mode in global store

This commit is contained in:
Matthias Kretschmann 2018-09-17 00:43:52 +02:00
parent b001dc9fa3
commit a1a2d258da
Signed by: m
GPG Key ID: 606EEEF3C479A91F
6 changed files with 111 additions and 101 deletions

View File

@ -40,7 +40,7 @@ The whole [portfolio](https://matthiaskretschmann.com) is a React-based Single P
### Lighthouse score ### Lighthouse score
[![Lighthouse scores](https://lighthouse.now.sh/?perf=99&pwa=100&a11y=100&bp=100&seo=100)](https://travis-ci.com/kremalicious/portfolio) [![Lighthouse scores](https://lighthouse.now.sh/?perf=100&pwa=100&a11y=100&bp=100&seo=100)](https://travis-ci.com/kremalicious/portfolio)
### One data file to rule all pages ### One data file to rule all pages

View File

@ -1,7 +1,7 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { StaticQuery, graphql } from 'gatsby' import { StaticQuery, graphql } from 'gatsby'
import posed, { PoseGroup } from 'react-pose' import posed from 'react-pose'
import { fadeIn } from '../atoms/Transitions' import { fadeIn } from '../atoms/Transitions'
import styles from './Availability.module.scss' import styles from './Availability.module.scss'
@ -32,7 +32,6 @@ export default class Availability extends PureComponent {
return ( return (
!this.props.hide && ( !this.props.hide && (
<PoseGroup animateOnMount={true}>
<Animation <Animation
className={ className={
status status
@ -46,7 +45,6 @@ export default class Availability extends PureComponent {
}} }}
/> />
</Animation> </Animation>
</PoseGroup>
) )
) )
}} }}

View File

@ -1,7 +1,7 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { StaticQuery, graphql } from 'gatsby' import { StaticQuery, graphql } from 'gatsby'
import posed, { PoseGroup } from 'react-pose' import posed from 'react-pose'
import { moveInBottom } from '../atoms/Transitions' import { moveInBottom } from '../atoms/Transitions'
import Logo from '../svg/Logo' import Logo from '../svg/Logo'
@ -48,7 +48,6 @@ export default class LogoUnit extends PureComponent {
const { minimal } = this.state const { minimal } = this.state
return ( return (
<PoseGroup animateOnMount={true}>
<Animation> <Animation>
<div className={minimal ? styles.minimal : styles.logounit}> <div className={minimal ? styles.minimal : styles.logounit}>
<Logo className={styles.logounit__logo} /> <Logo className={styles.logounit__logo} />
@ -60,7 +59,6 @@ export default class LogoUnit extends PureComponent {
</p> </p>
</div> </div>
</Animation> </Animation>
</PoseGroup>
) )
}} }}
/> />

View File

@ -1,7 +1,7 @@
import React, { PureComponent } from 'react' import React, { PureComponent } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { StaticQuery, graphql } from 'gatsby' import { StaticQuery, graphql } from 'gatsby'
import posed, { PoseGroup } from 'react-pose' import posed from 'react-pose'
import { moveInTop } from '../atoms/Transitions' import { moveInTop } from '../atoms/Transitions'
import Email from '../svg/Email' import Email from '../svg/Email'
@ -81,7 +81,6 @@ export default class Network extends PureComponent {
return ( return (
!this.props.hide && ( !this.props.hide && (
<PoseGroup animateOnMount={true}>
<Animation className={this.state.classes}> <Animation className={this.state.classes}>
{Object.keys(meta.social).map((key, i) => ( {Object.keys(meta.social).map((key, i) => (
<a className={styles.link} href={meta.social[key]} key={i}> <a className={styles.link} href={meta.social[key]} key={i}>
@ -90,7 +89,6 @@ export default class Network extends PureComponent {
</a> </a>
))} ))}
</Animation> </Animation>
</PoseGroup>
) )
) )
}} }}

View File

@ -1,7 +1,8 @@
import React, { PureComponent, Fragment } from 'react' import React, { PureComponent, Fragment } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import Helmet from 'react-helmet' import Helmet from 'react-helmet'
import posed, { PoseGroup } from 'react-pose' import posed from 'react-pose'
import { Consumer } from '../../store/createContext'
import { fadeIn } from '../atoms/Transitions' import { fadeIn } from '../atoms/Transitions'
import Day from '../svg/Day' import Day from '../svg/Day'
@ -22,66 +23,41 @@ ThemeToggle.propTypes = {
dark: PropTypes.bool dark: PropTypes.bool
} }
export default class ThemeSwitch extends PureComponent { const ThemeToggleInput = ({ dark, toggleDark }) => (
state = { dark: null }
isDark = () => this.state.dark === true
darkLocalStorageMode = darkLocalStorage => {
if (darkLocalStorage === 'true') {
this.setState({ dark: true })
} else {
this.setState({ dark: false })
}
}
darkMode = now => {
if (!this.isDark() && (now >= 19 || now <= 7)) {
this.setState({ dark: true })
} else {
this.setState({ dark: null })
}
}
componentDidMount() {
const now = new Date().getHours()
const darkLocalStorage = localStorage.getItem('dark')
if (darkLocalStorage) {
this.darkLocalStorageMode(darkLocalStorage)
} else {
this.darkMode(now)
}
}
handleChange = event => {
this.setState({ dark: event.target.checked })
localStorage.setItem('dark', event.target.checked)
}
render() {
return (
<Fragment>
<Helmet>
<body className={this.isDark() ? 'dark' : null} />
</Helmet>
<PoseGroup animateOnMount={true}>
<Animation className={styles.themeSwitch}>
<label className={styles.checkbox}>
<span className={styles.label}>Toggle Night Mode</span>
<input <input
onChange={this.handleChange} onChange={toggleDark}
type="checkbox" type="checkbox"
name="toggle" name="toggle"
value="toggle" value="toggle"
aria-describedby="toggle" aria-describedby="toggle"
checked={this.isDark()} checked={dark}
/> />
<ThemeToggle dark={this.isDark()} /> )
ThemeToggleInput.propTypes = {
dark: PropTypes.bool,
toggleDark: PropTypes.func
}
export default class ThemeSwitch extends PureComponent {
render() {
return (
<Consumer>
{({ dark, toggleDark }) => (
<Fragment>
<Helmet>
<body className={dark ? 'dark' : null} />
</Helmet>
<Animation className={styles.themeSwitch}>
<label className={styles.checkbox}>
<span className={styles.label}>Toggle Night Mode</span>
<ThemeToggleInput dark={dark} toggleDark={toggleDark} />
<ThemeToggle dark={dark} />
</label> </label>
</Animation> </Animation>
</PoseGroup>
</Fragment> </Fragment>
)}
</Consumer>
) )
} }
} }

View File

@ -4,13 +4,53 @@ import { Provider } from './createContext'
export default class AppProvider extends Component { export default class AppProvider extends Component {
state = { state = {
dark: false dark: false,
toggleDark: () => {
this.setState({ dark: !this.state.dark })
if (this.store) {
this.store.setItem('dark', !this.state.dark)
}
}
} }
static propTypes = { static propTypes = {
children: PropTypes.any.isRequired children: PropTypes.any.isRequired
} }
store = typeof localStorage === 'undefined' ? null : localStorage
darkLocalStorageMode = darkLocalStorage => {
if (darkLocalStorage === 'true') {
this.setState({ dark: true })
} else {
this.setState({ dark: false })
}
}
darkMode = now => {
if (!this.state.dark && (now >= 19 || now <= 7)) {
this.setState({ dark: true })
} else {
this.setState({ dark: null })
}
}
checkDark = () => {
const now = new Date().getHours()
const darkLocalStorage = this.store.getItem('dark')
if (darkLocalStorage) {
this.darkLocalStorageMode(darkLocalStorage)
} else {
this.darkMode(now)
}
}
componentDidMount() {
this.checkDark()
}
render() { render() {
return <Provider value={this.state}>{this.props.children}</Provider> return <Provider value={this.state}>{this.props.children}</Provider>
} }