@sveltebase/state 0.2.3 → 0.3.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.
Files changed (2) hide show
  1. package/README.md +188 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,188 @@
1
+ # `@sveltebase/state`
2
+
3
+ Small rune-friendly state helpers for Svelte 5.
4
+
5
+ This package provides two classes:
6
+
7
+ - `State<T>` for simple in-memory reactive state
8
+ - `PersistentState<TSchema>` for validated state that is persisted in cookies using `zod`
9
+
10
+ ## Install
11
+
12
+ ```bash
13
+ bun add @sveltebase/state zod
14
+ ```
15
+
16
+ You also need `svelte` as a peer dependency:
17
+
18
+ ```bash
19
+ bun add svelte
20
+ ```
21
+
22
+ ## What it exports
23
+
24
+ - `State`
25
+ - `PersistentState`
26
+
27
+ ## `State`
28
+
29
+ Use `State` when you want a tiny reactive wrapper around a value.
30
+
31
+ ### Example
32
+
33
+ ```ts
34
+ import { State } from "@sveltebase/state";
35
+
36
+ const counter = new State(0);
37
+
38
+ counter.current = 1;
39
+ counter.set((value) => value + 1);
40
+
41
+ console.log(counter.current); // 2
42
+ ```
43
+
44
+ ### API
45
+
46
+ #### `new State(initialValue)`
47
+
48
+ Creates a new reactive state container.
49
+
50
+ #### `state.current`
51
+
52
+ Gets or sets the current value.
53
+
54
+ #### `state.set(updater)`
55
+
56
+ Updates the current value using a callback.
57
+
58
+ ## `PersistentState`
59
+
60
+ Use `PersistentState` when you want state that:
61
+
62
+ - is validated with `zod`
63
+ - hydrates from cookies in the browser
64
+ - can be initialized from request cookies during SSR
65
+ - writes changes back to cookies automatically
66
+
67
+ ## Install requirements
68
+
69
+ `PersistentState` depends on `zod`, so make sure it is installed:
70
+
71
+ ```bash
72
+ bun add zod
73
+ ```
74
+
75
+ ## Example
76
+
77
+ ```ts
78
+ import { z } from "zod";
79
+ import { PersistentState } from "@sveltebase/state";
80
+
81
+ const themeSchema = z.enum(["light", "dark"]).default("light");
82
+
83
+ export const theme = new PersistentState("theme", themeSchema);
84
+
85
+ // read
86
+ console.log(theme.current);
87
+
88
+ // write
89
+ theme.current = "dark";
90
+
91
+ // update
92
+ theme.set((value) => (value === "dark" ? "light" : "dark"));
93
+ ```
94
+
95
+ ## SSR usage
96
+
97
+ When rendering on the server, you can initialize the state from cookies before using it.
98
+
99
+ ```ts
100
+ import { z } from "zod";
101
+ import { PersistentState } from "@sveltebase/state";
102
+
103
+ const localeSchema = z.enum(["en", "uz"]).default("en");
104
+
105
+ export const locale = new PersistentState("locale", localeSchema);
106
+
107
+ // for example, from a server load or request context:
108
+ locale.init([
109
+ { name: "locale", value: "\"uz\"" }
110
+ ]);
111
+
112
+ console.log(locale.current); // "uz"
113
+ ```
114
+
115
+ ## How persistence works
116
+
117
+ `PersistentState` stores the current value in a cookie using the key you pass to the constructor.
118
+
119
+ - On the client, it reads the cookie during hydration
120
+ - On updates, it writes the new value back to the cookie
121
+ - On the server, you can call `init(cookies)` to sync the initial value from request cookies
122
+
123
+ The stored cookie value is JSON-encoded and validated with your `zod` schema.
124
+
125
+ ## API
126
+
127
+ ### `new PersistentState(key, schema)`
128
+
129
+ Creates a persistent reactive state value.
130
+
131
+ - `key`: cookie name
132
+ - `schema`: `zod` schema used to validate and parse the value
133
+
134
+ ### `persistentState.current`
135
+
136
+ Gets or sets the parsed current value.
137
+
138
+ ### `persistentState.set(updater)`
139
+
140
+ Updates the current value using a callback.
141
+
142
+ ### `persistentState.init(cookies)`
143
+
144
+ Initializes the state from server-side cookies.
145
+
146
+ Expected shape:
147
+
148
+ ```ts
149
+ type Cookie = {
150
+ name: string;
151
+ value: string;
152
+ };
153
+ ```
154
+
155
+ ## Example with Svelte
156
+
157
+ ```svelte
158
+ <script lang="ts">
159
+ import { z } from "zod";
160
+ import { State, PersistentState } from "@sveltebase/state";
161
+
162
+ const count = new State(0);
163
+ const locale = new PersistentState("locale", z.enum(["en", "uz"]).default("en"));
164
+
165
+ function increment() {
166
+ count.set((value) => value + 1);
167
+ }
168
+
169
+ function toggleLocale() {
170
+ locale.current = locale.current === "en" ? "uz" : "en";
171
+ }
172
+ </script>
173
+
174
+ <button onclick={increment}>
175
+ Count: {count.current}
176
+ </button>
177
+
178
+ <button onclick={toggleLocale}>
179
+ Locale: {locale.current}
180
+ </button>
181
+ ```
182
+
183
+ ## Notes
184
+
185
+ - `PersistentState` uses cookies for persistence
186
+ - values are validated with `zod` before being accepted
187
+ - if cookie data is invalid, the schema fallback/default is used
188
+ - this package is designed for Svelte 5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltebase/state",
3
- "version": "0.2.3",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -18,7 +18,7 @@
18
18
  }
19
19
  },
20
20
  "dependencies": {
21
- "@sveltebase/utils": "0.2.3",
21
+ "@sveltebase/utils": "0.3.1",
22
22
  "zod": "^4.1.11"
23
23
  },
24
24
  "peerDependencies": {