mirror of
https://github.com/sergeyyarkov/devthread.ru.git
synced 2026-10-11 10:19:13 +03:00
custom dark mode
This commit is contained in:
parent
0a3ea9e4ad
commit
bdcb6cc55a
@ -40,7 +40,6 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
'gatsby-plugin-dark-mode',
|
|
||||||
'gatsby-plugin-react-helmet',
|
'gatsby-plugin-react-helmet',
|
||||||
'gatsby-plugin-sass',
|
'gatsby-plugin-sass',
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1 +1,40 @@
|
|||||||
|
import React from 'react';
|
||||||
import 'firebase/database';
|
import 'firebase/database';
|
||||||
|
|
||||||
|
const OnDark = () => {
|
||||||
|
return (
|
||||||
|
<script
|
||||||
|
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')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
` }}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const onRenderBody = ({ setPreBodyComponents }) => {
|
||||||
|
setPreBodyComponents(<OnDark key='isDarkTheme' />)
|
||||||
|
}
|
||||||
|
|||||||
8
package-lock.json
generated
8
package-lock.json
generated
@ -9071,14 +9071,6 @@
|
|||||||
"micromatch": "^3.1.10"
|
"micromatch": "^3.1.10"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"gatsby-plugin-dark-mode": {
|
|
||||||
"version": "1.1.2",
|
|
||||||
"resolved": "https://registry.npmjs.org/gatsby-plugin-dark-mode/-/gatsby-plugin-dark-mode-1.1.2.tgz",
|
|
||||||
"integrity": "sha512-kq/7/KSIgyYxolSqLxXfmcC/iBU98XpRGcIOaa3bjjl8Tb0WDYwfa45zABx1XNjB0YecWdgniss+kqmeAu04GA==",
|
|
||||||
"requires": {
|
|
||||||
"prop-types": "^15.5.7"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"gatsby-plugin-firebase": {
|
"gatsby-plugin-firebase": {
|
||||||
"version": "0.2.0-beta.4",
|
"version": "0.2.0-beta.4",
|
||||||
"resolved": "https://registry.npmjs.org/gatsby-plugin-firebase/-/gatsby-plugin-firebase-0.2.0-beta.4.tgz",
|
"resolved": "https://registry.npmjs.org/gatsby-plugin-firebase/-/gatsby-plugin-firebase-0.2.0-beta.4.tgz",
|
||||||
|
|||||||
@ -21,7 +21,6 @@
|
|||||||
"firebase": "^7.16.0",
|
"firebase": "^7.16.0",
|
||||||
"gatsby": "^2.23.12",
|
"gatsby": "^2.23.12",
|
||||||
"gatsby-image": "^2.4.12",
|
"gatsby-image": "^2.4.12",
|
||||||
"gatsby-plugin-dark-mode": "^1.1.2",
|
|
||||||
"gatsby-plugin-firebase": "^0.2.0-beta.4",
|
"gatsby-plugin-firebase": "^0.2.0-beta.4",
|
||||||
"gatsby-plugin-manifest": "^2.4.17",
|
"gatsby-plugin-manifest": "^2.4.17",
|
||||||
"gatsby-plugin-netlify-cms": "^4.3.9",
|
"gatsby-plugin-netlify-cms": "^4.3.9",
|
||||||
|
|||||||
@ -2,7 +2,6 @@ import React from 'react'
|
|||||||
import { useLocation } from '@reach/router'
|
import { useLocation } from '@reach/router'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
import { Link } from 'gatsby'
|
import { Link } from 'gatsby'
|
||||||
import { ThemeToggler } from 'gatsby-plugin-dark-mode'
|
|
||||||
import useSiteMetadataQuery from '../../hooks/useSiteMetadataQuery'
|
import useSiteMetadataQuery from '../../hooks/useSiteMetadataQuery'
|
||||||
|
|
||||||
import LogoIcon from '../../images/logo-icon.svg'
|
import LogoIcon from '../../images/logo-icon.svg'
|
||||||
@ -15,12 +14,14 @@ 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(false)
|
||||||
const { pathname } = useLocation()
|
const { pathname } = useLocation()
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
|
setIsDark(window.__isDarkTheme)
|
||||||
isMobileOpen ? document.body.style.overflowY = 'hidden' : document.body.style.overflowY = ''
|
isMobileOpen ? document.body.style.overflowY = 'hidden' : document.body.style.overflowY = ''
|
||||||
document.body.onscroll = () => window.pageYOffset >= 100 ? setIsScrolled(true) : setIsScrolled(false)
|
document.body.onscroll = () => window.pageYOffset >= 100 ? setIsScrolled(true) : setIsScrolled(false)
|
||||||
})
|
}, [setIsDark, isMobileOpen])
|
||||||
|
|
||||||
const mobileHandler = () => {
|
const mobileHandler = () => {
|
||||||
setIsMobileOpen(!isMobileOpen)
|
setIsMobileOpen(!isMobileOpen)
|
||||||
@ -56,9 +57,13 @@ const Header = () => {
|
|||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
<div className="header-nav__theme">
|
<div className="header-nav__theme">
|
||||||
<ThemeToggler>
|
{isDark ? <SunIcon onClick={() => {
|
||||||
{({ theme, toggleTheme }) => theme === 'dark' ? <SunIcon onClick={() => toggleTheme('light')} /> : <MoonIcon onClick={() => toggleTheme('dark')} />}
|
window.__setTheme('light')
|
||||||
</ThemeToggler>
|
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'}>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user