fix: hopefully fix for #334

This commit is contained in:
Sagnik Pradhan
2021-10-19 14:46:18 -04:00
committed by Jean-Philippe Sirois
parent 6b267780b5
commit b6b295fced
2 changed files with 31 additions and 9 deletions
+28 -6
View File
@@ -1,7 +1,29 @@
/** import React from "react"
* Implement Gatsby's SSR (Server Side Rendering) APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/ssr-apis/
*/
// You can delete this file if you're not using it const ThemeScript = () => {
const themeScript = `
(function() {
function getInitialTheme() {
const userSetTheme = window.localStorage.getItem("theme")
const prefersLightTheme = window.matchMedia(
"(prefers-color-scheme: light)"
)
if (userSetTheme && userSetTheme !== '"unset"') return userSetTheme
else if (prefersLightTheme.matches === true) return "light"
else return "dark"
}
document.documentElement.style.setProperty(
"--initial-theme",
getInitialTheme()
)
})()
`
return <script dangerouslySetInnerHTML={{ __html: themeScript }} />
}
export const onRenderBody = ({ setPreBodyComponents }) => {
setPreBodyComponents(<ThemeScript></ThemeScript>)
}
+3 -3
View File
@@ -26,7 +26,9 @@ const ThemeProvider: FC<IScopedDownChildren> = ({ children }) => {
// App's current theme // App's current theme
const [theme, setTheme] = useState<ThemeType>( const [theme, setTheme] = useState<ThemeType>(
localTheme === "unset" ? "dark" : localTheme document.documentElement.style.getPropertyValue(
"--initial-theme"
) as ThemeType
) )
const themeObject = useMemo( const themeObject = useMemo(
@@ -40,8 +42,6 @@ const ThemeProvider: FC<IScopedDownChildren> = ({ children }) => {
"(prefers-color-scheme: light)" "(prefers-color-scheme: light)"
) )
setTheme(prefersLightTheme.matches ? "light" : "dark")
prefersLightTheme.onchange = ({ matches }) => prefersLightTheme.onchange = ({ matches }) =>
setTheme(matches ? "light" : "dark") setTheme(matches ? "light" : "dark")