From 9b357a98fb5e06fdd6e2dc42cf80881cf1242fc3 Mon Sep 17 00:00:00 2001 From: Sagnik Pradhan Date: Sun, 3 Oct 2021 09:38:24 +0530 Subject: [PATCH] refact: rework theme code fix: lint errors fix: lint error fix: lint error --- gatsby-ssr.js | 36 ++++------------------ src/ThemeProvider.tsx | 63 ++++++++++++++++++-------------------- src/hooks/useIsBrowser.tsx | 11 +++++++ src/hooks/useMedia.tsx | 35 +++++++++++++++++++++ 4 files changed, 81 insertions(+), 64 deletions(-) create mode 100644 src/hooks/useIsBrowser.tsx create mode 100644 src/hooks/useMedia.tsx diff --git a/gatsby-ssr.js b/gatsby-ssr.js index 33c7ee9..b17b8fc 100644 --- a/gatsby-ssr.js +++ b/gatsby-ssr.js @@ -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