feat(fs.dev): Added theme support
This commit is contained in:
parent
84ffe26a76
commit
e1ecd0cb98
26 changed files with 834 additions and 96 deletions
32
packages/freesewing.shared/hooks/useLocalStorage.js
Normal file
32
packages/freesewing.shared/hooks/useLocalStorage.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
import { useState } from 'react'
|
||||
|
||||
// See: https://usehooks.com/useLocalStorage/
|
||||
|
||||
function useLocalStorage(key, initialValue) {
|
||||
const prefix = 'fs_'
|
||||
const [storedValue, setStoredValue] = useState(() => {
|
||||
if (typeof window === 'undefined') return initialValue // SSR has no window object
|
||||
try {
|
||||
const item = window.localStorage.getItem(prefix + key)
|
||||
return item ? JSON.parse(item) : initialValue
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return initialValue
|
||||
}
|
||||
})
|
||||
|
||||
const setValue = (value) => {
|
||||
if (typeof window === 'undefined') return null // SSR has no window object
|
||||
try {
|
||||
const valueToStore = value instanceof Function ? value(storedValue) : value
|
||||
setStoredValue(valueToStore)
|
||||
window.localStorage.setItem(prefix + key, JSON.stringify(valueToStore))
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
||||
return [storedValue, setValue]
|
||||
}
|
||||
|
||||
export default useLocalStorage
|
Loading…
Add table
Add a link
Reference in a new issue