refact: rework theme code

fix: lint errors

fix: lint error

fix: lint error
This commit is contained in:
Sagnik Pradhan
2021-10-19 14:46:18 -04:00
committed by Jean-Philippe Sirois
parent 975ccaa7b6
commit 9b357a98fb
4 changed files with 81 additions and 64 deletions
+6 -30
View File
@@ -1,31 +1,7 @@
import React from "react"
/**
* Implement Gatsby's SSR (Server Side Rendering) APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/ssr-apis/
*/
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"
}
if (typeof document === "undefined") return;
document.documentElement.style.setProperty(
"--initial-theme",
getInitialTheme()
)
})()
`
return <script dangerouslySetInnerHTML={{ __html: themeScript }} />
}
export const onRenderBody = ({ setPreBodyComponents }) => {
setPreBodyComponents(<ThemeScript></ThemeScript>)
}
// You can delete this file if you're not using it