feat: added prefers-color-scheme support

+ read media query for theme preference
+ improved typing of useLocalStorage hook
This commit is contained in:
Sagnik Pradhan
2021-10-19 14:46:18 -04:00
committed by Jean-Philippe Sirois
parent 32c12cba77
commit 6b267780b5
2 changed files with 42 additions and 9 deletions
+7 -3
View File
@@ -1,9 +1,13 @@
/* globals window */
import { useEffect, useState } from "react"
export const useLocalStorage = (name: string, initialValue: string) => {
export const useLocalStorage = <Value extends unknown = unknown>(
name: string,
initialValue: Value
) => {
const windowGlobal = typeof window !== "undefined" && window
const [value, setValue] = useState(() => {
const [value, setValue] = useState<Value>(() => {
if (windowGlobal) {
const currentValue = windowGlobal.localStorage.getItem(name)
return currentValue ? JSON.parse(currentValue) : initialValue
@@ -17,5 +21,5 @@ export const useLocalStorage = (name: string, initialValue: string) => {
}
}, [name, value, windowGlobal])
return [value, setValue]
return [value, setValue] as const
}