Files
docmost/apps/client/src/lib/jotai-helper.ts
T
Philip Okugbe b85b34d6b1 feat: resizable sidebar (#452)
* feat: resizable sidebar

* only expand space sidebar
2024-11-01 10:05:03 +00:00

18 lines
622 B
TypeScript

import { atom } from "jotai";
export function atomWithWebStorage<Value>(key: string, initialValue: Value, storage = localStorage) {
const storedValue = localStorage.getItem(key);
const isStringOrInt = typeof initialValue === "string" || typeof initialValue === "number";
const storageValue = storedValue ? isStringOrInt ? storedValue : storedValue === "true" : undefined;
const baseAtom = atom(storageValue ?? initialValue);
return atom(
get => get(baseAtom) as Value,
(_get, set, nextValue: Value) => {
set(baseAtom, nextValue);
storage.setItem(key, nextValue!.toString());
},
);
}