Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/app/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,33 @@ import Header from '@/components/Header';
import Footer from '@/components/Footer';
import { ThemeToggle } from '@/components/ThemeToggle';
import { ThemeProvider } from '@/components/ThemeContext';

export { metadata };

export default function RootLayout({ children }) {
const setInitialTheme = `
(function() {
try {
const savedTheme = localStorage.getItem('theme');
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const theme = savedTheme || (prefersDark ? 'dark' : 'light');
if (theme === 'dark') {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
} catch (e) {
console.warn('Theme init failed:', e);
}
})();
`;

return (
<html lang="en" className="scroll-smooth">
<head>
{/* This runs before the React app mounts, preventing flicker */}
<script dangerouslySetInnerHTML={{ __html: setInitialTheme }} />
</head>
<body className="flex flex-col min-h-screen bg-background-light dark:bg-background-dark text-gray-900 dark:text-gray-100">
<ThemeProvider>
<Header themeToggle={<ThemeToggle />} />
Expand Down
24 changes: 10 additions & 14 deletions src/components/ThemeContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ import { createContext, useContext, useState, useEffect } from 'react';
const ThemeContext = createContext();

export const ThemeProvider = ({ children }) => {
const [theme, setTheme] = useState(null); // Start with `null` to indicate the theme is loading

useEffect(() => {
// Check initial theme preference
const savedTheme = localStorage.getItem('theme');
const prefersDark = window.matchMedia(
'(prefers-color-scheme: dark)'
).matches;

const initialTheme = savedTheme || (prefersDark ? 'dark' : 'light');
setTheme(initialTheme);
document.documentElement.classList.toggle('dark', initialTheme === 'dark');
}, []);
const [theme, setTheme] = useState(
typeof window !== 'undefined'
? (localStorage.getItem('theme') || (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'))
: 'light'
);

const toggleTheme = () => {
const newTheme = theme === 'light' ? 'dark' : 'light';
Expand All @@ -26,9 +18,13 @@ export const ThemeProvider = ({ children }) => {
document.documentElement.classList.toggle('dark', newTheme === 'dark');
};

useEffect(() => {
document.documentElement.classList.toggle('dark', theme === 'dark');
}, [theme]);

return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
{theme !== null && children}
{children}
</ThemeContext.Provider>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/ThemeToggle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export const ThemeToggle = () => {
)}
</button>
);
};
};