Skip to content

Commit

Permalink
fix: preserve chosen theme when navigating
Browse files Browse the repository at this point in the history
  • Loading branch information
davidballester committed Apr 18, 2020
1 parent abf57b8 commit 88b4bd1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/components/theme-toggler/theme-toggler.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import useMediaQuery from '@material-ui/core/useMediaQuery';
import DarkModeIcon from '@material-ui/icons/Brightness3';
import LightModeIcon from '@material-ui/icons/Brightness7';

export default function ThemeToggler({ isDarkMode, toggleTheme, initializeTheme }) {
export default function ThemeToggler({ isDarkMode, isInitialized, toggleTheme, initializeTheme }) {
const darkModeBySystemPreferences = useMediaQuery('(prefers-color-scheme: dark)');
useEffect(() => {
initializeTheme(darkModeBySystemPreferences);
}, [initializeTheme, darkModeBySystemPreferences]);
if (!isInitialized) {
initializeTheme(darkModeBySystemPreferences);
}
}, [isInitialized, initializeTheme, darkModeBySystemPreferences]);
return (
<IconButton onClick={toggleTheme} color="inherit" aria-label="Dark mode toggler">
{isDarkMode && <LightModeIcon />}
Expand Down
3 changes: 2 additions & 1 deletion src/components/theme-toggler/theme-toggler.container.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { connect } from 'react-redux';

import ThemeToggler from './theme-toggler.component';
import { toggleTheme, initializeTheme, isDarkMode } from '../../ducks/theme-toggler.duck';
import { toggleTheme, initializeTheme, isDarkMode, isInitialized } from '../../ducks/theme-toggler.duck';

function mapStateToProps(state) {
return {
isDarkMode: isDarkMode(state),
isInitialized: isInitialized(state),
};
}

Expand Down
6 changes: 6 additions & 0 deletions src/ducks/theme-toggler.duck.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const THEME_INITIALIZE = 'grapher/Theme/INITIALIZE';

const initialState = {
darkMode: undefined,
initialized: false,
};

export default function reducer(state = initialState, action) {
Expand All @@ -11,6 +12,7 @@ export default function reducer(state = initialState, action) {
return {
...state,
darkMode: action.payload,
initialized: true,
};
}
case THEME_TOGGLE: {
Expand Down Expand Up @@ -41,3 +43,7 @@ export function initializeTheme(darkMode) {
export function isDarkMode(state) {
return state.theme.darkMode;
}

export function isInitialized(state) {
return state.theme.initialized;
}

0 comments on commit 88b4bd1

Please sign in to comment.