From 085572b1440222a25fa5870c484cfaa8b961d440 Mon Sep 17 00:00:00 2001 From: iamdarshshah Date: Mon, 25 May 2020 22:54:51 +0530 Subject: [PATCH] feat: Persist theme in localStorage --- packages/docs/src/App.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/docs/src/App.js b/packages/docs/src/App.js index 8cccf3d..1cba456 100644 --- a/packages/docs/src/App.js +++ b/packages/docs/src/App.js @@ -30,11 +30,25 @@ import './style.css' const themes = { base, light, dark } +const useThemeState = (defaultValue, key) => { + const [value, setValue] = React.useState(() => { + const theme = window.localStorage.getItem(key) + + return theme !== null ? JSON.parse(theme) : defaultValue + }) + + React.useEffect(() => { + window.localStorage.setItem(key, JSON.stringify(value)) + }, [key, value]) + + return [value, setValue] +} + const App = () => { const [menuVisible, setMenuVisibility] = React.useState(false) const [locationKey, setLocationKey] = React.useState() - const [theme, setTheme] = React.useState('light') + const [theme, setTheme] = useThemeState('light', 'theme') document.querySelector('#favicon').href = `favicon-${theme}.png`