@willbooster/shared-lib-react 3.3.17 → 3.4.0
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.
- package/dist/hooks/useLocalStorage.cjs.map +1 -1
- package/dist/hooks/useLocalStorage.d.ts +1 -1
- package/dist/hooks/useLocalStorage.js.map +1 -1
- package/dist/hooks/useSessionStorage.cjs.map +1 -1
- package/dist/hooks/useSessionStorage.d.ts +1 -1
- package/dist/hooks/useSessionStorage.js.map +1 -1
- package/dist/hooks/useStorage.cjs +1 -1
- package/dist/hooks/useStorage.cjs.map +1 -1
- package/dist/hooks/useStorage.d.ts +1 -1
- package/dist/hooks/useStorage.js +1 -1
- package/dist/hooks/useStorage.js.map +1 -1
- package/package.json +1 -1
|
@@ -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
|
|
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,
|
|
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
|
|
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
|
|
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,
|
|
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
|
|
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!==
|
|
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
|
|
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,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,MAAqC,mBAAvBnB,EAAqCA,IAAmCA,CAAkB,GACvG,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,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,GAC7D,GACC,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,
|
|
8
|
+
export declare function useStorage<T>(nonReactiveStorageType: 'localStorage' | 'sessionStorage', key: string, initialValueOrFunc: T | (() => T), nonReactiveOptions?: UseStorageOptions<T>): [T, React.Dispatch<React.SetStateAction<T>>];
|
package/dist/hooks/useStorage.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{useSyncExternalStore as e,useMemo as t,useCallback as n,useEffect as
|
|
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
|
|
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,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,MAAqC,mBAAvBnB,EAAqCA,IAAmCA,CAAkB,GACvG,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,IAWR,OARAiB,GAAU,KACR,MAAMC,EAC0B,mBAAvB/B,EAAqCA,IAAmCA,EAC7B,OAAhDU,OAAOZ,GAAwBa,QAAQZ,SAA0CiC,IAAzBD,GAC1DrB,OAAOZ,GAAwB6B,QAAQ5B,EAAKiB,KAAKU,UAAUK,GAC7D,GACC,CAAChC,EAAKC,IAEF,CAACa,EAAOO,EACjB"}
|