@willbooster/shared-lib-react 3.3.17 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"useInterval.cjs","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timer>(undefined);\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (milliseconds !== undefined) {\n timerId.current = globalThis.setInterval(() => callbackRef.current(), milliseconds);\n }\n return clearInterval;\n }, [milliseconds]);\n\n return clearInterval;\n}\n"],"names":["callback","milliseconds","timerId","useRef","undefined","callbackRef","current","clearInterval","useCallback","globalThis","useEffect","setInterval"],"mappings":"wDAoBO,SAAqBA,EAAsBC,GAChD,MAAMC,EAAUC,EAAMA,YAAwBC,GACxCC,EAAcF,EAAMA,OAACH,GAC3BK,EAAYC,QAAUN,EAEtB,MAAMO,EAAgBC,EAAAA,aAAY,KAChCC,WAAWF,cAAcL,EAAQI,QAAQ,GACxC,IASH,OAPAI,EAAAA,WAAU,UACaN,IAAjBH,IACFC,EAAQI,QAAUG,WAAWE,aAAY,IAAMN,EAAYC,WAAWL,IAEjEM,IACN,CAACN,IAEGM,CACT"}
1
+ {"version":3,"file":"useInterval.cjs","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timer>(undefined);\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (milliseconds !== undefined) {\n timerId.current = globalThis.setInterval(() => callbackRef.current(), milliseconds);\n }\n return clearInterval;\n }, [milliseconds]);\n\n return clearInterval;\n}\n"],"names":["callback","milliseconds","timerId","useRef","undefined","callbackRef","current","clearInterval","useCallback","globalThis","useEffect","setInterval"],"mappings":"wDAoBO,SAAqBA,EAAsBC,GAChD,MAAMC,EAAUC,EAAMA,YAAwBC,GACxCC,EAAcF,EAAMA,OAACH,GAC3BK,EAAYC,QAAUN,EAEtB,MAAMO,EAAgBC,EAAAA,aAAY,KAChCC,WAAWF,cAAcL,EAAQI,WAChC,IASH,OAPAI,EAAAA,WAAU,UACaN,IAAjBH,IACFC,EAAQI,QAAUG,WAAWE,aAAY,IAAMN,EAAYC,WAAWL,IAEjEM,IACN,CAACN,IAEGM,CACT"}
@@ -1 +1 @@
1
- {"version":3,"file":"useInterval.js","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timer>(undefined);\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (milliseconds !== undefined) {\n timerId.current = globalThis.setInterval(() => callbackRef.current(), milliseconds);\n }\n return clearInterval;\n }, [milliseconds]);\n\n return clearInterval;\n}\n"],"names":["useInterval","callback","milliseconds","timerId","useRef","undefined","callbackRef","current","clearInterval","useCallback","globalThis","useEffect","setInterval"],"mappings":"+DAoBO,SAASA,EAAYC,EAAsBC,GAChD,MAAMC,EAAUC,OAA8BC,GACxCC,EAAcF,EAAOH,GAC3BK,EAAYC,QAAUN,EAEtB,MAAMO,EAAgBC,GAAY,KAChCC,WAAWF,cAAcL,EAAQI,QAAQ,GACxC,IASH,OAPAI,GAAU,UACaN,IAAjBH,IACFC,EAAQI,QAAUG,WAAWE,aAAY,IAAMN,EAAYC,WAAWL,IAEjEM,IACN,CAACN,IAEGM,CACT"}
1
+ {"version":3,"file":"useInterval.js","sources":["../../src/hooks/useInterval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\n/**\n * A custom React hook that sets up an interval and returns a function to clear it.\n *\n * This hook ensures that the latest callback is always used, even if it changes between renders.\n * The interval can be dynamically started, stopped, or adjusted by changing the `milliseconds` parameter.\n *\n * @param callback - The function to be called at each interval. It is guaranteed to be the most recent version.\n * @param milliseconds - The interval duration in milliseconds. If undefined, the interval is cleared.\n * @returns A function to manually clear the interval.\n *\n * @example\n * const clearInterval = useInterval(() => {\n * console.log('This will run every 1000ms');\n * }, 1000);\n *\n * // To manually stop the interval:\n * clearInterval();\n */\nexport function useInterval(callback: () => void, milliseconds?: number): () => void {\n const timerId = useRef<number | NodeJS.Timer>(undefined);\n const callbackRef = useRef(callback);\n callbackRef.current = callback;\n\n const clearInterval = useCallback(() => {\n globalThis.clearInterval(timerId.current);\n }, []);\n\n useEffect(() => {\n if (milliseconds !== undefined) {\n timerId.current = globalThis.setInterval(() => callbackRef.current(), milliseconds);\n }\n return clearInterval;\n }, [milliseconds]);\n\n return clearInterval;\n}\n"],"names":["useInterval","callback","milliseconds","timerId","useRef","undefined","callbackRef","current","clearInterval","useCallback","globalThis","useEffect","setInterval"],"mappings":"+DAoBO,SAASA,EAAYC,EAAsBC,GAChD,MAAMC,EAAUC,OAA8BC,GACxCC,EAAcF,EAAOH,GAC3BK,EAAYC,QAAUN,EAEtB,MAAMO,EAAgBC,GAAY,KAChCC,WAAWF,cAAcL,EAAQI,WAChC,IASH,OAPAI,GAAU,UACaN,IAAjBH,IACFC,EAAQI,QAAUG,WAAWE,aAAY,IAAMN,EAAYC,WAAWL,IAEjEM,IACN,CAACN,IAEGM,CACT"}
@@ -1 +1 @@
1
- {"version":3,"file":"useLocalStorage.cjs","sources":["../../src/hooks/useLocalStorage.ts"],"sourcesContent":["import type React from 'react';\n\nimport type { UseStorageOptions } from './useStorage.js';\nimport { useStorage } from './useStorage.js';\n\nexport function useLocalStorage<T>(\n key: string,\n initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n return useStorage('localStorage', key, initialValue, nonReactiveOptions);\n}\n"],"names":["key","initialValue","nonReactiveOptions","useStorage"],"mappings":"uEAKO,SACLA,EACAC,EACAC,EAA2C,CAAA,GAE3C,OAAOC,EAAUA,WAAC,eAAgBH,EAAKC,EAAcC,EACvD"}
1
+ {"version":3,"file":"useLocalStorage.cjs","sources":["../../src/hooks/useLocalStorage.ts"],"sourcesContent":["import type React from 'react';\n\nimport type { UseStorageOptions } from './useStorage.js';\nimport { useStorage } from './useStorage.js';\n\nexport function useLocalStorage<T>(\n key: string,\n initialValueOrFunc: T | (() => T),\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n return useStorage('localStorage', key, initialValueOrFunc, nonReactiveOptions);\n}\n"],"names":["key","initialValueOrFunc","nonReactiveOptions","useStorage"],"mappings":"uEAKO,SACLA,EACAC,EACAC,EAA2C,CAAA,GAE3C,OAAOC,EAAUA,WAAC,eAAgBH,EAAKC,EAAoBC,EAC7D"}
@@ -1,3 +1,3 @@
1
1
  import type React from 'react';
2
2
  import type { UseStorageOptions } from './useStorage.js';
3
- export declare function useLocalStorage<T>(key: string, initialValue: T, nonReactiveOptions?: UseStorageOptions<T>): [T, React.Dispatch<React.SetStateAction<T>>];
3
+ export declare function useLocalStorage<T>(key: string, initialValueOrFunc: T | (() => T), nonReactiveOptions?: UseStorageOptions<T>): [T, React.Dispatch<React.SetStateAction<T>>];
@@ -1 +1 @@
1
- {"version":3,"file":"useLocalStorage.js","sources":["../../src/hooks/useLocalStorage.ts"],"sourcesContent":["import type React from 'react';\n\nimport type { UseStorageOptions } from './useStorage.js';\nimport { useStorage } from './useStorage.js';\n\nexport function useLocalStorage<T>(\n key: string,\n initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n return useStorage('localStorage', key, initialValue, nonReactiveOptions);\n}\n"],"names":["useLocalStorage","key","initialValue","nonReactiveOptions","useStorage"],"mappings":"6CAKO,SAASA,EACdC,EACAC,EACAC,EAA2C,CAAA,GAE3C,OAAOC,EAAW,eAAgBH,EAAKC,EAAcC,EACvD"}
1
+ {"version":3,"file":"useLocalStorage.js","sources":["../../src/hooks/useLocalStorage.ts"],"sourcesContent":["import type React from 'react';\n\nimport type { UseStorageOptions } from './useStorage.js';\nimport { useStorage } from './useStorage.js';\n\nexport function useLocalStorage<T>(\n key: string,\n initialValueOrFunc: T | (() => T),\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n return useStorage('localStorage', key, initialValueOrFunc, nonReactiveOptions);\n}\n"],"names":["useLocalStorage","key","initialValueOrFunc","nonReactiveOptions","useStorage"],"mappings":"6CAKO,SAASA,EACdC,EACAC,EACAC,EAA2C,CAAA,GAE3C,OAAOC,EAAW,eAAgBH,EAAKC,EAAoBC,EAC7D"}
@@ -1 +1 @@
1
- {"version":3,"file":"useSessionStorage.cjs","sources":["../../src/hooks/useSessionStorage.ts"],"sourcesContent":["import type React from 'react';\n\nimport type { UseStorageOptions } from './useStorage.js';\nimport { useStorage } from './useStorage.js';\n\nexport function useSessionStorage<T>(\n key: string,\n initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n return useStorage('sessionStorage', key, initialValue, nonReactiveOptions);\n}\n"],"names":["key","initialValue","nonReactiveOptions","useStorage"],"mappings":"yEAKO,SACLA,EACAC,EACAC,EAA2C,CAAA,GAE3C,OAAOC,EAAUA,WAAC,iBAAkBH,EAAKC,EAAcC,EACzD"}
1
+ {"version":3,"file":"useSessionStorage.cjs","sources":["../../src/hooks/useSessionStorage.ts"],"sourcesContent":["import type React from 'react';\n\nimport type { UseStorageOptions } from './useStorage.js';\nimport { useStorage } from './useStorage.js';\n\nexport function useSessionStorage<T>(\n key: string,\n initialValueOrFunc: T | (() => T),\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n return useStorage('sessionStorage', key, initialValueOrFunc, nonReactiveOptions);\n}\n"],"names":["key","initialValueOrFunc","nonReactiveOptions","useStorage"],"mappings":"yEAKO,SACLA,EACAC,EACAC,EAA2C,CAAA,GAE3C,OAAOC,EAAUA,WAAC,iBAAkBH,EAAKC,EAAoBC,EAC/D"}
@@ -1,3 +1,3 @@
1
1
  import type React from 'react';
2
2
  import type { UseStorageOptions } from './useStorage.js';
3
- export declare function useSessionStorage<T>(key: string, initialValue: T, nonReactiveOptions?: UseStorageOptions<T>): [T, React.Dispatch<React.SetStateAction<T>>];
3
+ export declare function useSessionStorage<T>(key: string, initialValueOrFunc: T | (() => T), nonReactiveOptions?: UseStorageOptions<T>): [T, React.Dispatch<React.SetStateAction<T>>];
@@ -1 +1 @@
1
- {"version":3,"file":"useSessionStorage.js","sources":["../../src/hooks/useSessionStorage.ts"],"sourcesContent":["import type React from 'react';\n\nimport type { UseStorageOptions } from './useStorage.js';\nimport { useStorage } from './useStorage.js';\n\nexport function useSessionStorage<T>(\n key: string,\n initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n return useStorage('sessionStorage', key, initialValue, nonReactiveOptions);\n}\n"],"names":["useSessionStorage","key","initialValue","nonReactiveOptions","useStorage"],"mappings":"6CAKO,SAASA,EACdC,EACAC,EACAC,EAA2C,CAAA,GAE3C,OAAOC,EAAW,iBAAkBH,EAAKC,EAAcC,EACzD"}
1
+ {"version":3,"file":"useSessionStorage.js","sources":["../../src/hooks/useSessionStorage.ts"],"sourcesContent":["import type React from 'react';\n\nimport type { UseStorageOptions } from './useStorage.js';\nimport { useStorage } from './useStorage.js';\n\nexport function useSessionStorage<T>(\n key: string,\n initialValueOrFunc: T | (() => T),\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n return useStorage('sessionStorage', key, initialValueOrFunc, nonReactiveOptions);\n}\n"],"names":["useSessionStorage","key","initialValueOrFunc","nonReactiveOptions","useStorage"],"mappings":"6CAKO,SAASA,EACdC,EACAC,EACAC,EAA2C,CAAA,GAE3C,OAAOC,EAAW,iBAAkBH,EAAKC,EAAoBC,EAC/D"}
@@ -1,2 +1,2 @@
1
- "use strict";var e=require("react");exports.useStorage=function(t,n,s,r={}){const o=e.useSyncExternalStore((e=>{const t=t=>{t.key===n&&e()};return globalThis.addEventListener("storage",t),()=>globalThis.removeEventListener("storage",t)}),(()=>window[t].getItem(n)),(()=>"ssrJsonText"in r&&r.ssrJsonText)),i=e.useMemo((()=>{try{if(o){const e=JSON.parse(o);return null!=r&&r.parseAfterJsonParse?null==r?void 0:r.parseAfterJsonParse(e):e}}catch(e){}return s}),[o,s]),a=e.useCallback((e=>{const s="function"==typeof e?e(i):e;let r=null;null==s?window[t].removeItem(n):(r=JSON.stringify(s),window[t].setItem(n,r)),globalThis.dispatchEvent(new StorageEvent("storage",{key:n,newValue:r}))}),[n,i]);return e.useEffect((()=>{null===window[t].getItem(n)&&void 0!==s&&window[t].setItem(n,JSON.stringify(s))}),[n,s]),[i,a]};
1
+ "use strict";var e=require("react");exports.useStorage=function(t,n,s,r={}){const o=e.useSyncExternalStore((e=>{const t=t=>{t.key===n&&e()};return globalThis.addEventListener("storage",t),()=>globalThis.removeEventListener("storage",t)}),(()=>window[t].getItem(n)),(()=>"ssrJsonText"in r&&r.ssrJsonText)),i=e.useMemo((()=>{try{if(o){const e=JSON.parse(o);return null!=r&&r.parseAfterJsonParse?null==r?void 0:r.parseAfterJsonParse(e):e}}catch(e){}return"function"==typeof s?s():s}),[o,s]),a=e.useCallback((e=>{const s="function"==typeof e?e(i):e;let r=null;null==s?window[t].removeItem(n):(r=JSON.stringify(s),window[t].setItem(n,r)),globalThis.dispatchEvent(new StorageEvent("storage",{key:n,newValue:r}))}),[n,i]);return e.useEffect((()=>{const e="function"==typeof s?s():s;null===window[t].getItem(n)&&void 0!==e&&window[t].setItem(n,JSON.stringify(e))}),[n,s]),[i,a]};
2
2
  //# sourceMappingURL=useStorage.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"useStorage.cjs","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => globalThis.removeEventListener('storage', newCallback);\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText) as unknown as T;\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return initialValue;\n }, [jsonText, initialValue]);\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n // eslint-disable-next-line unicorn/no-null\n let newValue: string | null = null;\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n newValue = JSON.stringify(nextState);\n window[nonReactiveStorageType].setItem(key, newValue);\n }\n globalThis.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n\n [key, value]\n );\n\n useEffect(() => {\n if (window[nonReactiveStorageType].getItem(key) === null && initialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(initialValue));\n }\n }, [key, initialValue]);\n\n return [value, setState];\n}\n"],"names":["nonReactiveStorageType","key","initialValue","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","undefined"],"mappings":"uDAUO,SACLA,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,EAAoBA,sBAClCC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,GAAU,EAGnC,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,IAAME,WAAWE,oBAAoB,UAAWJ,EAAY,IAErE,IAAMK,OAAOZ,GAAwBa,QAAQZ,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,cAE5DC,EAAQC,EAAAA,SAAQ,KACpB,IACE,GAAIZ,EAAU,CACZ,MAAMa,EAAOC,KAAKC,MAAMf,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBiB,oBAAsBjB,MAAAA,OAAAA,EAAAA,EAAoBiB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOnB,CAAY,GAClB,CAACE,EAAUF,IAERoB,EAAWC,EAAWA,aACzBC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFb,OAAOZ,GAAwB2B,WAAW1B,IAE1CyB,EAAWR,KAAKU,UAAUH,GAC1Bb,OAAOZ,GAAwB6B,QAAQ5B,EAAKyB,IAE9CjB,WAAWqB,cAAc,IAAIC,aAAa,UAAW,CAAE9B,MAAKyB,aAAY,GAG1E,CAACzB,EAAKc,IASR,OANAiB,EAAAA,WAAU,KAC4C,OAAhDpB,OAAOZ,GAAwBa,QAAQZ,SAAkCgC,IAAjB/B,GAC1DU,OAAOZ,GAAwB6B,QAAQ5B,EAAKiB,KAAKU,UAAU1B,GAC7D,GACC,CAACD,EAAKC,IAEF,CAACa,EAAOO,EACjB"}
1
+ {"version":3,"file":"useStorage.cjs","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValueOrFunc: T | (() => T),\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => globalThis.removeEventListener('storage', newCallback);\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText) as unknown as T;\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n }, [jsonText, initialValueOrFunc]);\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n // eslint-disable-next-line unicorn/no-null\n let newValue: string | null = null;\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n newValue = JSON.stringify(nextState);\n window[nonReactiveStorageType].setItem(key, newValue);\n }\n globalThis.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n\n [key, value]\n );\n\n useEffect(() => {\n const resolvedInitialValue =\n typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n if (window[nonReactiveStorageType].getItem(key) === null && resolvedInitialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(resolvedInitialValue));\n }\n }, [key, initialValueOrFunc]);\n\n return [value, setState];\n}\n"],"names":["nonReactiveStorageType","key","initialValueOrFunc","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","resolvedInitialValue","undefined"],"mappings":"uDAUO,SACLA,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,EAAoBA,sBAClCC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,KAGzB,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,IAAME,WAAWE,oBAAoB,UAAWJ,MAEzD,IAAMK,OAAOZ,GAAwBa,QAAQZ,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,cAE5DC,EAAQC,EAAAA,SAAQ,KACpB,IACE,GAAIZ,EAAU,CACZ,MAAMa,EAAOC,KAAKC,MAAMf,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBiB,oBAAsBjB,MAAAA,OAAAA,EAAAA,EAAoBiB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,MAAqC,mBAAvBnB,EAAqCA,IAAmCA,IACrF,CAACE,EAAUF,IAERoB,EAAWC,EAAWA,aACzBC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFb,OAAOZ,GAAwB2B,WAAW1B,IAE1CyB,EAAWR,KAAKU,UAAUH,GAC1Bb,OAAOZ,GAAwB6B,QAAQ5B,EAAKyB,IAE9CjB,WAAWqB,cAAc,IAAIC,aAAa,UAAW,CAAE9B,MAAKyB,gBAG9D,CAACzB,EAAKc,IAWR,OARAiB,EAAAA,WAAU,KACR,MAAMC,EAC0B,mBAAvB/B,EAAqCA,IAAmCA,EAC7B,OAAhDU,OAAOZ,GAAwBa,QAAQZ,SAA0CiC,IAAzBD,GAC1DrB,OAAOZ,GAAwB6B,QAAQ5B,EAAKiB,KAAKU,UAAUK,MAE5D,CAAChC,EAAKC,IAEF,CAACa,EAAOO,EACjB"}
@@ -5,4 +5,4 @@ export type UseStorageOptions<T> = {
5
5
  } | {
6
6
  parseAfterJsonParse?: (value: unknown) => T;
7
7
  };
8
- export declare function useStorage<T>(nonReactiveStorageType: 'localStorage' | 'sessionStorage', key: string, initialValue: T, nonReactiveOptions?: UseStorageOptions<T>): [T, React.Dispatch<React.SetStateAction<T>>];
8
+ export declare function useStorage<T>(nonReactiveStorageType: 'localStorage' | 'sessionStorage', key: string, initialValueOrFunc: T | (() => T), nonReactiveOptions?: UseStorageOptions<T>): [T, React.Dispatch<React.SetStateAction<T>>];
@@ -1,2 +1,2 @@
1
- import{useSyncExternalStore as e,useMemo as t,useCallback as n,useEffect as r}from"react";function o(o,s,i,l={}){const a=e((e=>{const t=t=>{t.key===s&&e()};return globalThis.addEventListener("storage",t),()=>globalThis.removeEventListener("storage",t)}),(()=>window[o].getItem(s)),(()=>"ssrJsonText"in l&&l.ssrJsonText)),u=t((()=>{try{if(a){const e=JSON.parse(a);return null!=l&&l.parseAfterJsonParse?null==l?void 0:l.parseAfterJsonParse(e):e}}catch(e){}return i}),[a,i]),w=n((e=>{const t="function"==typeof e?e(u):e;let n=null;null==t?window[o].removeItem(s):(n=JSON.stringify(t),window[o].setItem(s,n)),globalThis.dispatchEvent(new StorageEvent("storage",{key:s,newValue:n}))}),[s,u]);return r((()=>{null===window[o].getItem(s)&&void 0!==i&&window[o].setItem(s,JSON.stringify(i))}),[s,i]),[u,w]}export{o as useStorage};
1
+ import{useSyncExternalStore as e,useMemo as t,useCallback as n,useEffect as o}from"react";function r(r,s,i,l={}){const a=e((e=>{const t=t=>{t.key===s&&e()};return globalThis.addEventListener("storage",t),()=>globalThis.removeEventListener("storage",t)}),(()=>window[r].getItem(s)),(()=>"ssrJsonText"in l&&l.ssrJsonText)),u=t((()=>{try{if(a){const e=JSON.parse(a);return null!=l&&l.parseAfterJsonParse?null==l?void 0:l.parseAfterJsonParse(e):e}}catch(e){}return"function"==typeof i?i():i}),[a,i]),c=n((e=>{const t="function"==typeof e?e(u):e;let n=null;null==t?window[r].removeItem(s):(n=JSON.stringify(t),window[r].setItem(s,n)),globalThis.dispatchEvent(new StorageEvent("storage",{key:s,newValue:n}))}),[s,u]);return o((()=>{const e="function"==typeof i?i():i;null===window[r].getItem(s)&&void 0!==e&&window[r].setItem(s,JSON.stringify(e))}),[s,i]),[u,c]}export{r as useStorage};
2
2
  //# sourceMappingURL=useStorage.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"useStorage.js","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValue: T,\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => globalThis.removeEventListener('storage', newCallback);\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText) as unknown as T;\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return initialValue;\n }, [jsonText, initialValue]);\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n // eslint-disable-next-line unicorn/no-null\n let newValue: string | null = null;\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n newValue = JSON.stringify(nextState);\n window[nonReactiveStorageType].setItem(key, newValue);\n }\n globalThis.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n\n [key, value]\n );\n\n useEffect(() => {\n if (window[nonReactiveStorageType].getItem(key) === null && initialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(initialValue));\n }\n }, [key, initialValue]);\n\n return [value, setState];\n}\n"],"names":["useStorage","nonReactiveStorageType","key","initialValue","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","undefined"],"mappings":"0FAUO,SAASA,EACdC,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,GACdC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,GAAU,EAGnC,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,IAAME,WAAWE,oBAAoB,UAAWJ,EAAY,IAErE,IAAMK,OAAOZ,GAAwBa,QAAQZ,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,cAE5DC,EAAQC,GAAQ,KACpB,IACE,GAAIZ,EAAU,CACZ,MAAMa,EAAOC,KAAKC,MAAMf,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBiB,oBAAsBjB,MAAAA,OAAAA,EAAAA,EAAoBiB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,OAAOnB,CAAY,GAClB,CAACE,EAAUF,IAERoB,EAAWC,GACdC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFb,OAAOZ,GAAwB2B,WAAW1B,IAE1CyB,EAAWR,KAAKU,UAAUH,GAC1Bb,OAAOZ,GAAwB6B,QAAQ5B,EAAKyB,IAE9CjB,WAAWqB,cAAc,IAAIC,aAAa,UAAW,CAAE9B,MAAKyB,aAAY,GAG1E,CAACzB,EAAKc,IASR,OANAiB,GAAU,KAC4C,OAAhDpB,OAAOZ,GAAwBa,QAAQZ,SAAkCgC,IAAjB/B,GAC1DU,OAAOZ,GAAwB6B,QAAQ5B,EAAKiB,KAAKU,UAAU1B,GAC7D,GACC,CAACD,EAAKC,IAEF,CAACa,EAAOO,EACjB"}
1
+ {"version":3,"file":"useStorage.js","sources":["../../src/hooks/useStorage.ts"],"sourcesContent":["import type React from 'react';\nimport { useMemo, useCallback, useEffect, useSyncExternalStore } from 'react';\n\nexport type UseStorageOptions<T> =\n | {\n parseAfterJsonParse?: (value: unknown) => T;\n ssrJsonText: string;\n }\n | { parseAfterJsonParse?: (value: unknown) => T };\n\nexport function useStorage<T>(\n nonReactiveStorageType: 'localStorage' | 'sessionStorage',\n key: string,\n initialValueOrFunc: T | (() => T),\n nonReactiveOptions: UseStorageOptions<T> = {}\n): [T, React.Dispatch<React.SetStateAction<T>>] {\n const jsonText = useSyncExternalStore(\n (callback) => {\n const newCallback = (event: StorageEvent): void => {\n if (event.key === key) callback();\n };\n globalThis.addEventListener('storage', newCallback);\n return () => globalThis.removeEventListener('storage', newCallback);\n },\n () => window[nonReactiveStorageType].getItem(key),\n () => 'ssrJsonText' in nonReactiveOptions && nonReactiveOptions.ssrJsonText\n );\n const value = useMemo(() => {\n try {\n if (jsonText) {\n const json = JSON.parse(jsonText) as unknown as T;\n return nonReactiveOptions?.parseAfterJsonParse ? nonReactiveOptions?.parseAfterJsonParse(json) : json;\n }\n } catch {\n // do nothing\n }\n return typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n }, [jsonText, initialValueOrFunc]);\n\n const setState = useCallback(\n (valueOrFunc: T | ((prevState: T) => T)) => {\n const nextState = typeof valueOrFunc === 'function' ? (valueOrFunc as (prevState: T) => T)(value) : valueOrFunc;\n\n // eslint-disable-next-line unicorn/no-null\n let newValue: string | null = null;\n if (nextState === undefined || nextState === null) {\n window[nonReactiveStorageType].removeItem(key);\n } else {\n newValue = JSON.stringify(nextState);\n window[nonReactiveStorageType].setItem(key, newValue);\n }\n globalThis.dispatchEvent(new StorageEvent('storage', { key, newValue }));\n },\n\n [key, value]\n );\n\n useEffect(() => {\n const resolvedInitialValue =\n typeof initialValueOrFunc === 'function' ? (initialValueOrFunc as () => T)() : initialValueOrFunc;\n if (window[nonReactiveStorageType].getItem(key) === null && resolvedInitialValue !== undefined) {\n window[nonReactiveStorageType].setItem(key, JSON.stringify(resolvedInitialValue));\n }\n }, [key, initialValueOrFunc]);\n\n return [value, setState];\n}\n"],"names":["useStorage","nonReactiveStorageType","key","initialValueOrFunc","nonReactiveOptions","jsonText","useSyncExternalStore","callback","newCallback","event","globalThis","addEventListener","removeEventListener","window","getItem","ssrJsonText","value","useMemo","json","JSON","parse","parseAfterJsonParse","_unused","setState","useCallback","valueOrFunc","nextState","newValue","removeItem","stringify","setItem","dispatchEvent","StorageEvent","useEffect","resolvedInitialValue","undefined"],"mappings":"0FAUO,SAASA,EACdC,EACAC,EACAC,EACAC,EAA2C,CAAA,GAE3C,MAAMC,EAAWC,GACdC,IACC,MAAMC,EAAeC,IACfA,EAAMP,MAAQA,GAAKK,KAGzB,OADAG,WAAWC,iBAAiB,UAAWH,GAChC,IAAME,WAAWE,oBAAoB,UAAWJ,MAEzD,IAAMK,OAAOZ,GAAwBa,QAAQZ,KAC7C,IAAM,gBAAiBE,GAAsBA,EAAmBW,cAE5DC,EAAQC,GAAQ,KACpB,IACE,GAAIZ,EAAU,CACZ,MAAMa,EAAOC,KAAKC,MAAMf,GACxB,OAAyB,MAAlBD,GAAAA,EAAoBiB,oBAAsBjB,MAAAA,OAAAA,EAAAA,EAAoBiB,oBAAoBH,GAAQA,CACnG,CACD,CAAC,MAAAI,GACA,CAEF,MAAqC,mBAAvBnB,EAAqCA,IAAmCA,IACrF,CAACE,EAAUF,IAERoB,EAAWC,GACdC,IACC,MAAMC,EAAmC,mBAAhBD,EAA8BA,EAAoCT,GAASS,EAGpG,IAAIE,EAA0B,KAC1BD,QACFb,OAAOZ,GAAwB2B,WAAW1B,IAE1CyB,EAAWR,KAAKU,UAAUH,GAC1Bb,OAAOZ,GAAwB6B,QAAQ5B,EAAKyB,IAE9CjB,WAAWqB,cAAc,IAAIC,aAAa,UAAW,CAAE9B,MAAKyB,gBAG9D,CAACzB,EAAKc,IAWR,OARAiB,GAAU,KACR,MAAMC,EAC0B,mBAAvB/B,EAAqCA,IAAmCA,EAC7B,OAAhDU,OAAOZ,GAAwBa,QAAQZ,SAA0CiC,IAAzBD,GAC1DrB,OAAOZ,GAAwB6B,QAAQ5B,EAAKiB,KAAKU,UAAUK,MAE5D,CAAChC,EAAKC,IAEF,CAACa,EAAOO,EACjB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@willbooster/shared-lib-react",
3
- "version": "3.3.17",
3
+ "version": "3.4.1",
4
4
  "license": "Apache-2.0",
5
5
  "author": "WillBooster Inc.",
6
6
  "sideEffects": false,
@@ -36,33 +36,34 @@
36
36
  "devDependencies": {
37
37
  "@babel/core": "7.27.4",
38
38
  "@mdx-js/react": "3.1.0",
39
- "@storybook/addon-actions": "8.6.14",
40
- "@storybook/addon-docs": "8.6.14",
39
+ "@storybook/addon-actions": "9.0.6",
40
+ "@storybook/addon-docs": "9.0.6",
41
41
  "@storybook/addon-essentials": "8.6.14",
42
42
  "@storybook/addon-interactions": "8.6.14",
43
- "@storybook/addon-links": "8.6.14",
43
+ "@storybook/addon-links": "9.0.6",
44
44
  "@storybook/builder-webpack4": "6.5.16",
45
45
  "@storybook/manager-webpack4": "6.5.16",
46
- "@storybook/react": "8.6.14",
46
+ "@storybook/react": "9.0.6",
47
47
  "@storybook/testing-library": "0.2.2",
48
48
  "@types/eslint": "8.56.10",
49
49
  "@types/micromatch": "4.0.9",
50
50
  "@types/react": "19.1.6",
51
- "@types/react-dom": "19.1.5",
52
- "@willbooster/eslint-config-ts-react": "11.2.0",
51
+ "@types/react-dom": "19.1.6",
52
+ "@willbooster/eslint-config-ts-react": "11.3.0",
53
53
  "@willbooster/prettier-config": "10.0.0",
54
54
  "babel-loader": "10.0.0",
55
- "build-ts": "14.0.6",
56
- "eslint": "9.27.0",
55
+ "build-ts": "15.0.2",
56
+ "eslint": "9.28.0",
57
57
  "eslint-config-flat-gitignore": "2.1.0",
58
58
  "eslint-config-prettier": "10.1.5",
59
- "eslint-import-resolver-typescript": "4.4.2",
60
- "eslint-plugin-import-x": "4.14.1",
59
+ "eslint-import-resolver-typescript": "4.4.3",
60
+ "eslint-plugin-import-x": "4.15.1",
61
61
  "eslint-plugin-react": "7.37.5",
62
+ "eslint-plugin-react-compiler": "19.1.0-rc.2",
62
63
  "eslint-plugin-react-hooks": "5.2.0",
63
64
  "eslint-plugin-sort-class-members": "1.21.0",
64
65
  "eslint-plugin-sort-destructure-keys": "2.0.0",
65
- "eslint-plugin-storybook": "0.12.0",
66
+ "eslint-plugin-storybook": "9.0.6",
66
67
  "eslint-plugin-unicorn": "59.0.1",
67
68
  "eslint-plugin-unused-imports": "4.1.4",
68
69
  "globals": "16.2.0",
@@ -73,8 +74,8 @@
73
74
  "react-dom": "19.1.0",
74
75
  "sort-package-json": "3.2.1",
75
76
  "typescript": "5.8.3",
76
- "typescript-eslint": "8.33.0",
77
- "vitest": "3.1.4"
77
+ "typescript-eslint": "8.33.1",
78
+ "vitest": "3.2.2"
78
79
  },
79
80
  "peerDependencies": {
80
81
  "react": "~18.2.0 || ~18.3.0 || ~19.0.0 || ~19.1.0",