@elementor/session 4.3.0-beta2 → 4.3.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/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/use-session-storage.ts +5 -1
package/dist/index.d.mts
CHANGED
|
@@ -5,7 +5,7 @@ declare const getSessionStorageItem: <T>(key: string) => T | undefined;
|
|
|
5
5
|
declare const setSessionStorageItem: (key: string, item: unknown) => void;
|
|
6
6
|
declare const removeSessionStorageItem: (key: string) => void;
|
|
7
7
|
|
|
8
|
-
declare const useSessionStorage: <T>(key: string, customPrefix?: string) => readonly [T | null
|
|
8
|
+
declare const useSessionStorage: <T>(key: string, customPrefix?: string) => readonly [T | null, (newValue: T) => void, () => void];
|
|
9
9
|
|
|
10
10
|
type ContextValue = {
|
|
11
11
|
prefix: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ declare const getSessionStorageItem: <T>(key: string) => T | undefined;
|
|
|
5
5
|
declare const setSessionStorageItem: (key: string, item: unknown) => void;
|
|
6
6
|
declare const removeSessionStorageItem: (key: string) => void;
|
|
7
7
|
|
|
8
|
-
declare const useSessionStorage: <T>(key: string, customPrefix?: string) => readonly [T | null
|
|
8
|
+
declare const useSessionStorage: <T>(key: string, customPrefix?: string) => readonly [T | null, (newValue: T) => void, () => void];
|
|
9
9
|
|
|
10
10
|
type ContextValue = {
|
|
11
11
|
prefix: string;
|
package/dist/index.js
CHANGED
|
@@ -80,7 +80,10 @@ var useSessionStorage = (key, customPrefix) => {
|
|
|
80
80
|
const contextPrefix = (0, import_react2.useContext)(Context)?.prefix ?? "";
|
|
81
81
|
const prefix = customPrefix ? customPrefix : contextPrefix;
|
|
82
82
|
const prefixedKey = `${prefix}/${key}`;
|
|
83
|
-
const [value, setValue] = (0, import_react2.useState)()
|
|
83
|
+
const [value, setValue] = (0, import_react2.useState)(() => {
|
|
84
|
+
const storedValue = getSessionStorageItem(prefixedKey);
|
|
85
|
+
return storedValue ?? null;
|
|
86
|
+
});
|
|
84
87
|
(0, import_react2.useEffect)(() => {
|
|
85
88
|
return subscribeToSessionStorage(prefixedKey, (newValue) => {
|
|
86
89
|
setValue(newValue ?? null);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/session-storage.ts","../src/use-session-storage.ts","../src/session-storage-context.tsx"],"sourcesContent":["export * from './session-storage';\nexport * from './use-session-storage';\nexport * from './session-storage-context';\n","export const getSessionStorageItem = < T >( key: string ): T | undefined => {\n\treturn JSON.parse( sessionStorage.getItem( key ) || '{}' )?.item;\n};\n\nexport const setSessionStorageItem = ( key: string, item: unknown ) => {\n\tsessionStorage.setItem( key, JSON.stringify( { item } ) );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n\nexport const removeSessionStorageItem = ( key: string ) => {\n\tsessionStorage.removeItem( key );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n","import { useContext, useEffect, useState } from 'react';\n\nimport { getSessionStorageItem, removeSessionStorageItem, setSessionStorageItem } from './session-storage';\nimport { Context } from './session-storage-context';\n\nexport const useSessionStorage = < T >( key: string, customPrefix?: string ) => {\n\tconst contextPrefix = useContext( Context )?.prefix ?? '';\n\tconst prefix = customPrefix ? customPrefix : contextPrefix;\n\tconst prefixedKey = `${ prefix }/${ key }`;\n\n\tconst [ value, setValue ] = useState< T | null >();\n\n\tuseEffect( () => {\n\t\treturn subscribeToSessionStorage< T | null >( prefixedKey, ( newValue ) => {\n\t\t\tsetValue( newValue ?? null );\n\t\t} );\n\t}, [ prefixedKey ] );\n\n\tconst saveValue = ( newValue: T ) => {\n\t\tsetSessionStorageItem( prefixedKey, newValue );\n\t};\n\n\tconst removeValue = () => {\n\t\tremoveSessionStorageItem( prefixedKey );\n\t};\n\n\treturn [ value, saveValue, removeValue ] as const;\n};\n\nconst subscribeToSessionStorage = < T >( key: string, subscriber: ( value: T ) => void ) => {\n\tsubscriber( getSessionStorageItem( key ) as T );\n\n\tconst abortController = new AbortController();\n\n\twindow.addEventListener(\n\t\t'storage',\n\t\t( e ) => {\n\t\t\tif ( e.key !== key || e.storageArea !== sessionStorage ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsubscriber( getSessionStorageItem( key ) as T );\n\t\t},\n\t\t{ signal: abortController.signal }\n\t);\n\n\treturn () => {\n\t\tabortController.abort();\n\t};\n};\n","import * as React from 'react';\nimport { createContext, type PropsWithChildren, useContext } from 'react';\n\ntype ContextValue = {\n\tprefix: string;\n};\n\nexport const Context = createContext< ContextValue | null >( null );\n\ntype Props = PropsWithChildren< ContextValue >;\n\nexport function SessionStorageProvider( { children, prefix }: Props ) {\n\tconst contextPrefix = useContext( Context )?.prefix ?? '';\n\tconst chainedPrefix = contextPrefix ? `${ contextPrefix }/${ prefix }` : prefix;\n\n\treturn <Context.Provider value={ { prefix: chainedPrefix } }>{ children }</Context.Provider>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,wBAAwB,CAAO,QAAgC;AAC3E,SAAO,KAAK,MAAO,eAAe,QAAS,GAAI,KAAK,IAAK,GAAG;AAC7D;AAEO,IAAM,wBAAwB,CAAE,KAAa,SAAmB;AACtE,iBAAe,QAAS,KAAK,KAAK,UAAW,EAAE,KAAK,CAAE,CAAE;AAMxD,SAAO;AAAA,IACN,IAAI,aAAc,WAAW;AAAA,MAC5B;AAAA,MACA,aAAa;AAAA,IACd,CAAE;AAAA,EACH;AACD;AAEO,IAAM,2BAA2B,CAAE,QAAiB;AAC1D,iBAAe,WAAY,GAAI;AAM/B,SAAO;AAAA,IACN,IAAI,aAAc,WAAW;AAAA,MAC5B;AAAA,MACA,aAAa;AAAA,IACd,CAAE;AAAA,EACH;AACD;;;AChCA,IAAAA,gBAAgD;;;ACAhD,YAAuB;AACvB,mBAAkE;AAM3D,IAAM,cAAU,4BAAsC,IAAK;AAI3D,SAAS,uBAAwB,EAAE,UAAU,OAAO,GAAW;AACrE,QAAM,oBAAgB,yBAAY,OAAQ,GAAG,UAAU;AACvD,QAAM,gBAAgB,gBAAgB,GAAI,aAAc,IAAK,MAAO,KAAK;AAEzE,SAAO,oCAAC,QAAQ,UAAR,EAAiB,OAAQ,EAAE,QAAQ,cAAc,KAAM,QAAU;AAC1E;;;ADXO,IAAM,oBAAoB,CAAO,KAAa,iBAA2B;AAC/E,QAAM,oBAAgB,0BAAY,OAAQ,GAAG,UAAU;AACvD,QAAM,SAAS,eAAe,eAAe;AAC7C,QAAM,cAAc,GAAI,MAAO,IAAK,GAAI;AAExC,QAAM,CAAE,OAAO,QAAS,QAAI,
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/session-storage.ts","../src/use-session-storage.ts","../src/session-storage-context.tsx"],"sourcesContent":["export * from './session-storage';\nexport * from './use-session-storage';\nexport * from './session-storage-context';\n","export const getSessionStorageItem = < T >( key: string ): T | undefined => {\n\treturn JSON.parse( sessionStorage.getItem( key ) || '{}' )?.item;\n};\n\nexport const setSessionStorageItem = ( key: string, item: unknown ) => {\n\tsessionStorage.setItem( key, JSON.stringify( { item } ) );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n\nexport const removeSessionStorageItem = ( key: string ) => {\n\tsessionStorage.removeItem( key );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n","import { useContext, useEffect, useState } from 'react';\n\nimport { getSessionStorageItem, removeSessionStorageItem, setSessionStorageItem } from './session-storage';\nimport { Context } from './session-storage-context';\n\nexport const useSessionStorage = < T >( key: string, customPrefix?: string ) => {\n\tconst contextPrefix = useContext( Context )?.prefix ?? '';\n\tconst prefix = customPrefix ? customPrefix : contextPrefix;\n\tconst prefixedKey = `${ prefix }/${ key }`;\n\n\tconst [ value, setValue ] = useState< T | null >( () => {\n\t\tconst storedValue = getSessionStorageItem( prefixedKey ) as T | undefined;\n\n\t\treturn storedValue ?? null;\n\t} );\n\n\tuseEffect( () => {\n\t\treturn subscribeToSessionStorage< T | null >( prefixedKey, ( newValue ) => {\n\t\t\tsetValue( newValue ?? null );\n\t\t} );\n\t}, [ prefixedKey ] );\n\n\tconst saveValue = ( newValue: T ) => {\n\t\tsetSessionStorageItem( prefixedKey, newValue );\n\t};\n\n\tconst removeValue = () => {\n\t\tremoveSessionStorageItem( prefixedKey );\n\t};\n\n\treturn [ value, saveValue, removeValue ] as const;\n};\n\nconst subscribeToSessionStorage = < T >( key: string, subscriber: ( value: T ) => void ) => {\n\tsubscriber( getSessionStorageItem( key ) as T );\n\n\tconst abortController = new AbortController();\n\n\twindow.addEventListener(\n\t\t'storage',\n\t\t( e ) => {\n\t\t\tif ( e.key !== key || e.storageArea !== sessionStorage ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsubscriber( getSessionStorageItem( key ) as T );\n\t\t},\n\t\t{ signal: abortController.signal }\n\t);\n\n\treturn () => {\n\t\tabortController.abort();\n\t};\n};\n","import * as React from 'react';\nimport { createContext, type PropsWithChildren, useContext } from 'react';\n\ntype ContextValue = {\n\tprefix: string;\n};\n\nexport const Context = createContext< ContextValue | null >( null );\n\ntype Props = PropsWithChildren< ContextValue >;\n\nexport function SessionStorageProvider( { children, prefix }: Props ) {\n\tconst contextPrefix = useContext( Context )?.prefix ?? '';\n\tconst chainedPrefix = contextPrefix ? `${ contextPrefix }/${ prefix }` : prefix;\n\n\treturn <Context.Provider value={ { prefix: chainedPrefix } }>{ children }</Context.Provider>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,wBAAwB,CAAO,QAAgC;AAC3E,SAAO,KAAK,MAAO,eAAe,QAAS,GAAI,KAAK,IAAK,GAAG;AAC7D;AAEO,IAAM,wBAAwB,CAAE,KAAa,SAAmB;AACtE,iBAAe,QAAS,KAAK,KAAK,UAAW,EAAE,KAAK,CAAE,CAAE;AAMxD,SAAO;AAAA,IACN,IAAI,aAAc,WAAW;AAAA,MAC5B;AAAA,MACA,aAAa;AAAA,IACd,CAAE;AAAA,EACH;AACD;AAEO,IAAM,2BAA2B,CAAE,QAAiB;AAC1D,iBAAe,WAAY,GAAI;AAM/B,SAAO;AAAA,IACN,IAAI,aAAc,WAAW;AAAA,MAC5B;AAAA,MACA,aAAa;AAAA,IACd,CAAE;AAAA,EACH;AACD;;;AChCA,IAAAA,gBAAgD;;;ACAhD,YAAuB;AACvB,mBAAkE;AAM3D,IAAM,cAAU,4BAAsC,IAAK;AAI3D,SAAS,uBAAwB,EAAE,UAAU,OAAO,GAAW;AACrE,QAAM,oBAAgB,yBAAY,OAAQ,GAAG,UAAU;AACvD,QAAM,gBAAgB,gBAAgB,GAAI,aAAc,IAAK,MAAO,KAAK;AAEzE,SAAO,oCAAC,QAAQ,UAAR,EAAiB,OAAQ,EAAE,QAAQ,cAAc,KAAM,QAAU;AAC1E;;;ADXO,IAAM,oBAAoB,CAAO,KAAa,iBAA2B;AAC/E,QAAM,oBAAgB,0BAAY,OAAQ,GAAG,UAAU;AACvD,QAAM,SAAS,eAAe,eAAe;AAC7C,QAAM,cAAc,GAAI,MAAO,IAAK,GAAI;AAExC,QAAM,CAAE,OAAO,QAAS,QAAI,wBAAsB,MAAM;AACvD,UAAM,cAAc,sBAAuB,WAAY;AAEvD,WAAO,eAAe;AAAA,EACvB,CAAE;AAEF,+BAAW,MAAM;AAChB,WAAO,0BAAuC,aAAa,CAAE,aAAc;AAC1E,eAAU,YAAY,IAAK;AAAA,IAC5B,CAAE;AAAA,EACH,GAAG,CAAE,WAAY,CAAE;AAEnB,QAAM,YAAY,CAAE,aAAiB;AACpC,0BAAuB,aAAa,QAAS;AAAA,EAC9C;AAEA,QAAM,cAAc,MAAM;AACzB,6BAA0B,WAAY;AAAA,EACvC;AAEA,SAAO,CAAE,OAAO,WAAW,WAAY;AACxC;AAEA,IAAM,4BAA4B,CAAO,KAAa,eAAsC;AAC3F,aAAY,sBAAuB,GAAI,CAAO;AAE9C,QAAM,kBAAkB,IAAI,gBAAgB;AAE5C,SAAO;AAAA,IACN;AAAA,IACA,CAAE,MAAO;AACR,UAAK,EAAE,QAAQ,OAAO,EAAE,gBAAgB,gBAAiB;AACxD;AAAA,MACD;AAEA,iBAAY,sBAAuB,GAAI,CAAO;AAAA,IAC/C;AAAA,IACA,EAAE,QAAQ,gBAAgB,OAAO;AAAA,EAClC;AAEA,SAAO,MAAM;AACZ,oBAAgB,MAAM;AAAA,EACvB;AACD;","names":["import_react"]}
|
package/dist/index.mjs
CHANGED
|
@@ -39,7 +39,10 @@ var useSessionStorage = (key, customPrefix) => {
|
|
|
39
39
|
const contextPrefix = useContext2(Context)?.prefix ?? "";
|
|
40
40
|
const prefix = customPrefix ? customPrefix : contextPrefix;
|
|
41
41
|
const prefixedKey = `${prefix}/${key}`;
|
|
42
|
-
const [value, setValue] = useState()
|
|
42
|
+
const [value, setValue] = useState(() => {
|
|
43
|
+
const storedValue = getSessionStorageItem(prefixedKey);
|
|
44
|
+
return storedValue ?? null;
|
|
45
|
+
});
|
|
43
46
|
useEffect(() => {
|
|
44
47
|
return subscribeToSessionStorage(prefixedKey, (newValue) => {
|
|
45
48
|
setValue(newValue ?? null);
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/session-storage.ts","../src/use-session-storage.ts","../src/session-storage-context.tsx"],"sourcesContent":["export const getSessionStorageItem = < T >( key: string ): T | undefined => {\n\treturn JSON.parse( sessionStorage.getItem( key ) || '{}' )?.item;\n};\n\nexport const setSessionStorageItem = ( key: string, item: unknown ) => {\n\tsessionStorage.setItem( key, JSON.stringify( { item } ) );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n\nexport const removeSessionStorageItem = ( key: string ) => {\n\tsessionStorage.removeItem( key );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n","import { useContext, useEffect, useState } from 'react';\n\nimport { getSessionStorageItem, removeSessionStorageItem, setSessionStorageItem } from './session-storage';\nimport { Context } from './session-storage-context';\n\nexport const useSessionStorage = < T >( key: string, customPrefix?: string ) => {\n\tconst contextPrefix = useContext( Context )?.prefix ?? '';\n\tconst prefix = customPrefix ? customPrefix : contextPrefix;\n\tconst prefixedKey = `${ prefix }/${ key }`;\n\n\tconst [ value, setValue ] = useState< T | null >();\n\n\tuseEffect( () => {\n\t\treturn subscribeToSessionStorage< T | null >( prefixedKey, ( newValue ) => {\n\t\t\tsetValue( newValue ?? null );\n\t\t} );\n\t}, [ prefixedKey ] );\n\n\tconst saveValue = ( newValue: T ) => {\n\t\tsetSessionStorageItem( prefixedKey, newValue );\n\t};\n\n\tconst removeValue = () => {\n\t\tremoveSessionStorageItem( prefixedKey );\n\t};\n\n\treturn [ value, saveValue, removeValue ] as const;\n};\n\nconst subscribeToSessionStorage = < T >( key: string, subscriber: ( value: T ) => void ) => {\n\tsubscriber( getSessionStorageItem( key ) as T );\n\n\tconst abortController = new AbortController();\n\n\twindow.addEventListener(\n\t\t'storage',\n\t\t( e ) => {\n\t\t\tif ( e.key !== key || e.storageArea !== sessionStorage ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsubscriber( getSessionStorageItem( key ) as T );\n\t\t},\n\t\t{ signal: abortController.signal }\n\t);\n\n\treturn () => {\n\t\tabortController.abort();\n\t};\n};\n","import * as React from 'react';\nimport { createContext, type PropsWithChildren, useContext } from 'react';\n\ntype ContextValue = {\n\tprefix: string;\n};\n\nexport const Context = createContext< ContextValue | null >( null );\n\ntype Props = PropsWithChildren< ContextValue >;\n\nexport function SessionStorageProvider( { children, prefix }: Props ) {\n\tconst contextPrefix = useContext( Context )?.prefix ?? '';\n\tconst chainedPrefix = contextPrefix ? `${ contextPrefix }/${ prefix }` : prefix;\n\n\treturn <Context.Provider value={ { prefix: chainedPrefix } }>{ children }</Context.Provider>;\n}\n"],"mappings":";AAAO,IAAM,wBAAwB,CAAO,QAAgC;AAC3E,SAAO,KAAK,MAAO,eAAe,QAAS,GAAI,KAAK,IAAK,GAAG;AAC7D;AAEO,IAAM,wBAAwB,CAAE,KAAa,SAAmB;AACtE,iBAAe,QAAS,KAAK,KAAK,UAAW,EAAE,KAAK,CAAE,CAAE;AAMxD,SAAO;AAAA,IACN,IAAI,aAAc,WAAW;AAAA,MAC5B;AAAA,MACA,aAAa;AAAA,IACd,CAAE;AAAA,EACH;AACD;AAEO,IAAM,2BAA2B,CAAE,QAAiB;AAC1D,iBAAe,WAAY,GAAI;AAM/B,SAAO;AAAA,IACN,IAAI,aAAc,WAAW;AAAA,MAC5B;AAAA,MACA,aAAa;AAAA,IACd,CAAE;AAAA,EACH;AACD;;;AChCA,SAAS,cAAAA,aAAY,WAAW,gBAAgB;;;ACAhD,YAAY,WAAW;AACvB,SAAS,eAAuC,kBAAkB;AAM3D,IAAM,UAAU,cAAsC,IAAK;AAI3D,SAAS,uBAAwB,EAAE,UAAU,OAAO,GAAW;AACrE,QAAM,gBAAgB,WAAY,OAAQ,GAAG,UAAU;AACvD,QAAM,gBAAgB,gBAAgB,GAAI,aAAc,IAAK,MAAO,KAAK;AAEzE,SAAO,oCAAC,QAAQ,UAAR,EAAiB,OAAQ,EAAE,QAAQ,cAAc,KAAM,QAAU;AAC1E;;;ADXO,IAAM,oBAAoB,CAAO,KAAa,iBAA2B;AAC/E,QAAM,gBAAgBC,YAAY,OAAQ,GAAG,UAAU;AACvD,QAAM,SAAS,eAAe,eAAe;AAC7C,QAAM,cAAc,GAAI,MAAO,IAAK,GAAI;AAExC,QAAM,CAAE,OAAO,QAAS,IAAI,
|
|
1
|
+
{"version":3,"sources":["../src/session-storage.ts","../src/use-session-storage.ts","../src/session-storage-context.tsx"],"sourcesContent":["export const getSessionStorageItem = < T >( key: string ): T | undefined => {\n\treturn JSON.parse( sessionStorage.getItem( key ) || '{}' )?.item;\n};\n\nexport const setSessionStorageItem = ( key: string, item: unknown ) => {\n\tsessionStorage.setItem( key, JSON.stringify( { item } ) );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n\nexport const removeSessionStorageItem = ( key: string ) => {\n\tsessionStorage.removeItem( key );\n\n\t// The browser doesn't dispatch the `storage` event for the current tab,\n\t// so we need to dispatch it manually.\n\t//\n\t// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event\n\twindow.dispatchEvent(\n\t\tnew StorageEvent( 'storage', {\n\t\t\tkey,\n\t\t\tstorageArea: sessionStorage,\n\t\t} )\n\t);\n};\n","import { useContext, useEffect, useState } from 'react';\n\nimport { getSessionStorageItem, removeSessionStorageItem, setSessionStorageItem } from './session-storage';\nimport { Context } from './session-storage-context';\n\nexport const useSessionStorage = < T >( key: string, customPrefix?: string ) => {\n\tconst contextPrefix = useContext( Context )?.prefix ?? '';\n\tconst prefix = customPrefix ? customPrefix : contextPrefix;\n\tconst prefixedKey = `${ prefix }/${ key }`;\n\n\tconst [ value, setValue ] = useState< T | null >( () => {\n\t\tconst storedValue = getSessionStorageItem( prefixedKey ) as T | undefined;\n\n\t\treturn storedValue ?? null;\n\t} );\n\n\tuseEffect( () => {\n\t\treturn subscribeToSessionStorage< T | null >( prefixedKey, ( newValue ) => {\n\t\t\tsetValue( newValue ?? null );\n\t\t} );\n\t}, [ prefixedKey ] );\n\n\tconst saveValue = ( newValue: T ) => {\n\t\tsetSessionStorageItem( prefixedKey, newValue );\n\t};\n\n\tconst removeValue = () => {\n\t\tremoveSessionStorageItem( prefixedKey );\n\t};\n\n\treturn [ value, saveValue, removeValue ] as const;\n};\n\nconst subscribeToSessionStorage = < T >( key: string, subscriber: ( value: T ) => void ) => {\n\tsubscriber( getSessionStorageItem( key ) as T );\n\n\tconst abortController = new AbortController();\n\n\twindow.addEventListener(\n\t\t'storage',\n\t\t( e ) => {\n\t\t\tif ( e.key !== key || e.storageArea !== sessionStorage ) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tsubscriber( getSessionStorageItem( key ) as T );\n\t\t},\n\t\t{ signal: abortController.signal }\n\t);\n\n\treturn () => {\n\t\tabortController.abort();\n\t};\n};\n","import * as React from 'react';\nimport { createContext, type PropsWithChildren, useContext } from 'react';\n\ntype ContextValue = {\n\tprefix: string;\n};\n\nexport const Context = createContext< ContextValue | null >( null );\n\ntype Props = PropsWithChildren< ContextValue >;\n\nexport function SessionStorageProvider( { children, prefix }: Props ) {\n\tconst contextPrefix = useContext( Context )?.prefix ?? '';\n\tconst chainedPrefix = contextPrefix ? `${ contextPrefix }/${ prefix }` : prefix;\n\n\treturn <Context.Provider value={ { prefix: chainedPrefix } }>{ children }</Context.Provider>;\n}\n"],"mappings":";AAAO,IAAM,wBAAwB,CAAO,QAAgC;AAC3E,SAAO,KAAK,MAAO,eAAe,QAAS,GAAI,KAAK,IAAK,GAAG;AAC7D;AAEO,IAAM,wBAAwB,CAAE,KAAa,SAAmB;AACtE,iBAAe,QAAS,KAAK,KAAK,UAAW,EAAE,KAAK,CAAE,CAAE;AAMxD,SAAO;AAAA,IACN,IAAI,aAAc,WAAW;AAAA,MAC5B;AAAA,MACA,aAAa;AAAA,IACd,CAAE;AAAA,EACH;AACD;AAEO,IAAM,2BAA2B,CAAE,QAAiB;AAC1D,iBAAe,WAAY,GAAI;AAM/B,SAAO;AAAA,IACN,IAAI,aAAc,WAAW;AAAA,MAC5B;AAAA,MACA,aAAa;AAAA,IACd,CAAE;AAAA,EACH;AACD;;;AChCA,SAAS,cAAAA,aAAY,WAAW,gBAAgB;;;ACAhD,YAAY,WAAW;AACvB,SAAS,eAAuC,kBAAkB;AAM3D,IAAM,UAAU,cAAsC,IAAK;AAI3D,SAAS,uBAAwB,EAAE,UAAU,OAAO,GAAW;AACrE,QAAM,gBAAgB,WAAY,OAAQ,GAAG,UAAU;AACvD,QAAM,gBAAgB,gBAAgB,GAAI,aAAc,IAAK,MAAO,KAAK;AAEzE,SAAO,oCAAC,QAAQ,UAAR,EAAiB,OAAQ,EAAE,QAAQ,cAAc,KAAM,QAAU;AAC1E;;;ADXO,IAAM,oBAAoB,CAAO,KAAa,iBAA2B;AAC/E,QAAM,gBAAgBC,YAAY,OAAQ,GAAG,UAAU;AACvD,QAAM,SAAS,eAAe,eAAe;AAC7C,QAAM,cAAc,GAAI,MAAO,IAAK,GAAI;AAExC,QAAM,CAAE,OAAO,QAAS,IAAI,SAAsB,MAAM;AACvD,UAAM,cAAc,sBAAuB,WAAY;AAEvD,WAAO,eAAe;AAAA,EACvB,CAAE;AAEF,YAAW,MAAM;AAChB,WAAO,0BAAuC,aAAa,CAAE,aAAc;AAC1E,eAAU,YAAY,IAAK;AAAA,IAC5B,CAAE;AAAA,EACH,GAAG,CAAE,WAAY,CAAE;AAEnB,QAAM,YAAY,CAAE,aAAiB;AACpC,0BAAuB,aAAa,QAAS;AAAA,EAC9C;AAEA,QAAM,cAAc,MAAM;AACzB,6BAA0B,WAAY;AAAA,EACvC;AAEA,SAAO,CAAE,OAAO,WAAW,WAAY;AACxC;AAEA,IAAM,4BAA4B,CAAO,KAAa,eAAsC;AAC3F,aAAY,sBAAuB,GAAI,CAAO;AAE9C,QAAM,kBAAkB,IAAI,gBAAgB;AAE5C,SAAO;AAAA,IACN;AAAA,IACA,CAAE,MAAO;AACR,UAAK,EAAE,QAAQ,OAAO,EAAE,gBAAgB,gBAAiB;AACxD;AAAA,MACD;AAEA,iBAAY,sBAAuB,GAAI,CAAO;AAAA,IAC/C;AAAA,IACA,EAAE,QAAQ,gBAAgB,OAAO;AAAA,EAClC;AAEA,SAAO,MAAM;AACZ,oBAAgB,MAAM;AAAA,EACvB;AACD;","names":["useContext","useContext"]}
|
package/package.json
CHANGED
|
@@ -8,7 +8,11 @@ export const useSessionStorage = < T >( key: string, customPrefix?: string ) =>
|
|
|
8
8
|
const prefix = customPrefix ? customPrefix : contextPrefix;
|
|
9
9
|
const prefixedKey = `${ prefix }/${ key }`;
|
|
10
10
|
|
|
11
|
-
const [ value, setValue ] = useState< T | null >()
|
|
11
|
+
const [ value, setValue ] = useState< T | null >( () => {
|
|
12
|
+
const storedValue = getSessionStorageItem( prefixedKey ) as T | undefined;
|
|
13
|
+
|
|
14
|
+
return storedValue ?? null;
|
|
15
|
+
} );
|
|
12
16
|
|
|
13
17
|
useEffect( () => {
|
|
14
18
|
return subscribeToSessionStorage< T | null >( prefixedKey, ( newValue ) => {
|