Skip to content

Commit

Permalink
feat: add darkmode persist
Browse files Browse the repository at this point in the history
  • Loading branch information
yencolon committed Oct 17, 2020
1 parent c83e874 commit e1af15e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { h } from 'preact';
import { useState} from 'preact/hooks'
import Home from './components/home'
import { useDarkModeState } from './hooks/useDarkMode'
import './styles/main.css'

function App() {
const [isDark, setIsDark] = useState(false)
const [isDark, setIsDark] = useDarkModeState()

const onChangeTheme = (arg) => {
setIsDark(arg)
setIsDark(arg)
}

return (
Expand Down
17 changes: 17 additions & 0 deletions src/hooks/useDarkMode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useState, useEffect } from "preact/hooks"

const DARK = 'dark'
const LIGHT = 'ligth'

export const useDarkModeState = () => {
const [isDark, setIsDark] = useState(() => {
const mode = localStorage.getItem('themeMode', LIGHT)
return mode === 'dark'
});

useEffect(() => {
localStorage.setItem('themeMode', isDark ? DARK : LIGHT)
}, [isDark])

return [isDark, setIsDark]
}

0 comments on commit e1af15e

Please sign in to comment.