Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Persist theme in localStorage #94

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: Persist theme in localStorage
iamdarshshah committed May 25, 2020
commit 085572b1440222a25fa5870c484cfaa8b961d440
16 changes: 15 additions & 1 deletion packages/docs/src/App.js
Original file line number Diff line number Diff line change
@@ -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`