@sveltebase/state 0.3.3 → 0.3.4

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 +78 -114
  2. package/package.json +2 -2
package/README.md CHANGED
@@ -1,80 +1,56 @@
1
1
  # `@sveltebase/state`
2
2
 
3
- Small rune-friendly state helpers for Svelte 5.
3
+ Small state helpers for Svelte 5.
4
4
 
5
- This package provides two classes:
5
+ This package exports two classes:
6
6
 
7
- - `State<T>` for simple in-memory reactive state
8
- - `PersistentState<TSchema>` for validated state that is persisted in cookies using `zod`
7
+ - `State<T>`: simple reactive in-memory state
8
+ - `PersistentState<TSchema>`: reactive state backed by cookies and validated with `zod`
9
9
 
10
10
  ## Install
11
11
 
12
- ```bash
13
- bun add @sveltebase/state zod
14
- ```
12
+ ~~~bash
13
+ bun add @sveltebase/state zod svelte
14
+ ~~~
15
15
 
16
- You also need `svelte` as a peer dependency:
17
-
18
- ```bash
19
- bun add svelte
20
- ```
21
-
22
- ## What it exports
16
+ ## Exports
23
17
 
24
18
  - `State`
25
19
  - `PersistentState`
26
20
 
27
21
  ## `State`
28
22
 
29
- Use `State` when you want a tiny reactive wrapper around a value.
30
-
31
- ### Example
23
+ A tiny wrapper around a value.
32
24
 
33
- ```ts
25
+ ~~~ts
34
26
  import { State } from "@sveltebase/state";
35
27
 
36
- const counter = new State(0);
28
+ const count = new State(0);
37
29
 
38
- counter.current = 1;
39
- counter.set((value) => value + 1);
30
+ count.current = 1;
31
+ count.set((value) => value + 1);
40
32
 
41
- console.log(counter.current); // 2
42
- ```
33
+ console.log(count.current); // 2
34
+ ~~~
43
35
 
44
36
  ### API
45
37
 
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.
38
+ - `new State(initialValue)`
39
+ - `state.current`
40
+ - `state.set(updater)`
57
41
 
58
42
  ## `PersistentState`
59
43
 
60
- Use `PersistentState` when you want state that:
44
+ Cookie-backed state with schema validation.
61
45
 
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
46
+ It:
66
47
 
67
- ## Install requirements
48
+ - reads from cookies
49
+ - validates with `zod`
50
+ - writes updates back to cookies
51
+ - can load the initial value during SSR
68
52
 
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
53
+ ~~~ts
78
54
  import { z } from "zod";
79
55
  import { PersistentState } from "@sveltebase/state";
80
56
 
@@ -82,107 +58,95 @@ const themeSchema = z.enum(["light", "dark"]).default("light");
82
58
 
83
59
  export const theme = new PersistentState("theme", themeSchema);
84
60
 
85
- // read
86
- console.log(theme.current);
87
-
88
- // write
89
61
  theme.current = "dark";
90
-
91
- // update
92
62
  theme.set((value) => (value === "dark" ? "light" : "dark"));
93
- ```
63
+ ~~~
94
64
 
95
- ## SSR usage
65
+ ## SSR setup
96
66
 
97
- When rendering on the server, you can initialize the state from cookies before using it.
67
+ For SSR, return all cookies from `+layout.server.ts`, then initialize the state with `state.init(() => data.cookies)` so it loads the server value first.
98
68
 
99
- ```ts
100
- import { z } from "zod";
101
- import { PersistentState } from "@sveltebase/state";
69
+ ### `src/routes/+layout.server.ts`
102
70
 
103
- const localeSchema = z.enum(["en", "uz"]).default("en");
71
+ ~~~ts
72
+ export async function load({ cookies }) {
73
+ return {
74
+ cookies: cookies.getAll()
75
+ };
76
+ }
77
+ ~~~
104
78
 
105
- export const locale = new PersistentState("locale", localeSchema);
79
+ ### `src/routes/+layout.svelte`
106
80
 
107
- // for example, from a server load or request context:
108
- locale.init([
109
- { name: "locale", value: "\"uz\"" }
110
- ]);
81
+ ~~~svelte
82
+ <script lang="ts">
83
+ import type { LayoutData } from "./$types";
84
+ import { locale } from "$lib/state";
111
85
 
112
- console.log(locale.current); // "uz"
113
- ```
86
+ let { data }: { data: LayoutData } = $props();
114
87
 
115
- ## How persistence works
88
+ locale.init(() => data.cookies);
89
+ </script>
116
90
 
117
- `PersistentState` stores the current value in a cookie using the key you pass to the constructor.
91
+ <slot />
92
+ ~~~
118
93
 
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
94
+ ### `src/lib/state.ts`
122
95
 
123
- The stored cookie value is JSON-encoded and validated with your `zod` schema.
96
+ ~~~ts
97
+ import { z } from "zod";
98
+ import { PersistentState } from "@sveltebase/state";
99
+
100
+ export const locale = new PersistentState(
101
+ "locale",
102
+ z.enum(["en", "uz"]).default("en")
103
+ );
104
+ ~~~
105
+
106
+ With this setup:
107
+
108
+ - on the server, `init` reads from `data.cookies`
109
+ - the initial SSR render uses that cookie value
110
+ - in the browser, updates keep syncing back to cookies
124
111
 
125
112
  ## API
126
113
 
127
114
  ### `new PersistentState(key, schema)`
128
115
 
129
- Creates a persistent reactive state value.
116
+ Creates a persistent state value.
130
117
 
131
118
  - `key`: cookie name
132
- - `schema`: `zod` schema used to validate and parse the value
119
+ - `schema`: `zod` schema for parsing and validation
133
120
 
134
121
  ### `persistentState.current`
135
122
 
136
- Gets or sets the parsed current value.
123
+ Gets or sets the current value.
137
124
 
138
125
  ### `persistentState.set(updater)`
139
126
 
140
- Updates the current value using a callback.
127
+ Updates the current value.
141
128
 
142
129
  ### `persistentState.init(cookies)`
143
130
 
144
- Initializes the state from server-side cookies.
131
+ Loads the value from server cookies.
132
+
133
+ It accepts either:
145
134
 
146
- Expected shape:
135
+ - a cookie array
136
+ - a getter function like `() => data.cookies`
147
137
 
148
- ```ts
138
+ Expected cookie shape:
139
+
140
+ ~~~ts
149
141
  type Cookie = {
150
142
  name: string;
151
143
  value: string;
152
144
  };
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
- ```
145
+ ~~~
182
146
 
183
147
  ## Notes
184
148
 
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
149
+ - `PersistentState` stores JSON in cookies
150
+ - invalid cookie data falls back to the schema result
151
+ - `init(...)` is for server-side initialization
188
152
  - 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.3.3",
3
+ "version": "0.3.4",
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.3.3",
21
+ "@sveltebase/utils": "0.3.4",
22
22
  "zod": "^4.1.11"
23
23
  },
24
24
  "peerDependencies": {