Skip to content

Commit

Permalink
[dashboard] Automatically update color theme on system change / stora…
Browse files Browse the repository at this point in the history
…ge event
  • Loading branch information
jankeromnes committed Apr 16, 2021
1 parent 2093f15 commit 83d8c0a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions components/dashboard/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ function App() {
}, []);

useEffect(() => {
if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
const updateTheme = () => {
const isDark = localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia("(prefers-color-scheme: dark)").matches);
document.documentElement.classList.toggle('dark', isDark);
}
updateTheme();
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
mediaQuery.addEventListener('change', updateTheme);
window.addEventListener('storage', updateTheme);
return function cleanup() {
mediaQuery.removeEventListener('change', updateTheme);
window.removeEventListener('storage', updateTheme);
}
}, [localStorage.theme]);

Expand Down

0 comments on commit 83d8c0a

Please sign in to comment.