Skip to content

Commit a462969

Browse files
committed
feat: Enhance ThemeProvider with mounted state management for theme application
- Introduced useState and useEffect hooks to manage the mounted state of the ThemeProvider. - Updated theme application logic to ensure it only runs after the component is mounted, preventing potential rendering issues. - Cleaned up imports and removed unnecessary comments for improved code clarity.
1 parent aa1e5fc commit a462969

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/providers/ThemeProvider.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import React, { useContext } from 'react';
3+
import React, { useContext, useState, useEffect } from 'react';
44
import { ThemeProvider as StyledThemeProvider } from 'styled-components'; // Re-enable SC provider import
55
import '@mantine/core/styles.css';
66
import '@xyflow/react/dist/style.css';
@@ -30,6 +30,7 @@ const StyledComponentsBridge: React.FC<{ children: React.ReactNode }> = ({ child
3030
};
3131

3232
export const ThemeProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
33+
const [mounted, setMounted] = useState(false);
3334
const pathname = usePathname();
3435
const isRoot = pathname?.split('?')[0].replace(/\/+$/, '') === '' || pathname === '/';
3536
const colorScheme = isRoot ? 'dark' : 'light';
@@ -38,21 +39,28 @@ export const ThemeProvider: React.FC<{ children: React.ReactNode }> = ({ childre
3839
console.warn('toggleColorScheme is not implemented for route-based theme');
3940
};
4041

42+
useEffect(() => {
43+
setMounted(true);
44+
}, []);
45+
4146
React.useEffect(() => {
47+
if (!mounted) return;
4248
document.body.classList.remove('light-theme', 'dark-theme');
4349
document.documentElement.classList.remove('light-theme', 'dark-theme');
4450
const themeClass = colorScheme === 'dark' ? 'dark-theme' : 'light-theme';
4551
document.body.classList.add(themeClass);
4652
document.documentElement.classList.add(themeClass);
4753
document.documentElement.style.colorScheme = colorScheme;
4854
console.log('[ThemeProvider] Applied theme:', colorScheme);
49-
}, [colorScheme]);
55+
}, [colorScheme, mounted]);
56+
57+
if (!mounted) {
58+
return null;
59+
}
5060

5161
return (
5262
<ThemeContext.Provider value={{ colorScheme, toggleColorScheme }}>
53-
{/* Pass determined scheme to MantineProvider */}
5463
<MantineProvider colorScheme={colorScheme}>
55-
{/* Wrap DualAudioProvider with ReduxProvider */}
5664
<ReduxProvider>
5765
<DualAudioProvider>
5866
<StyledComponentsBridge>

0 commit comments

Comments
 (0)