feat: implement dark mode

This commit is contained in:
Jean-Philippe Sirois
2019-09-14 15:47:10 -04:00
parent a9e74c108f
commit 5f76447e05
18 changed files with 266 additions and 98 deletions
+20
View File
@@ -0,0 +1,20 @@
import { useEffect, useState } from "react"
export const useLocalStorage = (name: string, initialValue: string) => {
const windowGlobal = typeof window !== "undefined" && window
const [value, setValue] = useState(() => {
if (windowGlobal) {
const currentValue = windowGlobal.localStorage.getItem(name)
return currentValue ? JSON.parse(currentValue) : initialValue
}
return initialValue
})
useEffect(() => {
if (windowGlobal) {
windowGlobal.localStorage.setItem(name, JSON.stringify(value))
}
}, [name, value, windowGlobal])
return [value, setValue]
}
+12
View File
@@ -0,0 +1,12 @@
import { useContext } from "react"
import { IThemeContext, ThemeContext } from "../ThemeProvider"
export default function useTheme(): IThemeContext {
const themeContext = useContext(ThemeContext)
if (!themeContext) {
throw Error("Need context")
}
return themeContext
}