oihana-next-ui 0.1.35 → 0.1.36
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/package.json +1 -1
- package/src/hooks/useDisplayPreference.js +16 -12
- package/src/version.js +1 -1
package/package.json
CHANGED
|
@@ -16,19 +16,20 @@
|
|
|
16
16
|
*
|
|
17
17
|
* @example
|
|
18
18
|
* ```js
|
|
19
|
+
* // With persistence
|
|
19
20
|
* const [ display , saveDisplay , clearDisplay ] = useDisplayPreference( 'products' , 'flex' ) ;
|
|
20
21
|
*
|
|
21
|
-
*
|
|
22
|
-
* clearDisplay(
|
|
22
|
+
* // Without persistence (pageKey absent) — save/clear are no-ops
|
|
23
|
+
* const [ display , saveDisplay , clearDisplay ] = useDisplayPreference( null , 'flex' ) ;
|
|
23
24
|
* ```
|
|
24
25
|
*/
|
|
25
26
|
|
|
26
27
|
import { useSyncExternalStore } from 'react' ;
|
|
27
28
|
|
|
28
|
-
import readStorage from '
|
|
29
|
-
import
|
|
30
|
-
import
|
|
31
|
-
import
|
|
29
|
+
import readStorage from 'oihana-next-ui/helpers/storage/readStorage' ;
|
|
30
|
+
import writeStorage from 'oihana-next-ui/helpers/storage/writeStorage' ;
|
|
31
|
+
import removeStorage from 'oihana-next-ui/helpers/storage/removeStorage' ;
|
|
32
|
+
import subscribeStorage from 'oihana-next-ui/helpers/storage/subscribeStorage' ;
|
|
32
33
|
|
|
33
34
|
/**
|
|
34
35
|
* Prefix applied to all display preference storage keys.
|
|
@@ -45,22 +46,25 @@ export const DISPLAY_STORAGE_PREFIX = 'display__' ;
|
|
|
45
46
|
export const getDisplayStorageKey = pageKey => `${ DISPLAY_STORAGE_PREFIX }${ pageKey }` ;
|
|
46
47
|
|
|
47
48
|
/**
|
|
48
|
-
* @param {string} pageKey - Page identifier — use `url` or `path` prop from ThingsPage.
|
|
49
|
-
*
|
|
49
|
+
* @param {string|null|undefined} pageKey - Page identifier — use `url` or `path` prop from ThingsPage.
|
|
50
|
+
* Pass null/undefined to disable persistence entirely.
|
|
51
|
+
* @param {string} [defaultValue='flex'] - Fallback when nothing is stored.
|
|
50
52
|
* @returns {[ string , (mode: string) => void , () => void ]}
|
|
51
53
|
*/
|
|
52
54
|
const useDisplayPreference = ( pageKey , defaultValue = 'flex' ) =>
|
|
53
55
|
{
|
|
54
|
-
const key = getDisplayStorageKey( pageKey ) ;
|
|
56
|
+
const key = pageKey ? getDisplayStorageKey( pageKey ) : null ;
|
|
55
57
|
|
|
56
|
-
const subscribe = cb => subscribeStorage( cb ) ;
|
|
57
|
-
const getSnapshot = () => readStorage( key ) ?? defaultValue ;
|
|
58
|
+
const subscribe = cb => key ? subscribeStorage( cb ) : () => {} ;
|
|
59
|
+
const getSnapshot = () => key ? ( readStorage( key ) ?? defaultValue ) : defaultValue ;
|
|
58
60
|
const getServerSnapshot = () => defaultValue ;
|
|
59
61
|
|
|
60
62
|
const value = useSyncExternalStore( subscribe , getSnapshot , getServerSnapshot ) ;
|
|
61
63
|
|
|
62
64
|
const save = mode =>
|
|
63
65
|
{
|
|
66
|
+
if ( !key ) return ;
|
|
67
|
+
|
|
64
68
|
if ( mode )
|
|
65
69
|
{
|
|
66
70
|
writeStorage( key , mode ) ;
|
|
@@ -71,7 +75,7 @@ const useDisplayPreference = ( pageKey , defaultValue = 'flex' ) =>
|
|
|
71
75
|
}
|
|
72
76
|
} ;
|
|
73
77
|
|
|
74
|
-
const clear = () => removeStorage( key ) ;
|
|
78
|
+
const clear = () => key && removeStorage( key ) ;
|
|
75
79
|
|
|
76
80
|
return [ value , save , clear ] ;
|
|
77
81
|
} ;
|
package/src/version.js
CHANGED