diff --git a/www/reference/src/components/ColorModeToggler/index.js b/www/reference/src/components/ColorModeToggler/index.js index 7b990bc539..67aa6fd83e 100644 --- a/www/reference/src/components/ColorModeToggler/index.js +++ b/www/reference/src/components/ColorModeToggler/index.js @@ -8,19 +8,40 @@ import { useColorMode } from 'theme-ui' export default function ColorModeToggler () { const [, setColorMode] = useColorMode() + + function checkLocalStorage (currentTheme, toggleTheme) { + //check that theme local storage values are set correctly + let themeUiColorMode = window.localStorage.getItem('theme-ui-color-mode'); + let theme = window.localStorage.getItem('theme') + if (!themeUiColorMode) { + themeUiColorMode = theme || currentTheme + window.localStorage.setItem('theme-ui-color-mode', themeUiColorMode); + setColorMode(themeUiColorMode); + } + if (!theme) { + theme = themeUiColorMode || currentTheme + window.localStorage.setItem('theme', theme); + toggleTheme(theme) + } + + return theme || themeUiColorMode || currentTheme; + } return ( - {({ theme, toggleTheme }) => ( - - )} + {({ theme, toggleTheme }) => { + const currentTheme = checkLocalStorage(theme, toggleTheme); + return ( + + ); + }} ) } \ No newline at end of file