Skip to content

Commit 6869518

Browse files
committed
add Store to the documentation
1 parent c351179 commit 6869518

2 files changed

Lines changed: 151 additions & 5 deletions

File tree

www/astro.config.mjs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ export default defineConfig({
1010
discord: "https://wtw.dev/chat",
1111
},
1212
sidebar: [
13+
{
14+
label: "💾 Store",
15+
link: "/store",
16+
},
1317
{
1418
label: "🔎 Query",
1519
link: "/query",
@@ -18,18 +22,17 @@ export default defineConfig({
1822
label: "🔎 Scope",
1923
link: "/scope",
2024
},
21-
2225
{
2326
label: "🌊 Stream",
2427
link: "/stream",
25-
badge: { text: 'Deprecated', variant: 'caution' },
28+
badge: { text: "Deprecated", variant: "caution" },
2629
},
2730
{
2831
label: "🧘‍♂️ Form",
2932
autogenerate: { directory: "form" },
30-
collapsed: true,
31-
badge: { text: 'Deprecated', variant: 'caution' },
32-
}
33+
collapsed: true,
34+
badge: { text: "Deprecated", variant: "caution" },
35+
},
3336
],
3437
customCss: [
3538
"@fontsource/atkinson-hyperlegible/400.css",

www/src/content/docs/store.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: Simple store 💾
3+
description: A reactive store that combines the simplicity of signals with the power of "selectors" you'd find in Zustand or Redux.
4+
---
5+
6+
A reactive store that combines the simplicity of signals with the power of "selectors" you'd find in Zustand or Redux.
7+
8+
```tsx
9+
import { store } from "@simplestack/store";
10+
11+
const documentStore = store({
12+
title: "Untitled",
13+
description: "Description",
14+
});
15+
16+
const title = documentStore.select("title");
17+
const description = documentStore.select("description");
18+
19+
title.set("New title");
20+
console.log(title.get()); // "New title"
21+
description.set("New description");
22+
console.log(description.get()); // "New description"
23+
```
24+
25+
## Installation
26+
27+
Install the dependency from npm:
28+
29+
```bash
30+
npm i @simplestack/store
31+
```
32+
33+
Then, import the store and use it in your component:
34+
35+
```tsx
36+
import { store } from "@simplestack/store";
37+
```
38+
39+
## Usage
40+
41+
Here's an overview of how stores are created, and how you can operate on parts of a store using `.select()`:
42+
43+
```tsx
44+
import { store } from "@simplestack/store";
45+
import { useStoreValue } from "@simplestack/store/react";
46+
47+
// Define your store with an initial state
48+
const documentStore = store({
49+
title: "Untitled",
50+
authors: ["Ada", "Ben"],
51+
meta: {
52+
pages: 3,
53+
tags: ["draft", "internal"],
54+
},
55+
});
56+
57+
// Select parts of a store to listen to individually
58+
const titleStore = documentStore.select("title");
59+
const tagsStore = documentStore.select("meta").select("tags");
60+
61+
function Document() {
62+
// Update your UI with the store's current state
63+
const { title, tags } = useStoreValue(documentStore);
64+
return (
65+
<div>
66+
{title} {tags.join(", ")}
67+
</div>
68+
);
69+
}
70+
71+
function Title() {
72+
// And scope updates with selected stores for fine-grained control
73+
const title = useStoreValue(titleStore);
74+
return (
75+
<input value={title} onChange={(e) => titleStore.set(e.target.value)} />
76+
);
77+
}
78+
```
79+
80+
## API
81+
82+
### store(initial)
83+
84+
Creates a store with `get`, `set`, `subscribe`, and (for objects and arrays) `select`.
85+
86+
- Parameters: `initial: number | string | boolean | null | undefined | object`
87+
- Returns: `Store<T>` where `T` is inferred from `initial` or supplied via generics
88+
89+
```ts
90+
import { store } from "@simplestack/store";
91+
92+
const counter = store(0);
93+
counter.set((n) => n + 1);
94+
console.log(counter.get()); // 1
95+
96+
// Select parts of a store for objects and arrays
97+
const doc = store({ title: "x" });
98+
const title = doc.select("title");
99+
```
100+
101+
### React
102+
103+
#### useStoreValue(store)
104+
105+
React hook to subscribe to a store and get its current value.
106+
107+
- Parameters: `store: Store<T> | undefined`
108+
- Returns: `T | undefined`
109+
110+
```tsx
111+
import { store } from "@simplestack/store";
112+
import { useStoreValue } from "@simplestack/store/react";
113+
114+
const counterStore = store(0);
115+
116+
function Counter() {
117+
const counter = useStoreValue(counterStore);
118+
return (
119+
<button onClick={() => counterStore.set((n) => n + 1)}>{counter}</button>
120+
);
121+
}
122+
```
123+
124+
## Type Reference
125+
126+
These types are exported for TypeScript users.
127+
128+
- StateObject: `Record<string | number | symbol, any>`
129+
- StatePrimitive: `string | number | boolean | null | undefined`
130+
- Setter<T>: `T | ((state: T) => T)`
131+
- Store<T>:
132+
- `get(): T`
133+
- `set(setter: Setter<T>): void`
134+
- `subscribe(callback: (state: T) => void): () => void`
135+
- `select(key: K): Store<SelectValue<T, K>>`: present only when `T` is an object or array
136+
137+
## Contributing
138+
139+
Contributions are welcome! Please feel free to submit an issue or pull request.
140+
141+
## License
142+
143+
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.

0 commit comments

Comments
 (0)