valtio-define 1.0.0 → 1.0.1

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 CHANGED
@@ -7,18 +7,20 @@
7
7
  [![coverage][coverage-src]][coverage-href]
8
8
  [![License][license-src]][license-href]
9
9
 
10
- ⚡ Quickly create a fully functional and robust Valtio factory
10
+ ⚡ Quickly create a fully functional and robust [Valtio](https://valtio.dev) factory
11
11
 
12
12
  ## Installation
13
13
 
14
14
  ```bash
15
- npm install valtio-define
15
+ pnpm add valtio-define
16
16
  ```
17
17
 
18
18
  ## Usage
19
19
 
20
20
  ### Basic Store
21
21
 
22
+ Create a reactive store with state and actions. The store provides a simple and intuitive API for managing state in React applications, built on top of Valtio.
23
+
22
24
  ```tsx
23
25
  import { defineStore, useStore } from 'valtio-define'
24
26
 
@@ -32,10 +34,10 @@ const store = defineStore({
32
34
  })
33
35
 
34
36
  function Counter() {
35
- const { count, increment } = useStore(store)
37
+ const { count } = useStore(store)
36
38
  return (
37
39
  <div>
38
- <button onClick={increment}>Increment</button>
40
+ <button onClick={store.increment}>Increment</button>
39
41
  <div>{count}</div>
40
42
  </div>
41
43
  )
@@ -44,6 +46,8 @@ function Counter() {
44
46
 
45
47
  ### With Getters
46
48
 
49
+ Getters are computed properties that automatically update when their dependencies change. They provide a clean way to derive state without manually tracking dependencies.
50
+
47
51
  ```tsx
48
52
  const store = defineStore({
49
53
  state: () => ({ count: 0 }),
@@ -169,6 +173,87 @@ function App() {
169
173
  }
170
174
  ```
171
175
 
176
+ ### Store to State Hooks
177
+
178
+ Convert store state to React hooks similar to `useState`. This provides a more React-idiomatic way to access and update store state.
179
+
180
+ #### `storeToState(store, key)`
181
+
182
+ Returns a tuple `[state, setter]` for a single store key, similar to React's `useState`.
183
+
184
+ ```tsx
185
+ import { defineStore, storeToState } from 'valtio-define'
186
+
187
+ const store = defineStore({
188
+ state: { count: 0, name: 'test' },
189
+ })
190
+
191
+ function Counter() {
192
+ const [count, setCount] = storeToState(store, 'count')
193
+ const [name, setName] = storeToState(store, 'name')
194
+
195
+ return (
196
+ <div>
197
+ <button onClick={() => setCount(count + 1)}>Increment</button>
198
+ <button onClick={() => setCount(prev => prev + 1)}>Increment (functional)</button>
199
+ <div>
200
+ Count:
201
+ {count}
202
+ </div>
203
+ <div>
204
+ Name:
205
+ {name}
206
+ </div>
207
+ </div>
208
+ )
209
+ }
210
+ ```
211
+
212
+ **Parameters:**
213
+ - `store`: Store instance created by `defineStore`
214
+ - `key`: Key of the state property to access
215
+
216
+ **Returns:** `[state, setter]` tuple where:
217
+ - `state`: Current value of the state property
218
+ - `setter`: Function to update the state (accepts value or updater function)
219
+
220
+ #### `storeToStates(store)`
221
+
222
+ Returns an object with all store keys mapped to `[state, setter]` tuples.
223
+
224
+ ```tsx
225
+ function Component() {
226
+ const {
227
+ count: [count, setCount],
228
+ name: [name, setName],
229
+ user: [user, setUser],
230
+ } = storeToStates(store)
231
+
232
+ return (
233
+ <div>
234
+ <button onClick={() => setCount(count + 1)}>Increment</button>
235
+ <div>
236
+ Count:
237
+ {count}
238
+ </div>
239
+ <div>
240
+ Name:
241
+ {name}
242
+ </div>
243
+ <div>
244
+ User:
245
+ {user.name}
246
+ </div>
247
+ </div>
248
+ )
249
+ }
250
+ ```
251
+
252
+ **Parameters:**
253
+ - `store`: Store instance created by `defineStore`
254
+
255
+ **Returns:** Object where each key maps to a `[state, setter]` tuple
256
+
172
257
  ## API
173
258
 
174
259
  ### `defineStore(store)`
@@ -179,7 +264,7 @@ Creates a store with state, actions, and getters.
179
264
  - `store.state`: Initial state object or factory function
180
265
  - `store.actions`: Object containing action methods
181
266
  - `store.getters`: Object containing getter methods
182
- - `store.persist`: Persistence plugin configuration (boolean or object) - see [Persistence Plugin](#persistence-plugin)
267
+ - `store.persist`: Persistence plugin configuration (boolean or object) - see [Persistence Plugin](#persistence)
183
268
 
184
269
  **Returns:** Store instance with reactive state and actions
185
270
 
@@ -192,6 +277,27 @@ React hook that returns a snapshot of the store state.
192
277
 
193
278
  **Returns:** Snapshot of the store state
194
279
 
280
+ ### `storeToState(store, key)`
281
+
282
+ React hook that returns a `[state, setter]` tuple for a single store key, similar to React's `useState`.
283
+
284
+ **Parameters:**
285
+ - `store`: Store instance created by `defineStore`
286
+ - `key`: Key of the state property to access
287
+
288
+ **Returns:** `[state, setter]` tuple where:
289
+ - `state`: Current value of the state property
290
+ - `setter`: Function to update the state (accepts value or updater function like `setState(value)` or `setState(prev => newValue)`)
291
+
292
+ ### `storeToStates(store)`
293
+
294
+ React hook that returns an object with all store keys mapped to `[state, setter]` tuples.
295
+
296
+ **Parameters:**
297
+ - `store`: Store instance created by `defineStore`
298
+
299
+ **Returns:** Object where each key maps to a `[state, setter]` tuple, preserving the correct type for each property
300
+
195
301
  ### Plugins
196
302
 
197
303
  Plugins allow you to extend store functionality. You can use plugins globally or per-store.
@@ -199,11 +305,11 @@ Plugins allow you to extend store functionality. You can use plugins globally or
199
305
  #### Global Plugin Registration
200
306
 
201
307
  ```tsx
202
- import { use } from 'valtio-define'
308
+ import valtio from 'valtio-define'
203
309
  import { persistent } from 'valtio-define/plugins'
204
310
 
205
311
  // Register plugin globally - applies to all stores
206
- use(persistent())
312
+ valtio.use(persistent())
207
313
  ```
208
314
 
209
315
  #### Per-Store Plugin Registration
@@ -1,4 +1,4 @@
1
- import { s as Plugin } from "./index-BxQlAvn6.mjs";
1
+ import { s as Plugin } from "./index-C_5_h4QO.mjs";
2
2
  import { Awaitable } from "@hairy/utils";
3
3
 
4
4
  //#region src/plugins/persistent/index.d.ts
@@ -16,7 +16,7 @@ interface PersistentOptions<S extends object = Record<string, unknown>> {
16
16
  }
17
17
  declare module 'valtio-define/types' {
18
18
  interface StoreDefine<S extends object, A extends ActionsTree, G extends Getters<any>> {
19
- persist?: PersistentOptions<S>;
19
+ persist?: PersistentOptions<S> | boolean;
20
20
  }
21
21
  }
22
22
  //#endregion
@@ -25,7 +25,7 @@ interface SubscribeKey<S, G extends Getters<S>> {
25
25
  interface Patch<S, G extends Getters<S>> {
26
26
  (patch: Partial<S> | ((state: S & GettersReturnType<G>) => void)): void;
27
27
  }
28
- type Store<S, A extends Actions<S>, G extends Getters<S>> = {
28
+ type Store<S, A extends Actions<S> = Actions<S>, G extends Getters<S> = Getters<S>> = {
29
29
  $subscribe: Subscribe<S, G>;
30
30
  $subscribeKey: SubscribeKey<S, G>;
31
31
  $patch: Patch<S, G>;
package/dist/index.d.mts CHANGED
@@ -1,4 +1,5 @@
1
- import { a as GettersReturnType, d as StoreDefine, i as Getters, r as ActionsTree, s as Plugin, t as Actions, u as Store } from "./index-BxQlAvn6.mjs";
1
+ import { a as GettersReturnType, d as StoreDefine, i as Getters, r as ActionsTree, s as Plugin, t as Actions, u as Store } from "./index-C_5_h4QO.mjs";
2
+ import { Dispatch, SetStateAction } from "react";
2
3
  import { Snapshot } from "valtio";
3
4
 
4
5
  //#region src/plugin.d.ts
@@ -39,9 +40,13 @@ declare function defineStore<S extends object, A extends ActionsTree, G extends
39
40
  //#region src/use.d.ts
40
41
  declare function useStore<S extends object, A extends Actions<S>, G extends Getters<S>>(store: Store<S, A, G>): Snapshot<S & GettersReturnType<G> & A>;
41
42
  //#endregion
43
+ //#region src/utils.d.ts
44
+ declare function storeToState<S extends object, K$1 extends keyof S>(store: Store<S>, key: K$1): [S[K$1], Dispatch<SetStateAction<S[K$1]>>];
45
+ declare function storeToStates<S extends object>(store: Store<S>): { [K in keyof S]: [S[K], Dispatch<SetStateAction<S[K]>>] };
46
+ //#endregion
42
47
  //#region src/index.d.ts
43
48
  declare const _default: {
44
49
  use: typeof use;
45
50
  };
46
51
  //#endregion
47
- export { _default as default, defineStore, plugins, use, useStore };
52
+ export { _default as default, defineStore, plugins, storeToState, storeToStates, use, useStore };
package/dist/index.mjs CHANGED
@@ -112,9 +112,27 @@ function useStore(store) {
112
112
  return useSnapshot(store.$state);
113
113
  }
114
114
 
115
+ //#endregion
116
+ //#region src/utils.ts
117
+ function storeToState(store, key) {
118
+ const state = useStore(store);
119
+ function set(value) {
120
+ if (typeof value === "function") store.$patch((state$1) => {
121
+ state$1[key] = value(state$1[key]);
122
+ });
123
+ else store.$patch((state$1) => {
124
+ state$1[key] = value;
125
+ });
126
+ }
127
+ return [state[key], set];
128
+ }
129
+ function storeToStates(store) {
130
+ return Object.fromEntries(Object.keys(store.$state).map((key) => [key, storeToState(store, key)]));
131
+ }
132
+
115
133
  //#endregion
116
134
  //#region src/index.ts
117
135
  var src_default = { use };
118
136
 
119
137
  //#endregion
120
- export { src_default as default, defineStore, plugins, use, useStore };
138
+ export { src_default as default, defineStore, plugins, storeToState, storeToStates, use, useStore };
@@ -6,7 +6,8 @@ import { generateStructureId } from "structure-id";
6
6
  //#region src/plugins/persistent/index.ts
7
7
  function persistent() {
8
8
  return (context) => {
9
- const options = context.options.persist || {};
9
+ if (!context.options.persist) return;
10
+ const options = typeof context.options.persist === "boolean" ? {} : context.options.persist;
10
11
  options.key = options.key || generateStructureId(context.store.$state);
11
12
  const storage = options.storage || (typeof localStorage !== "undefined" ? localStorage : void 0);
12
13
  const value = storage?.getItem(options.key);
@@ -1,3 +1,3 @@
1
- import "../index-BxQlAvn6.mjs";
2
- import { i as persistent, n as PersistentOptions, r as Storage, t as DeepKeys } from "../index-3Qs1mdbd.mjs";
1
+ import "../index-C_5_h4QO.mjs";
2
+ import { i as persistent, n as PersistentOptions, r as Storage, t as DeepKeys } from "../index-Bvdj9tgN.mjs";
3
3
  export { DeepKeys, PersistentOptions, Storage, persistent };
@@ -1,3 +1,3 @@
1
- import { t as persistent } from "../persistent-CQxMybmm.mjs";
1
+ import { t as persistent } from "../persistent-kgcmMQPj.mjs";
2
2
 
3
3
  export { persistent };
@@ -1,3 +1,3 @@
1
- import "../../index-BxQlAvn6.mjs";
2
- import { i as persistent, n as PersistentOptions, r as Storage, t as DeepKeys } from "../../index-3Qs1mdbd.mjs";
1
+ import "../../index-C_5_h4QO.mjs";
2
+ import { i as persistent, n as PersistentOptions, r as Storage, t as DeepKeys } from "../../index-Bvdj9tgN.mjs";
3
3
  export { DeepKeys, PersistentOptions, Storage, persistent };
@@ -1,3 +1,3 @@
1
- import { t as persistent } from "../../persistent-CQxMybmm.mjs";
1
+ import { t as persistent } from "../../persistent-kgcmMQPj.mjs";
2
2
 
3
3
  export { persistent };
@@ -1,2 +1,2 @@
1
- import { a as GettersReturnType, c as PluginContext, d as StoreDefine, f as Subscribe, i as Getters, l as Signal, n as ActionsOmitThisParameter, o as Patch, p as SubscribeKey, r as ActionsTree, s as Plugin, t as Actions, u as Store } from "../index-BxQlAvn6.mjs";
1
+ import { a as GettersReturnType, c as PluginContext, d as StoreDefine, f as Subscribe, i as Getters, l as Signal, n as ActionsOmitThisParameter, o as Patch, p as SubscribeKey, r as ActionsTree, s as Plugin, t as Actions, u as Store } from "../index-C_5_h4QO.mjs";
2
2
  export { Actions, ActionsOmitThisParameter, ActionsTree, Getters, GettersReturnType, Patch, Plugin, PluginContext, Signal, Store, StoreDefine, Subscribe, SubscribeKey };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "valtio-define",
3
3
  "type": "module",
4
- "version": "1.0.0",
4
+ "version": "1.0.1",
5
5
  "description": "⚡quickly create a fully functional and robust Valtio factory",
6
6
  "author": "Hairyf <wwu710632@gmail.com>",
7
7
  "license": "MIT",