UNPKG

@arolariu/components

Version:

🎨 70+ beautiful, accessible React components built on Base UI. TypeScript-first, CSS Modules styling, tree-shakeable, SSR-ready. Perfect for modern web apps, design systems & rapid prototyping. Zero config, maximum flexibility! ⚡

48 lines (47 loc) • 1.74 kB
"use client"; import * as __rspack_external_react from "react"; function useLocalStorage(key, initialValue) { const [storedValue, setStoredValue] = __rspack_external_react.useState(()=>{ if (void 0 === globalThis.window) return initialValue; try { const item = globalThis.window.localStorage.getItem(key); return null === item ? initialValue : JSON.parse(item); } catch { return initialValue; } }); const setValue = __rspack_external_react.useCallback((value)=>{ try { setStoredValue((currentValue)=>{ const valueToStore = "function" == typeof value ? value(currentValue) : value; if (void 0 !== globalThis.window) globalThis.window.localStorage.setItem(key, JSON.stringify(valueToStore)); return valueToStore; }); } catch {} }, [ key ]); __rspack_external_react.useEffect(()=>{ if (void 0 === globalThis.window) return; const handleStorageChange = (event)=>{ if (event.key !== key || event.storageArea !== globalThis.window.localStorage) return; try { const newValue = null === event.newValue ? initialValue : JSON.parse(event.newValue); setStoredValue(newValue); } catch {} }; globalThis.window.addEventListener("storage", handleStorageChange); return ()=>{ globalThis.window.removeEventListener("storage", handleStorageChange); }; }, [ key, initialValue ]); return [ storedValue, setValue ]; } export { useLocalStorage }; //# sourceMappingURL=useLocalStorage.js.map