Skip to content

Commit a6bda54

Browse files
Update ThemeContext.jsx
1 parent f118530 commit a6bda54

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

src/context/ThemeContext.jsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ export function ThemeProvider({ children }) {
2020
localStorage.setItem(STORAGE_KEY, theme);
2121
}, [theme]);
2222

23+
// Sync across tabs
24+
useEffect(() => {
25+
const handler = (e) => {
26+
if (e.key === STORAGE_KEY && e.newValue) {
27+
setTheme(e.newValue);
28+
}
29+
};
30+
window.addEventListener('storage', handler);
31+
return () => window.removeEventListener('storage', handler);
32+
}, []);
33+
2334
const toggleTheme = () =>
2435
setTheme((t) => (t === 'dark' ? 'light' : 'dark'));
2536

0 commit comments

Comments
 (0)