mirror of
https://github.com/sergeyyarkov/devthread.ru.git
synced 2026-10-11 10:19:13 +03:00
upd theme
This commit is contained in:
parent
1a342f3438
commit
e4cb0df237
@ -1,17 +1,38 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import ThemeContext from './src/context/ThemeContext'
|
||||||
import 'firebase/database';
|
import 'firebase/database';
|
||||||
|
|
||||||
const OnDark = () => {
|
const OnDark = () => {
|
||||||
const codeToRunOnClient = `
|
|
||||||
if (localStorage.getItem('dark') || (new Date().getHours() >= 22 || new Date().getHours() <= 6)) {
|
|
||||||
document.body.classList.add('dark')
|
|
||||||
window.localStorage.setItem('dark', 'true')
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<script
|
<script
|
||||||
dangerouslySetInnerHTML={{ __html: codeToRunOnClient }}
|
dangerouslySetInnerHTML={{ __html: `
|
||||||
|
(() => {
|
||||||
|
window.__isDarkTheme = false
|
||||||
|
|
||||||
|
window.__setTheme = function(theme) {
|
||||||
|
onChangeTheme(theme)
|
||||||
|
}
|
||||||
|
|
||||||
|
// onRefreshPage
|
||||||
|
if (localStorage.getItem('dark') || (new Date().getHours() >= 22 || new Date().getHours() <= 6)) {
|
||||||
|
window.__isDarkTheme = true
|
||||||
|
document.body.classList.add('dark')
|
||||||
|
window.localStorage.setItem('dark', 'true')
|
||||||
|
}
|
||||||
|
|
||||||
|
function onChangeTheme(theme) {
|
||||||
|
if (theme === 'dark') {
|
||||||
|
window.__isDarkTheme = true
|
||||||
|
localStorage.setItem('dark', 'true')
|
||||||
|
document.body.classList.add('dark')
|
||||||
|
} else if (theme === 'light') {
|
||||||
|
window.__isDarkTheme = false
|
||||||
|
localStorage.removeItem('dark')
|
||||||
|
document.body.classList.remove('dark')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
` }}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import { useLocation } from '@reach/router'
|
|||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
import { Link } from 'gatsby'
|
import { Link } from 'gatsby'
|
||||||
import useSiteMetadataQuery from '../../hooks/useSiteMetadataQuery'
|
import useSiteMetadataQuery from '../../hooks/useSiteMetadataQuery'
|
||||||
import ThemeContext from '../../context/ThemeContext'
|
|
||||||
|
|
||||||
import LogoIcon from '../../images/logo-icon.svg'
|
import LogoIcon from '../../images/logo-icon.svg'
|
||||||
import TelegramIcon from '../../images/telegram-icon.svg'
|
import TelegramIcon from '../../images/telegram-icon.svg'
|
||||||
@ -15,8 +14,8 @@ const Header = () => {
|
|||||||
const { siteMetadata: { title, menuLinks, social: { twitter, telegram } } } = useSiteMetadataQuery()
|
const { siteMetadata: { title, menuLinks, social: { twitter, telegram } } } = useSiteMetadataQuery()
|
||||||
const [isScrolled, setIsScrolled] = React.useState(false)
|
const [isScrolled, setIsScrolled] = React.useState(false)
|
||||||
const [isMobileOpen, setIsMobileOpen] = React.useState(false)
|
const [isMobileOpen, setIsMobileOpen] = React.useState(false)
|
||||||
|
const [isDark, setIsDark] = React.useState(window.__isDarkTheme)
|
||||||
const { pathname } = useLocation()
|
const { pathname } = useLocation()
|
||||||
const { isDark, toggleLightTheme, toggleDarkTheme } = React.useContext(ThemeContext)
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
isMobileOpen ? document.body.style.overflowY = 'hidden' : document.body.style.overflowY = ''
|
isMobileOpen ? document.body.style.overflowY = 'hidden' : document.body.style.overflowY = ''
|
||||||
@ -57,7 +56,13 @@ const Header = () => {
|
|||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
<div className="header-nav__theme">
|
<div className="header-nav__theme">
|
||||||
{isDark ? <SunIcon onClick={toggleLightTheme} /> : <MoonIcon onClick={toggleDarkTheme} />}
|
{isDark ? <SunIcon onClick={() => {
|
||||||
|
window.__setTheme('light')
|
||||||
|
setIsDark(!isDark)
|
||||||
|
}} /> : <MoonIcon onClick={() => {
|
||||||
|
window.__setTheme('dark')
|
||||||
|
setIsDark(!isDark)
|
||||||
|
}} />}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className={isMobileOpen ? 'header-content__nav mobile mobileOpen' : 'header-content__nav mobile'}>
|
<div className={isMobileOpen ? 'header-content__nav mobile mobileOpen' : 'header-content__nav mobile'}>
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import React from "react"
|
|||||||
const ThemeContext = React.createContext({ isDark: false })
|
const ThemeContext = React.createContext({ isDark: false })
|
||||||
|
|
||||||
const ThemeProvider = ({ children }) => {
|
const ThemeProvider = ({ children }) => {
|
||||||
const [isDark, setIsDark] = React.useState(false)
|
const [isDark, setIsDark] = React.useState(true)
|
||||||
|
|
||||||
const toggleDarkTheme = () => {
|
const toggleDarkTheme = () => {
|
||||||
setIsDark(true)
|
setIsDark(true)
|
||||||
@ -17,16 +17,17 @@ const ThemeProvider = ({ children }) => {
|
|||||||
window.localStorage.removeItem('dark')
|
window.localStorage.removeItem('dark')
|
||||||
}
|
}
|
||||||
|
|
||||||
React.useEffect(() => {
|
// React.useEffect(() => {
|
||||||
if (localStorage.getItem('dark')) {
|
// if (localStorage.getItem('dark')) {
|
||||||
setIsDark(true)
|
// setIsDark(true)
|
||||||
}
|
// }
|
||||||
}, [setIsDark])
|
// }, [setIsDark])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ThemeContext.Provider
|
<ThemeContext.Provider
|
||||||
value={{
|
value={{
|
||||||
isDark,
|
isDark,
|
||||||
|
setIsDark,
|
||||||
toggleDarkTheme,
|
toggleDarkTheme,
|
||||||
toggleLightTheme
|
toggleLightTheme
|
||||||
}}
|
}}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user