mirror of
https://github.com/Stone-Red-Code/website.git
synced 2026-09-06 16:06:12 +02:00
feat: added prefers-color-scheme support
+ read media query for theme preference + improved typing of useLocalStorage hook
This commit is contained in:
committed by
Jean-Philippe Sirois
parent
32c12cba77
commit
6b267780b5
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user