@yakocloud/state-vocab 1.0.1 → 1.0.2
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/README.md +74 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,83 @@
|
|
|
1
|
-
# state-vocab
|
|
1
|
+
# @yakocloud/state-vocab
|
|
2
2
|
|
|
3
3
|
A lightweight React state management library that synchronizes component state with any `Storage`-compatible (localStorage, sessionStorage, custom).
|
|
4
4
|
|
|
5
|
+
## Why use state-vocab?
|
|
6
|
+
|
|
7
|
+
Most state managers treat persistence as an afterthought — you manage state first, then manually sync it to localStorage. **state-vocab flips this**: storage is defined upfront alongside the state itself, and synchronization is automatic.
|
|
8
|
+
|
|
9
|
+
**Storage-first by design.** Every state node declares its backend at definition time — localStorage, sessionStorage, or any custom adapter. No `useEffect(() => localStorage.setItem(...), [value])` scattered across components.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// declare once — works everywhere
|
|
13
|
+
const storage = createRouter({
|
|
14
|
+
theme: defineState({ storage: localStorage, defaultValue: 'Dark' }),
|
|
15
|
+
session: defineState({ storage: sessionStorage }),
|
|
16
|
+
inMemory: defineState({ defaultValue: 0 }),
|
|
17
|
+
})
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**Bring your own backend.** The `storage` option accepts any object implementing the [Web Storage API](https://developer.mozilla.org/en-US/docs/Web/API/Storage). Point state directly at a server, IndexedDB wrapper, or encrypted store — no extra adapters needed.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
defineState({
|
|
24
|
+
storage: {
|
|
25
|
+
getItem: (key) => api.get(key),
|
|
26
|
+
setItem: (key, value) => api.set(key, value),
|
|
27
|
+
removeItem: (key) => api.delete(key),
|
|
28
|
+
// ...
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**No prop drilling.** State lives in a shared `storage` object imported directly into any component. No need to pass values down through layers of props or lift state up to a common ancestor — any component in the tree can read and write the same node independently.
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
// ❌ without state-vocab — thread props through every layer
|
|
37
|
+
function Page({ theme, onThemeChange }) {
|
|
38
|
+
return <Sidebar theme={theme} onThemeChange={onThemeChange} />
|
|
39
|
+
}
|
|
40
|
+
function Sidebar({ theme, onThemeChange }) {
|
|
41
|
+
return <ThemeToggle theme={theme} onThemeChange={onThemeChange} />
|
|
42
|
+
}
|
|
43
|
+
function ThemeToggle({ theme, onThemeChange }) {
|
|
44
|
+
return <button onClick={() => onThemeChange('Dark')}>{theme}</button>
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ✅ with state-vocab — import storage, use directly
|
|
48
|
+
function ThemeToggle() {
|
|
49
|
+
const [theme, setTheme] = storage.preference.theme.useState()
|
|
50
|
+
return <button onClick={() => setTheme('Dark')}>{theme}</button>
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**Dot-notation access with full TypeScript inference.** The state tree is navigated like a plain object — autocomplete guides you to the right node, and types flow from `defineState<T>` all the way to the hook return value without any manual annotations.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
const [theme, setTheme] = storage.preference.theme.useState()
|
|
58
|
+
// ^? 'Dark' | 'White' | 'System'
|
|
59
|
+
|
|
60
|
+
const [birthday, setBirthday] = storage.personal.birthday.useState()
|
|
61
|
+
// ^? Date | null
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
If you rename or restructure a node in `createRouter`, TypeScript immediately flags every broken reference across the codebase.
|
|
65
|
+
|
|
66
|
+
**Custom serialization per node.** Dates, Maps, class instances — define `serialize`/`deserialize` once and the hook handles the rest transparently.
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
defineState({
|
|
70
|
+
storage: localStorage,
|
|
71
|
+
deserialize: (raw) => new Date(JSON.parse(raw)),
|
|
72
|
+
})
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**Minimal API surface.** Three exports: `defineState`, `createRouter`, `StorageProvider`. No actions, reducers, selectors, or stores to configure.
|
|
76
|
+
|
|
5
77
|
## Installation
|
|
6
78
|
|
|
7
79
|
```bash
|
|
8
|
-
npm install state-vocab
|
|
80
|
+
npm install @yakocloud/state-vocab
|
|
9
81
|
```
|
|
10
82
|
|
|
11
83
|
## Quick Start
|