@kamod-ch/signals 1.0.0 → 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 +103 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# @kamod-ch/signals
|
|
2
|
+
|
|
3
|
+
Persisted Preact signals for `localStorage`, `sessionStorage`, IndexedDB, cookies, and memory.
|
|
4
|
+
|
|
5
|
+
`@kamod-ch/signals` is a lightweight helper package for Preact apps that need reactive state with durable storage. It is SSR-safe, supports cookie request contexts, and keeps the familiar `@preact/signals` API.
|
|
6
|
+
|
|
7
|
+
- Docs: <https://kamod-ch.github.io/kamod-signals/>
|
|
8
|
+
- npm: <https://www.npmjs.com/package/@kamod-ch/signals>
|
|
9
|
+
- Repository: <https://github.com/kamod-ch/kamod-signals>
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add @kamod-ch/signals @preact/signals preact
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { persistedSignal, usePersistedSignal } from "@kamod-ch/signals";
|
|
21
|
+
|
|
22
|
+
export const theme = persistedSignal("theme", "dark", { storage: "local" });
|
|
23
|
+
|
|
24
|
+
export const token = persistedSignal("token", "", {
|
|
25
|
+
storage: "cookie",
|
|
26
|
+
cookie: { expires: 7, path: "/" },
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
function Sidebar() {
|
|
30
|
+
const isOpen = usePersistedSignal("sidebar-open", true, { storage: "session" });
|
|
31
|
+
|
|
32
|
+
return <button onClick={() => (isOpen.value = !isOpen.value)}>Toggle</button>;
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## API
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
persistedSignal<T>(
|
|
40
|
+
key: string,
|
|
41
|
+
initialValue: T,
|
|
42
|
+
options?: PersistedSignalOptions<T>
|
|
43
|
+
): PersistedSignal<T>;
|
|
44
|
+
|
|
45
|
+
usePersistedSignal<T>(
|
|
46
|
+
key: string,
|
|
47
|
+
initialValue: T,
|
|
48
|
+
options?: PersistedSignalOptions<T>
|
|
49
|
+
): PersistedSignal<T>;
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
type PersistedSignalOptions<T> = {
|
|
54
|
+
storage?: "local" | "session" | "indexeddb" | "cookie" | "memory";
|
|
55
|
+
serialize?: (value: T) => string;
|
|
56
|
+
deserialize?: (raw: string) => T;
|
|
57
|
+
sync?: boolean;
|
|
58
|
+
removeOnUndefined?: boolean;
|
|
59
|
+
indexedDB?: { database?: string; store?: string; version?: number };
|
|
60
|
+
cookie?: {
|
|
61
|
+
expires?: number | Date;
|
|
62
|
+
path?: string;
|
|
63
|
+
domain?: string;
|
|
64
|
+
secure?: boolean;
|
|
65
|
+
sameSite?: "Lax" | "Strict" | "None";
|
|
66
|
+
};
|
|
67
|
+
cookieContext?: CookieContext;
|
|
68
|
+
};
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## SSR cookie context
|
|
72
|
+
|
|
73
|
+
Use `createCookieContext()` when a cookie-backed value must be available during server-side rendering.
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { createCookieContext, persistedSignal } from "@kamod-ch/signals";
|
|
77
|
+
|
|
78
|
+
const cookieContext = createCookieContext({
|
|
79
|
+
cookie: request.headers,
|
|
80
|
+
onSetCookie: (header) => response.headers.append("set-cookie", header),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const theme = persistedSignal("theme", "dark", {
|
|
84
|
+
storage: "cookie",
|
|
85
|
+
cookieContext,
|
|
86
|
+
cookie: { path: "/", sameSite: "Lax" },
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Notes
|
|
91
|
+
|
|
92
|
+
- SSR-safe: browser-only storage is never touched on the server.
|
|
93
|
+
- `local`, `session`, and `indexeddb` fall back to in-memory storage when unavailable.
|
|
94
|
+
- IndexedDB hydration is async, so signals start with `initialValue` and update after the persisted value loads.
|
|
95
|
+
- Cookie SSR works when you pass `cookieContext`; cache identity stays scoped to that request context.
|
|
96
|
+
|
|
97
|
+
## Publishing
|
|
98
|
+
|
|
99
|
+
This package is published from `packages/core` only:
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
pnpm publish:core
|
|
103
|
+
```
|