oihana-next-ui 0.1.34 → 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 +83 -0
- package/src/version.js +1 -1
package/package.json
CHANGED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
'use client' ;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Persists the display mode preference for a given page key.
|
|
5
|
+
*
|
|
6
|
+
* Storage key pattern: `display__${pageKey}` (e.g. `display__products`)
|
|
7
|
+
*
|
|
8
|
+
* Priority (read):
|
|
9
|
+
* 1. localStorage (client, reactive via useSyncExternalStore)
|
|
10
|
+
* 2. defaultValue prop
|
|
11
|
+
*
|
|
12
|
+
* Priority (write):
|
|
13
|
+
* writeStorage → localStorage + cookie (for SSR read on next load)
|
|
14
|
+
*
|
|
15
|
+
* @module hooks/useDisplayPreference
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```js
|
|
19
|
+
* // With persistence
|
|
20
|
+
* const [ display , saveDisplay , clearDisplay ] = useDisplayPreference( 'products' , 'flex' ) ;
|
|
21
|
+
*
|
|
22
|
+
* // Without persistence (pageKey absent) — save/clear are no-ops
|
|
23
|
+
* const [ display , saveDisplay , clearDisplay ] = useDisplayPreference( null , 'flex' ) ;
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { useSyncExternalStore } from 'react' ;
|
|
28
|
+
|
|
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' ;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Prefix applied to all display preference storage keys.
|
|
36
|
+
* @type {string}
|
|
37
|
+
*/
|
|
38
|
+
export const DISPLAY_STORAGE_PREFIX = 'display__' ;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Builds the full storage key from a page identifier.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} pageKey - e.g. 'products' or 'app.products'
|
|
44
|
+
* @returns {string} - e.g. 'display__products'
|
|
45
|
+
*/
|
|
46
|
+
export const getDisplayStorageKey = pageKey => `${ DISPLAY_STORAGE_PREFIX }${ pageKey }` ;
|
|
47
|
+
|
|
48
|
+
/**
|
|
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.
|
|
52
|
+
* @returns {[ string , (mode: string) => void , () => void ]}
|
|
53
|
+
*/
|
|
54
|
+
const useDisplayPreference = ( pageKey , defaultValue = 'flex' ) =>
|
|
55
|
+
{
|
|
56
|
+
const key = pageKey ? getDisplayStorageKey( pageKey ) : null ;
|
|
57
|
+
|
|
58
|
+
const subscribe = cb => key ? subscribeStorage( cb ) : () => {} ;
|
|
59
|
+
const getSnapshot = () => key ? ( readStorage( key ) ?? defaultValue ) : defaultValue ;
|
|
60
|
+
const getServerSnapshot = () => defaultValue ;
|
|
61
|
+
|
|
62
|
+
const value = useSyncExternalStore( subscribe , getSnapshot , getServerSnapshot ) ;
|
|
63
|
+
|
|
64
|
+
const save = mode =>
|
|
65
|
+
{
|
|
66
|
+
if ( !key ) return ;
|
|
67
|
+
|
|
68
|
+
if ( mode )
|
|
69
|
+
{
|
|
70
|
+
writeStorage( key , mode ) ;
|
|
71
|
+
}
|
|
72
|
+
else
|
|
73
|
+
{
|
|
74
|
+
removeStorage( key ) ;
|
|
75
|
+
}
|
|
76
|
+
} ;
|
|
77
|
+
|
|
78
|
+
const clear = () => key && removeStorage( key ) ;
|
|
79
|
+
|
|
80
|
+
return [ value , save , clear ] ;
|
|
81
|
+
} ;
|
|
82
|
+
|
|
83
|
+
export default useDisplayPreference ;
|
package/src/version.js
CHANGED