1- // majority of code from https://www.joshwcomeau.com/react/dark-mode/ and https://github.com/gperl27/gatsby-styled-components-dark-mode
2- // context provider for app to make accessible theme setting, toggle function, etc.
1+ //majority of code from https://www.joshwcomeau.com/react/dark-mode/ and https://github.com/gperl27/gatsby-styled-components-dark-mode
2+
3+ //context provider for app to make accessible theme setting, toggle function, etc.
34
45import React , { createContext , useState , useEffect , useCallback } from "react" ;
56
@@ -36,16 +37,13 @@ const applyThemeToDOM = (theme) => {
3637 const root = window . document . documentElement ;
3738 root . style . setProperty ( "--initial-color-mode" , theme ) ;
3839 root . setAttribute ( "data-theme" , theme ) ;
39-
40- // Sync with SSR injected state
4140 window . __theme = theme ;
4241} ;
4342
4443export const ThemeManagerProvider = ( props ) => {
4544 const [ themeSetting , setThemeSetting ] = useState ( ThemeSetting . SYSTEM ) ;
4645 const [ didLoad , setDidLoad ] = useState ( false ) ;
4746
48- // Initialize state from SSR script to prevent hydration mismatch
4947 const [ isDark , setIsDark ] = useState ( ( ) => {
5048 if ( isBrowser ) {
5149 if ( window . __theme === ThemeSetting . DARK ) return true ;
@@ -59,11 +57,8 @@ export const ThemeManagerProvider = (props) => {
5957
6058 const root = window . document . documentElement ;
6159 const initialColorValue = ( root . style . getPropertyValue ( "--initial-color-mode" ) || "" ) . trim ( ) ;
62-
63- // Prioritize SSR-injected theme
6460 const actualTheme = window . __theme || initialColorValue || ThemeSetting . LIGHT ;
6561
66- // Get stored theme from localStorage
6762 const storedTheme = localStorage . getItem ( DarkThemeKey ) ;
6863
6964 if ( storedTheme && storedTheme !== ThemeSetting . SYSTEM ) {
@@ -85,7 +80,7 @@ export const ThemeManagerProvider = (props) => {
8580 setDidLoad ( true ) ;
8681 } , [ ] ) ;
8782
88- // Listen to system color scheme changes only when on SYSTEM mode
83+ // Listen to system color scheme changes only when on SYSTEM mode
8984 useEffect ( ( ) => {
9085 if ( ! isBrowser || themeSetting !== ThemeSetting . SYSTEM ) return ;
9186
@@ -107,11 +102,11 @@ export const ThemeManagerProvider = (props) => {
107102 const newIsDark = ! isDark ;
108103 const newTheme = newIsDark ? ThemeSetting . DARK : ThemeSetting . LIGHT ;
109104
110- // Update state
105+ // Update state
111106 setIsDark ( newIsDark ) ;
112107 setThemeSetting ( newTheme ) ;
113108
114- // Apply to DOM immediately
109+ // Apply to DOM immediately
115110 applyThemeToDOM ( newTheme ) ;
116111
117112 // Persist to localStorage
@@ -143,14 +138,14 @@ export const ThemeManagerProvider = (props) => {
143138 return ;
144139 }
145140
146- // Update state
141+ // Update state
147142 setIsDark ( newIsDark ) ;
148143 setThemeSetting ( setting ) ;
149144
150145 // Apply to DOM immediately
151146 applyThemeToDOM ( themeToApply ) ;
152147
153- // Persist to localStorage
148+ // Persist to localStorage
154149 localStorage . setItem ( DarkThemeKey , setting ) ;
155150 } ,
156151 [ isDark ]
0 commit comments