oihana-next-ui 0.1.34 → 0.1.35
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 +79 -0
- package/src/version.js +1 -1
package/package.json
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
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
|
+
* const [ display , saveDisplay , clearDisplay ] = useDisplayPreference( 'products' , 'flex' ) ;
|
|
20
|
+
*
|
|
21
|
+
* saveDisplay( 'masonry' ) ; // → localStorage + cookie display__products=masonry
|
|
22
|
+
* clearDisplay() ; // → removes both
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { useSyncExternalStore } from 'react' ;
|
|
27
|
+
|
|
28
|
+
import readStorage from '../helpers/storage/readStorage' ;
|
|
29
|
+
import removeStorage from '../helpers/storage/removeStorage' ;
|
|
30
|
+
import subscribeStorage from '../helpers/storage/subscribeStorage' ;
|
|
31
|
+
import writeStorage from '../helpers/storage/writeStorage' ;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Prefix applied to all display preference storage keys.
|
|
35
|
+
* @type {string}
|
|
36
|
+
*/
|
|
37
|
+
export const DISPLAY_STORAGE_PREFIX = 'display__' ;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Builds the full storage key from a page identifier.
|
|
41
|
+
*
|
|
42
|
+
* @param {string} pageKey - e.g. 'products' or 'app.products'
|
|
43
|
+
* @returns {string} - e.g. 'display__products'
|
|
44
|
+
*/
|
|
45
|
+
export const getDisplayStorageKey = pageKey => `${ DISPLAY_STORAGE_PREFIX }${ pageKey }` ;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {string} pageKey - Page identifier — use `url` or `path` prop from ThingsPage.
|
|
49
|
+
* @param {string} [defaultValue='flex'] - Fallback when nothing is stored.
|
|
50
|
+
* @returns {[ string , (mode: string) => void , () => void ]}
|
|
51
|
+
*/
|
|
52
|
+
const useDisplayPreference = ( pageKey , defaultValue = 'flex' ) =>
|
|
53
|
+
{
|
|
54
|
+
const key = getDisplayStorageKey( pageKey ) ;
|
|
55
|
+
|
|
56
|
+
const subscribe = cb => subscribeStorage( cb ) ;
|
|
57
|
+
const getSnapshot = () => readStorage( key ) ?? defaultValue ;
|
|
58
|
+
const getServerSnapshot = () => defaultValue ;
|
|
59
|
+
|
|
60
|
+
const value = useSyncExternalStore( subscribe , getSnapshot , getServerSnapshot ) ;
|
|
61
|
+
|
|
62
|
+
const save = mode =>
|
|
63
|
+
{
|
|
64
|
+
if ( mode )
|
|
65
|
+
{
|
|
66
|
+
writeStorage( key , mode ) ;
|
|
67
|
+
}
|
|
68
|
+
else
|
|
69
|
+
{
|
|
70
|
+
removeStorage( key ) ;
|
|
71
|
+
}
|
|
72
|
+
} ;
|
|
73
|
+
|
|
74
|
+
const clear = () => removeStorage( key ) ;
|
|
75
|
+
|
|
76
|
+
return [ value , save , clear ] ;
|
|
77
|
+
} ;
|
|
78
|
+
|
|
79
|
+
export default useDisplayPreference ;
|
package/src/version.js
CHANGED