@@ -15,7 +15,7 @@ interface ThemeContextType {
1515}
1616
1717const ThemeContext = createContext < ThemeContextType > ( {
18- isDarkTheme : true ,
18+ isDarkTheme : false ,
1919 toggleTheme : ( ) => { } ,
2020 isLoaded : false ,
2121 currentNetworkId : 'polkadot' ,
@@ -37,7 +37,9 @@ export function ThemeProvider({ children, initialNetworkId = 'polkadot' }: Theme
3737 const [ isLoaded , setIsLoaded ] = useState < boolean > ( false ) ;
3838 const [ currentNetworkId , setCurrentNetworkId ] = useState < string > ( initialNetworkId ) ;
3939 const [ mounted , setMounted ] = useState ( false ) ;
40+ const [ storedThemeApplied , setStoredThemeApplied ] = useState ( false ) ;
4041
42+
4143 const applyTheme = useCallback ( ( isDark : boolean , networkId : string ) => {
4244 if ( typeof window === 'undefined' ) return ;
4345
@@ -61,17 +63,21 @@ export function ThemeProvider({ children, initialNetworkId = 'polkadot' }: Theme
6163 } ) ;
6264 } , [ ] ) ;
6365
66+
6467 useEffect ( ( ) => {
65- setMounted ( true ) ;
68+ if ( typeof window === 'undefined' || storedThemeApplied ) return ;
6669
70+ setMounted ( true ) ;
6771 const storedTheme = localStorage . getItem ( 'theme' ) ;
6872 const storedNetwork = localStorage . getItem ( 'currentNetwork' ) ;
69- const networkToUse = storedNetwork || currentNetworkId ;
7073
74+
75+ const networkToUse = storedNetwork || initialNetworkId ;
7176 if ( storedNetwork && storedNetwork !== currentNetworkId ) {
72- setCurrentNetworkId ( storedNetwork ) ;
77+ setCurrentNetworkId ( networkToUse ) ;
7378 }
7479
80+
7581 let isDark : boolean ;
7682 if ( storedTheme ) {
7783 isDark = storedTheme === 'dark' ;
@@ -82,7 +88,9 @@ export function ThemeProvider({ children, initialNetworkId = 'polkadot' }: Theme
8288 setIsDarkTheme ( isDark ) ;
8389 applyTheme ( isDark , networkToUse ) ;
8490 setIsLoaded ( true ) ;
91+ setStoredThemeApplied ( true ) ;
8592
93+
8694 const mediaQuery = window . matchMedia ( '(prefers-color-scheme: dark)' ) ;
8795 const handleChange = ( e : MediaQueryListEvent ) => {
8896 if ( ! localStorage . getItem ( 'theme' ) ) {
@@ -93,14 +101,15 @@ export function ThemeProvider({ children, initialNetworkId = 'polkadot' }: Theme
93101
94102 mediaQuery . addEventListener ( 'change' , handleChange ) ;
95103 return ( ) => mediaQuery . removeEventListener ( 'change' , handleChange ) ;
96- } , [ applyTheme , currentNetworkId ] ) ;
104+ } , [ applyTheme , initialNetworkId , storedThemeApplied ] ) ;
97105
106+
98107 useEffect ( ( ) => {
99- if ( isLoaded && mounted ) {
100- applyTheme ( isDarkTheme , currentNetworkId ) ;
101- localStorage . setItem ( 'currentNetwork' , currentNetworkId ) ;
102- }
103- } , [ currentNetworkId , isLoaded , isDarkTheme , applyTheme , mounted ] ) ;
108+ if ( ! mounted ) return ;
109+
110+ applyTheme ( isDarkTheme , currentNetworkId ) ;
111+ localStorage . setItem ( 'currentNetwork' , currentNetworkId ) ;
112+ } , [ currentNetworkId , isDarkTheme , applyTheme , mounted ] ) ;
104113
105114 const toggleTheme = useCallback ( ( ) => {
106115 const newIsDark = ! isDarkTheme ;
@@ -148,7 +157,7 @@ export function ThemeProvider({ children, initialNetworkId = 'polkadot' }: Theme
148157
149158
150159 if ( ! mounted ) {
151- return null ;
160+ return < div style = { { visibility : 'hidden' } } > { children } </ div > ;
152161 }
153162
154163 return (
0 commit comments