@manyducks.co/dolla 2.0.0-alpha.2 → 2.0.0-alpha.21
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 +168 -513
- package/dist/index.d.ts +12 -10
- package/dist/index.js +994 -727
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.js +2 -2
- package/dist/jsx-runtime.js +2 -2
- package/dist/markup.d.ts +39 -26
- package/dist/modules/dolla.d.ts +28 -14
- package/dist/modules/i18n.d.ts +129 -0
- package/dist/modules/render.d.ts +5 -8
- package/dist/modules/router.d.ts +8 -8
- package/dist/nodes/cond.d.ts +7 -7
- package/dist/nodes/html.d.ts +12 -6
- package/dist/nodes/observer.d.ts +9 -8
- package/dist/nodes/outlet.d.ts +8 -8
- package/dist/nodes/portal.d.ts +4 -4
- package/dist/nodes/repeat.d.ts +14 -14
- package/dist/nodes/text.d.ts +6 -6
- package/dist/passthrough-6Lrg96Id.js +1393 -0
- package/dist/passthrough-6Lrg96Id.js.map +1 -0
- package/dist/state.d.ts +151 -0
- package/dist/types.d.ts +12 -12
- package/dist/utils.d.ts +0 -1
- package/dist/view.d.ts +45 -9
- package/docs/http.md +5 -0
- package/docs/i18n.md +5 -0
- package/docs/router.md +5 -0
- package/docs/states.md +5 -0
- package/docs/views.md +5 -0
- package/notes/context-vars.md +21 -0
- package/notes/readme-scratch.md +222 -0
- package/notes/route-middleware.md +42 -0
- package/notes/scratch.md +93 -6
- package/package.json +13 -13
- package/tests/{signals.test.js → state.test.js} +6 -6
- package/dist/modules/language.d.ts +0 -41
- package/dist/passthrough-DrtCifRF.js +0 -1228
- package/dist/passthrough-DrtCifRF.js.map +0 -1
- package/dist/signals.d.ts +0 -101
package/README.md
CHANGED
|
@@ -3,119 +3,117 @@
|
|
|
3
3
|

|
|
4
4
|

|
|
5
5
|
|
|
6
|
-
> WARNING: This package is in active development. It may contain serious bugs and
|
|
6
|
+
> WARNING: This package is in active development. It may contain serious bugs and docs may be outdated or inaccurate. Use at your own risk.
|
|
7
7
|
|
|
8
8
|
Dolla is a batteries-included JavaScript frontend framework covering the needs of moderate-to-complex single page apps:
|
|
9
9
|
|
|
10
|
-
- Reactive DOM updates (Signals
|
|
11
|
-
- Reusable components (
|
|
12
|
-
-
|
|
13
|
-
- HTTP client
|
|
14
|
-
-
|
|
10
|
+
- ⚡ Reactive DOM updates with [State](). Inspired by Signals, but with more explicit tracking.
|
|
11
|
+
- 📦 Reusable components with [Views](#section-views).
|
|
12
|
+
- 🔀 Built in [routing]() with nested routes and middleware support (check login status, preload data, etc).
|
|
13
|
+
- 🐕 Built in [HTTP]() client with middleware support (set auth headers, etc).
|
|
14
|
+
- 📍 Built in [localization]() system (store translated strings in JSON files and call the `t` function to get them).
|
|
15
|
+
- 🍳 Build system optional. Write views in JSX or use `html` tagged template literals.
|
|
15
16
|
|
|
16
17
|
Let's first get into some examples.
|
|
17
18
|
|
|
18
|
-
##
|
|
19
|
+
## State
|
|
19
20
|
|
|
20
|
-
###
|
|
21
|
+
### Basic State API
|
|
21
22
|
|
|
22
23
|
```jsx
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
// Create a readable state and setter.
|
|
26
|
-
const [$count, setCount] = createSignal(0);
|
|
27
|
-
|
|
28
|
-
// Derive a new state from one or more states.
|
|
29
|
-
const $doubled = derive([$$count], (count) => count * 2);
|
|
30
|
-
```
|
|
24
|
+
import { createState, toState, valueOf, derive, createWatcher } from "@manyducks.co/dolla";
|
|
31
25
|
|
|
32
|
-
|
|
26
|
+
const [$count, setCount] = createState(72);
|
|
33
27
|
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
// Get value
|
|
29
|
+
$count.get(): // 72
|
|
36
30
|
|
|
37
|
-
|
|
31
|
+
// Replace the stored value with something else
|
|
32
|
+
setCount(300);
|
|
33
|
+
$count.get(); // 300
|
|
38
34
|
|
|
39
|
-
//
|
|
40
|
-
setCount(
|
|
35
|
+
// You can also pass a function that takes the current value and returns a new one
|
|
36
|
+
setCount((current) => current + 1);
|
|
37
|
+
$count.get(); // 301
|
|
41
38
|
|
|
42
|
-
//
|
|
43
|
-
const
|
|
44
|
-
const
|
|
39
|
+
// Derive a new state from one or more other states. Whenever $count changes, $doubled will follow.
|
|
40
|
+
const $doubled = derive([$count], (count) => count * 2);
|
|
41
|
+
const $sum = derive([$count, $doubled], (count, doubled) => count + doubled);
|
|
45
42
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
43
|
+
// Returns the value of a state. If the value is not a state it is returned as is.
|
|
44
|
+
const count = valueOf($count);
|
|
45
|
+
const bool = valueOf(true);
|
|
49
46
|
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
// Creates a state from a value. If the value is already a state it is returned as is.
|
|
48
|
+
const $bool = toState(true);
|
|
49
|
+
const $anotherCount = toState($count);
|
|
52
50
|
|
|
53
|
-
|
|
51
|
+
const watcher = createWatcher();
|
|
54
52
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
53
|
+
// Watch for changes to the value
|
|
54
|
+
const stop = watcher.watch([$count], (value) => [
|
|
55
|
+
// This function is called immediately with the current value, then again each time the value changes.
|
|
56
|
+
]);
|
|
57
|
+
stop(); // Stop watching for changes
|
|
60
58
|
|
|
61
|
-
setCount(1); // $count = 1, $doubled = 2
|
|
62
|
-
setCount(256); // $count = 256, $doubled = 512
|
|
63
|
-
setCount(-37); // $count = -37, $doubled = -74
|
|
64
59
|
```
|
|
65
60
|
|
|
66
|
-
|
|
61
|
+
States also come in a settable variety that includes the setter on the same object. Sometimes you want to pass around a two-way binding and this is what SettableState is for.
|
|
67
62
|
|
|
68
|
-
```
|
|
69
|
-
import
|
|
63
|
+
```jsx
|
|
64
|
+
import { createSettableState, fromSettable, toSettable } from "@manyducks.co/dolla";
|
|
70
65
|
|
|
71
|
-
|
|
72
|
-
|
|
66
|
+
// Settable states can be set by passing a value when they are called.
|
|
67
|
+
const $$value = createSettableState("Test");
|
|
68
|
+
$$value(); // "Test"
|
|
69
|
+
$$value("New Value");
|
|
70
|
+
$$value(); // "New Value"
|
|
73
71
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
72
|
+
// They can also be split into a State and Setter
|
|
73
|
+
const [$value, setValue] = fromSettableState($$value);
|
|
77
74
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
<p>Clicks: ${$count}</p>
|
|
81
|
-
<button onclick=${increment}>Click here to increment</button>
|
|
82
|
-
</div>
|
|
83
|
-
`;
|
|
84
|
-
}
|
|
75
|
+
// And a State and Setter can be combined into a SettableState.
|
|
76
|
+
const $$otherValue = toSettableState($value, setValue);
|
|
85
77
|
|
|
86
|
-
|
|
78
|
+
// Or discard the setter and make it read-only using the good old toState function:
|
|
79
|
+
const $value = toState($$value);
|
|
87
80
|
```
|
|
88
81
|
|
|
89
|
-
|
|
82
|
+
You can also do weird proxy things like this:
|
|
90
83
|
|
|
91
|
-
```
|
|
92
|
-
|
|
84
|
+
```jsx
|
|
85
|
+
// Create an original place for the state to live
|
|
86
|
+
const [$value, setValue] = createState(5);
|
|
93
87
|
|
|
94
|
-
|
|
95
|
-
|
|
88
|
+
// Derive a state that doubles the value
|
|
89
|
+
const $doubled = derive([$value], (value) => value * 2);
|
|
96
90
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
91
|
+
// Create a setter that takes the doubled value and sets the original $value accordingly.
|
|
92
|
+
const setDoubled = createSetter($doubled, (next, current) => {
|
|
93
|
+
setValue(next / 2);
|
|
94
|
+
});
|
|
100
95
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
<p>Clicks: ${$count}</p>
|
|
104
|
-
<button onclick=${increment}>Click here to increment</button>
|
|
105
|
-
</div>
|
|
106
|
-
`;
|
|
107
|
-
}
|
|
96
|
+
// Bundle the derived state and setter into a SettableState to pass around.
|
|
97
|
+
const $$doubled = toSettableState($doubled, setDoubled);
|
|
108
98
|
|
|
109
|
-
|
|
99
|
+
// Setting the doubled state...
|
|
100
|
+
$$doubled(100);
|
|
101
|
+
|
|
102
|
+
// ... will be reflected everywhere.
|
|
103
|
+
$$doubled(); // 100
|
|
104
|
+
$doubled(); // 100
|
|
105
|
+
$value(); // 50
|
|
110
106
|
```
|
|
111
107
|
|
|
112
|
-
|
|
108
|
+
## Views [id="section-views"]
|
|
109
|
+
|
|
110
|
+
A basic view:
|
|
113
111
|
|
|
114
112
|
```js
|
|
115
|
-
import Dolla, {
|
|
113
|
+
import Dolla, { createState, html } from "@manyducks.co/dolla";
|
|
116
114
|
|
|
117
115
|
function Counter(props, ctx) {
|
|
118
|
-
const [$count, setCount] =
|
|
116
|
+
const [$count, setCount] = createState(0);
|
|
119
117
|
|
|
120
118
|
function increment() {
|
|
121
119
|
setCount((count) => count + 1);
|
|
@@ -124,25 +122,17 @@ function Counter(props, ctx) {
|
|
|
124
122
|
return html`
|
|
125
123
|
<div>
|
|
126
124
|
<p>Clicks: ${$count}</p>
|
|
127
|
-
<button onclick=${increment}
|
|
125
|
+
<button onclick=${increment}>+1</button>
|
|
128
126
|
</div>
|
|
129
127
|
`;
|
|
130
128
|
}
|
|
131
129
|
|
|
132
|
-
Dolla.language.setup({
|
|
133
|
-
initialLanguage: "en",
|
|
134
|
-
languages: [
|
|
135
|
-
{ name: "en", strings: { buttonLabel: "Click here to increment" } },
|
|
136
|
-
{ name: "ja", strings: { buttonLabel: "ここに押して増加する" } },
|
|
137
|
-
],
|
|
138
|
-
});
|
|
139
|
-
|
|
140
130
|
Dolla.mount(document.body, Counter);
|
|
141
131
|
```
|
|
142
132
|
|
|
143
133
|
If you've ever used React before (and chances are you have if you're interested in obscure frameworks like this one) this should look very familiar to you.
|
|
144
134
|
|
|
145
|
-
The biggest difference is that the Counter function runs only once when the component is mounted. All updates after that point are a direct result of
|
|
135
|
+
The biggest difference is that the Counter function runs only once when the component is mounted. All updates after that point are a direct result of `$count` being updated.
|
|
146
136
|
|
|
147
137
|
## Advanced Componentry
|
|
148
138
|
|
|
@@ -155,10 +145,10 @@ Component functions take two arguments; props and a `Context` object. Props are
|
|
|
155
145
|
Props are values passed down from parent components. These can be static values, signals, callbacks and anything else the child component needs to do its job.
|
|
156
146
|
|
|
157
147
|
```tsx
|
|
158
|
-
import { type
|
|
148
|
+
import { type State, type Context, html } from "@manyducks.co/dolla";
|
|
159
149
|
|
|
160
150
|
type HeadingProps = {
|
|
161
|
-
$text:
|
|
151
|
+
$text: State<string>;
|
|
162
152
|
};
|
|
163
153
|
|
|
164
154
|
function Heading(props: HeadingProps, c: Context) {
|
|
@@ -179,10 +169,10 @@ function Layout() {
|
|
|
179
169
|
### Context
|
|
180
170
|
|
|
181
171
|
```tsx
|
|
182
|
-
import { type
|
|
172
|
+
import { type State, type Context, html } from "@manyducks.co/dolla";
|
|
183
173
|
|
|
184
174
|
type HeadingProps = {
|
|
185
|
-
$text:
|
|
175
|
+
$text: State<string>;
|
|
186
176
|
};
|
|
187
177
|
|
|
188
178
|
function Heading(props: HeadingProps, c: Context) {
|
|
@@ -216,7 +206,7 @@ function Heading(props: HeadingProps, c: Context) {
|
|
|
216
206
|
c.info("Heading has just been unmounted. Good time to finalize teardown.");
|
|
217
207
|
});
|
|
218
208
|
|
|
219
|
-
//
|
|
209
|
+
// States can be watched by the component context.
|
|
220
210
|
// Watchers created this way are cleaned up automatically when the component unmounts.
|
|
221
211
|
|
|
222
212
|
c.watch(props.$text, (value) => {
|
|
@@ -284,17 +274,17 @@ $selected.get(); // "Bon"
|
|
|
284
274
|
Proxy
|
|
285
275
|
|
|
286
276
|
```jsx
|
|
287
|
-
import {
|
|
277
|
+
import { createState, createProxyState } from "@manyducks.co/dolla";
|
|
288
278
|
|
|
289
|
-
const [$names, setNames] =
|
|
290
|
-
const [$index, setIndex] =
|
|
279
|
+
const [$names, setNames] = createState(["Morg", "Ton", "Bon"]);
|
|
280
|
+
const [$index, setIndex] = createState(0);
|
|
291
281
|
|
|
292
|
-
const [$selected, setSelected] =
|
|
282
|
+
const [$selected, setSelected] = createProxyState([$names, $index], {
|
|
293
283
|
get(names, index) {
|
|
294
284
|
return names[index];
|
|
295
285
|
},
|
|
296
|
-
set(next) {
|
|
297
|
-
const index =
|
|
286
|
+
set(next, names, _) {
|
|
287
|
+
const index = names.indexOf(next);
|
|
298
288
|
if (index === -1) {
|
|
299
289
|
throw new Error("Name is not in the list!");
|
|
300
290
|
}
|
|
@@ -313,137 +303,6 @@ $selected.get(); // "Ton"
|
|
|
313
303
|
$index.get(); // 1
|
|
314
304
|
```
|
|
315
305
|
|
|
316
|
-
##
|
|
317
|
-
|
|
318
|
-
States come in two varieties, each with a constructor function and a TypeScript type to match. These are:
|
|
319
|
-
|
|
320
|
-
- `Readable<T>`, which has only a `.get()` method that returns the current value.
|
|
321
|
-
- `Writable<T>`, which extends `Readable<T>` and adds a couple methods:
|
|
322
|
-
- `.set(value: T)` to replace the stored value.
|
|
323
|
-
- `.update(callback: (current: T) => T)` which takes a function that receives the current value and returns a new one.
|
|
324
|
-
|
|
325
|
-
The constructor functions are `$` for `Readable` and `$$` for `Writable`. By convention, the names of each are prefixed with `$` or `$$` to indicate its type, making the data flow a lot easier to understand at a glance.
|
|
326
|
-
|
|
327
|
-
```js
|
|
328
|
-
import { signal } from "@manyducks.co/dolla";
|
|
329
|
-
|
|
330
|
-
// By convention, Writable names are prefixed with two dollar signs and Readable with one.
|
|
331
|
-
const [$number, setNumber] = signal(5);
|
|
332
|
-
|
|
333
|
-
// Returns the current value held by the Writable.
|
|
334
|
-
$number.get();
|
|
335
|
-
// Stores a new value to the Writable.
|
|
336
|
-
setNumber(12);
|
|
337
|
-
// Uses a callback to update the value. Takes the current value and returns the next.
|
|
338
|
-
setNumber((current) => current + 1);
|
|
339
|
-
|
|
340
|
-
// Derive a new state from an existing one.
|
|
341
|
-
const $doubled = derive([$number], (value) => value * 2);
|
|
342
|
-
$doubled.get(); // 26 ($number is 13)
|
|
343
|
-
|
|
344
|
-
// Derive one new state from the latest values of many other states.
|
|
345
|
-
const $many = derive([$number, $doubled], (num, doubled) => num + doubled);
|
|
346
|
-
```
|
|
347
|
-
|
|
348
|
-
Now how do we use it? For a real example, a simple greeter app. The user types their name into a text input and that value is reflected in a heading above the input. For this we will use the `writable` function to create a state container. That container can be slotted into our JSX as a text node or DOM property. Any changes to the value will now be reflected in the DOM.
|
|
349
|
-
|
|
350
|
-
```jsx
|
|
351
|
-
import { signal } from "@manyducks.co/dolla";
|
|
352
|
-
|
|
353
|
-
function Greeter() {
|
|
354
|
-
const [$name, setName] = signal("Valued Customer");
|
|
355
|
-
|
|
356
|
-
return (
|
|
357
|
-
<section>
|
|
358
|
-
<header>
|
|
359
|
-
<h1>Hello, {$name}!</h1>
|
|
360
|
-
</header>
|
|
361
|
-
|
|
362
|
-
<input
|
|
363
|
-
value={$name}
|
|
364
|
-
onChange={(e) => {
|
|
365
|
-
setName(e.target.value);
|
|
366
|
-
}}
|
|
367
|
-
/>
|
|
368
|
-
</section>
|
|
369
|
-
);
|
|
370
|
-
}
|
|
371
|
-
```
|
|
372
|
-
|
|
373
|
-
### Computed
|
|
374
|
-
|
|
375
|
-
Computed states take one or more Readables or Writables and produce a new value _computed_ from those.
|
|
376
|
-
|
|
377
|
-
```js
|
|
378
|
-
import { $, $$ } from "@manyducks.co/dolla";
|
|
379
|
-
|
|
380
|
-
const $$count = $$(100);
|
|
381
|
-
|
|
382
|
-
const $double = $($$count, (value) => value * 2);
|
|
383
|
-
```
|
|
384
|
-
|
|
385
|
-
In that example, `$$double` will always have a value derived from that of `$$count`.
|
|
386
|
-
|
|
387
|
-
Let's look at a more typical example where we're basically joining two pieces of data; a list of users and the ID of the selected user.
|
|
388
|
-
|
|
389
|
-
```js
|
|
390
|
-
import { $, $$ } from "@manyducks.co/dolla";
|
|
391
|
-
|
|
392
|
-
// Let's assume this list of users was fetched from an API somewhere.
|
|
393
|
-
const $$people = $$([
|
|
394
|
-
{
|
|
395
|
-
id: 1,
|
|
396
|
-
name: "Borb",
|
|
397
|
-
},
|
|
398
|
-
{
|
|
399
|
-
id: 2,
|
|
400
|
-
name: "Bex",
|
|
401
|
-
},
|
|
402
|
-
{
|
|
403
|
-
id: 3,
|
|
404
|
-
name: "Bleeblop",
|
|
405
|
-
},
|
|
406
|
-
]);
|
|
407
|
-
|
|
408
|
-
// Let's assume this ID was chosen from an input where the above users were displayed.
|
|
409
|
-
const $$selectedId = $$(2);
|
|
410
|
-
|
|
411
|
-
// Now we get the object of the person who is selected.
|
|
412
|
-
const $selectedPerson = $($$people, $$selectedId, (people, selectedId) => {
|
|
413
|
-
return people.find((person) => person.id === selectedId);
|
|
414
|
-
});
|
|
415
|
-
|
|
416
|
-
// Now we get a Readable of just that person's name. Say we're going to display it on the page somewhere.
|
|
417
|
-
const $personName = $($selectedPerson, (person) => person.name);
|
|
418
|
-
|
|
419
|
-
console.log($personName.get()); // "Bex"
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
Notice that the structure above composes a data pipeline; if any of the data changes, so do the computed values, but the relationship between the data remains the same. Now that we've defined these relationships, `$selectedPerson` is always the person pointed to by `$$selectedId`. `$personName` is always the name of `$selectedPerson`, etc.
|
|
423
|
-
|
|
424
|
-
### Unwrap
|
|
425
|
-
|
|
426
|
-
The `unwrap` function returns the current value of a Readable or Writable, or if passed a non-Readable value returns that exact value. This function is used to guarantee you have a plain value when you may be dealing with either a container or a plain value.
|
|
427
|
-
|
|
428
|
-
```js
|
|
429
|
-
import { $, $$, unwrap } from "@manyducks.co/dolla";
|
|
430
|
-
|
|
431
|
-
const $$number = $$(5);
|
|
432
|
-
|
|
433
|
-
unwrap($$number); // 5
|
|
434
|
-
unwrap($(5)); // 5
|
|
435
|
-
unwrap(5); // 5
|
|
436
|
-
```
|
|
437
|
-
|
|
438
|
-
### Advanced Use Cases
|
|
439
|
-
|
|
440
|
-
<details>
|
|
441
|
-
<summary><code>observe</code> and <code>proxy</code></summary>
|
|
442
|
-
|
|
443
|
-
> TO DO
|
|
444
|
-
|
|
445
|
-
</details>
|
|
446
|
-
|
|
447
306
|
## Views
|
|
448
307
|
|
|
449
308
|
Views are what most frameworks would call Components. Dolla calls them Views because they deal specifically with stuff the user sees, and because Dolla also has another type of component called Stores that share data between views. We will get into those later.
|
|
@@ -530,7 +389,7 @@ The `repeat` helper repeats a render function for each item in a list. The `keyF
|
|
|
530
389
|
|
|
531
390
|
```jsx
|
|
532
391
|
function RepeatedListView() {
|
|
533
|
-
const $items =
|
|
392
|
+
const $items = Dolla.toState(["Squirrel", "Chipmunk", "Groundhog"]);
|
|
534
393
|
|
|
535
394
|
return (
|
|
536
395
|
<ul>
|
|
@@ -559,7 +418,7 @@ function PortalView() {
|
|
|
559
418
|
);
|
|
560
419
|
|
|
561
420
|
// Content will be appended to `document.body` while this view is connected.
|
|
562
|
-
return portal(
|
|
421
|
+
return portal(document.body, content);
|
|
563
422
|
}
|
|
564
423
|
```
|
|
565
424
|
|
|
@@ -637,18 +496,6 @@ function ExampleView() {
|
|
|
637
496
|
}
|
|
638
497
|
```
|
|
639
498
|
|
|
640
|
-
#### Using Stores
|
|
641
|
-
|
|
642
|
-
```jsx
|
|
643
|
-
import { UserStore } from "../stores/UserStore.js";
|
|
644
|
-
|
|
645
|
-
function ExampleView(props, ctx) {
|
|
646
|
-
const { $name } = ctx.getStore(UserStore);
|
|
647
|
-
|
|
648
|
-
return <h1>Hello {$name}!</h1>;
|
|
649
|
-
}
|
|
650
|
-
```
|
|
651
|
-
|
|
652
499
|
#### Observing States
|
|
653
500
|
|
|
654
501
|
The `observe` function starts observing when the view is connected and stops when disconnected. This takes care of cleaning up observers so you don't have to worry about memory leaks.
|
|
@@ -665,239 +512,6 @@ function ExampleView(props, ctx) {
|
|
|
665
512
|
}
|
|
666
513
|
```
|
|
667
514
|
|
|
668
|
-
#### Example: Counter View
|
|
669
|
-
|
|
670
|
-
Putting it all together, we have a view that maintains a counter. The user sees the current count displayed, and below it three buttons; one to increment by 1, one to decrement by 1, and one to reset the value to 0.
|
|
671
|
-
|
|
672
|
-
```jsx
|
|
673
|
-
import { $$ } from "@manyducks.co/dolla";
|
|
674
|
-
|
|
675
|
-
function CounterView(props, ctx) {
|
|
676
|
-
const $$count = $$(0);
|
|
677
|
-
|
|
678
|
-
function increment() {
|
|
679
|
-
$$count.update((n) => n + 1);
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
function decrement() {
|
|
683
|
-
$$count.update((n) => n - 1);
|
|
684
|
-
}
|
|
685
|
-
|
|
686
|
-
function reset() {
|
|
687
|
-
$$count.set(0);
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
return (
|
|
691
|
-
<div>
|
|
692
|
-
<p>The count is {$$count}</p>
|
|
693
|
-
<div>
|
|
694
|
-
<button onClick={increment}>+1</button>
|
|
695
|
-
<button onClick={decrement}>-1</button>
|
|
696
|
-
<button onClick={reset}>Reset</button>
|
|
697
|
-
</div>
|
|
698
|
-
</div>
|
|
699
|
-
);
|
|
700
|
-
}
|
|
701
|
-
```
|
|
702
|
-
|
|
703
|
-
## Stores
|
|
704
|
-
|
|
705
|
-
A store is a function that returns a plain JavaScript object. If this store is registered on the app, a single instance of the store is shared across all views and stores in the app. If the store is registered using a `StoreScope`, a single instance of the store is shared amongst all child elements of that `StoreScope`.
|
|
706
|
-
|
|
707
|
-
Stores are accessed with the `getStore` function available on the context object in views and other stores.
|
|
708
|
-
|
|
709
|
-
Stores are helpful for managing persistent state that needs to be accessed in many places.
|
|
710
|
-
|
|
711
|
-
```js
|
|
712
|
-
import { App } from "@manyducks.co/dolla";
|
|
713
|
-
|
|
714
|
-
const app = App({
|
|
715
|
-
view: LayoutView,
|
|
716
|
-
stores: [MessageStore],
|
|
717
|
-
});
|
|
718
|
-
|
|
719
|
-
// We define a store that just exports a message.
|
|
720
|
-
function MessageStore() {
|
|
721
|
-
return {
|
|
722
|
-
message: "Hello from the message store!",
|
|
723
|
-
};
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
// All instances of MessageView will share just one instance of MessageStore.
|
|
727
|
-
function MessageView(props, ctx) {
|
|
728
|
-
const store = ctx.getStore(MessageStore);
|
|
729
|
-
|
|
730
|
-
return <p>{store.message}</p>;
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
// And a layout view with five MessageViews inside.
|
|
734
|
-
function LayoutView() {
|
|
735
|
-
return (
|
|
736
|
-
<div>
|
|
737
|
-
<h1>Title</h1>
|
|
738
|
-
<MessageView />
|
|
739
|
-
<MessageView />
|
|
740
|
-
<MessageView />
|
|
741
|
-
<MessageView />
|
|
742
|
-
<MessageView />
|
|
743
|
-
</div>
|
|
744
|
-
);
|
|
745
|
-
}
|
|
746
|
-
|
|
747
|
-
// Connect the app.
|
|
748
|
-
app.connect("#app");
|
|
749
|
-
```
|
|
750
|
-
|
|
751
|
-
The output:
|
|
752
|
-
|
|
753
|
-
```html
|
|
754
|
-
<div id="app">
|
|
755
|
-
<div>
|
|
756
|
-
<h1>Title</h1>
|
|
757
|
-
<p>Hello from the message store!</p>
|
|
758
|
-
<p>Hello from the message store!</p>
|
|
759
|
-
<p>Hello from the message store!</p>
|
|
760
|
-
<p>Hello from the message store!</p>
|
|
761
|
-
<p>Hello from the message store!</p>
|
|
762
|
-
</div>
|
|
763
|
-
</div>
|
|
764
|
-
```
|
|
765
|
-
|
|
766
|
-
### StoreScope
|
|
767
|
-
|
|
768
|
-
Stores relevant to only a part of the view tree can be scoped using a `StoreScope`.
|
|
769
|
-
|
|
770
|
-
```jsx
|
|
771
|
-
function ExampleStore() {
|
|
772
|
-
return { value: 5 };
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
function ExampleView(props, ctx) {
|
|
776
|
-
const store = ctx.getStore(ExampleStore);
|
|
777
|
-
|
|
778
|
-
return <div>{store.value}</div>;
|
|
779
|
-
}
|
|
780
|
-
|
|
781
|
-
function LayoutView() {
|
|
782
|
-
return (
|
|
783
|
-
<StoreScope stores={[ExampleStore]}>
|
|
784
|
-
<ExampleView />
|
|
785
|
-
</StoreScope>
|
|
786
|
-
);
|
|
787
|
-
}
|
|
788
|
-
```
|
|
789
|
-
|
|
790
|
-
## Apps and Routing
|
|
791
|
-
|
|
792
|
-
```jsx
|
|
793
|
-
import { App } from "@manyducks.co/dolla";
|
|
794
|
-
|
|
795
|
-
const app = App({
|
|
796
|
-
// Debug options control what gets printed from messages logged through view and store contexts.
|
|
797
|
-
debug: {
|
|
798
|
-
// A comma-separated list of filters. '*' means allow everything and '-dolla/*' means suppress messages with labels beginning with 'dolla/'.
|
|
799
|
-
filter: "*,-dolla/*",
|
|
800
|
-
|
|
801
|
-
// Never print ctx.info() messages
|
|
802
|
-
info: false,
|
|
803
|
-
|
|
804
|
-
// Only print ctx.log() and ctx.warn() messages in development mode
|
|
805
|
-
log: "development",
|
|
806
|
-
warn: "development",
|
|
807
|
-
|
|
808
|
-
// Always print ctx.error() messages
|
|
809
|
-
error: true,
|
|
810
|
-
},
|
|
811
|
-
|
|
812
|
-
mode: "development", // or "production" (enables additional debug features and logging in "development")
|
|
813
|
-
|
|
814
|
-
view: (_, ctx) => {
|
|
815
|
-
// Define a custom root view. By default this just renders any routes like so:
|
|
816
|
-
return ctx.outlet();
|
|
817
|
-
},
|
|
818
|
-
});
|
|
819
|
-
```
|
|
820
|
-
|
|
821
|
-
#### Routes and Outlets
|
|
822
|
-
|
|
823
|
-
The main view (defined with the app's `main` method) is the top-level view that will always be displayed while the app is connected.
|
|
824
|
-
|
|
825
|
-
```jsx
|
|
826
|
-
// Here is an app with a hypothetical main view with a layout and navigation:
|
|
827
|
-
const app = App({
|
|
828
|
-
view: (_, ctx) => {
|
|
829
|
-
return (
|
|
830
|
-
<div class="todo-layout">
|
|
831
|
-
<nav>
|
|
832
|
-
<ul>
|
|
833
|
-
<li>
|
|
834
|
-
<a href="/tasks">Tasks</a>
|
|
835
|
-
</li>
|
|
836
|
-
<li>
|
|
837
|
-
<a href="/completed">Completed</a>
|
|
838
|
-
</li>
|
|
839
|
-
</ul>
|
|
840
|
-
</nav>
|
|
841
|
-
{/*
|
|
842
|
-
* An outlet is where children of a view are shown.
|
|
843
|
-
* Because this is a main view, children in this case
|
|
844
|
-
* are the views that correspond to matched routes.
|
|
845
|
-
*/}
|
|
846
|
-
{ctx.outlet()}
|
|
847
|
-
</div>
|
|
848
|
-
);
|
|
849
|
-
},
|
|
850
|
-
|
|
851
|
-
stores: [
|
|
852
|
-
{
|
|
853
|
-
store: RouterStore,
|
|
854
|
-
options: {
|
|
855
|
-
hash: true, // Use hash-based routing (default false)
|
|
856
|
-
|
|
857
|
-
// Here are a couple of routes to be rendered into our layout:
|
|
858
|
-
routes: [
|
|
859
|
-
{ path: "/tasks", view: TasksView },
|
|
860
|
-
{ path: "/completed", view: CompletedView },
|
|
861
|
-
],
|
|
862
|
-
},
|
|
863
|
-
},
|
|
864
|
-
],
|
|
865
|
-
});
|
|
866
|
-
```
|
|
867
|
-
|
|
868
|
-
Routes can also be nested. Just like the main view and its routes, subroutes will be displayed in the outlet of their parent view.
|
|
869
|
-
|
|
870
|
-
```jsx
|
|
871
|
-
const app = App({
|
|
872
|
-
stores: [
|
|
873
|
-
{
|
|
874
|
-
store: RouterStore,
|
|
875
|
-
options: {
|
|
876
|
-
routes: [
|
|
877
|
-
{
|
|
878
|
-
path: "/tasks",
|
|
879
|
-
view: TasksView,
|
|
880
|
-
routes: [
|
|
881
|
-
{ path: "/", view: TaskListView },
|
|
882
|
-
|
|
883
|
-
// In routes, `{value}` is a dynamic value that matches anything,
|
|
884
|
-
// and `{#value}` is a dynamic value that matches a number.
|
|
885
|
-
{ path: "/{#id}", view: TaskDetailsView },
|
|
886
|
-
{ path: "/{#id}/edit", view: TaskEditView },
|
|
887
|
-
|
|
888
|
-
// If the route is any other than the ones defined above, redirect to the list.
|
|
889
|
-
// Redirects support './' and '../' style relative paths.
|
|
890
|
-
{ path: "*", redirect: "./" },
|
|
891
|
-
],
|
|
892
|
-
},
|
|
893
|
-
{ path: "/completed", view: CompletedView },
|
|
894
|
-
],
|
|
895
|
-
},
|
|
896
|
-
},
|
|
897
|
-
],
|
|
898
|
-
});
|
|
899
|
-
```
|
|
900
|
-
|
|
901
515
|
#### Routing
|
|
902
516
|
|
|
903
517
|
Dolla makes heavy use of client-side routing. You can define as many routes as you have views, and the URL
|
|
@@ -925,30 +539,23 @@ to your code (`router` store, `$params` readable). Below are some examples of pa
|
|
|
925
539
|
Now, here are some route examples in the context of an app:
|
|
926
540
|
|
|
927
541
|
```js
|
|
928
|
-
import
|
|
929
|
-
import { PersonDetails, ThingIndex, ThingDetails, ThingEdit, ThingDelete } from "./
|
|
542
|
+
import Dolla from "@manyducks.co/dolla";
|
|
543
|
+
import { PersonDetails, ThingIndex, ThingDetails, ThingEdit, ThingDelete } from "./views.js";
|
|
930
544
|
|
|
931
|
-
|
|
932
|
-
|
|
545
|
+
Dolla.router.setup({
|
|
546
|
+
routes: [
|
|
547
|
+
{ path: "/people/{name}", view: PersonDetails },
|
|
933
548
|
{
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
{ path: "/", view: ThingIndex }, // matches `/things`
|
|
945
|
-
{ path: "/{#id}", view: ThingDetails }, // matches `/things/{#id}`
|
|
946
|
-
{ path: "/{#id}/edit", view: ThingEdit }, // matches `/things/{#id}/edit`
|
|
947
|
-
{ path: "/{#id}/delete", view: ThingDelete }, // matches `/things/{#id}/delete`
|
|
948
|
-
],
|
|
949
|
-
},
|
|
950
|
-
],
|
|
951
|
-
},
|
|
549
|
+
// A `null` component with subroutes acts as a namespace for those subroutes.
|
|
550
|
+
// Passing a view instead of `null` results in subroutes being rendered inside that view wherever `ctx.outlet()` is called.
|
|
551
|
+
path: "/things",
|
|
552
|
+
view: null,
|
|
553
|
+
routes: [
|
|
554
|
+
{ path: "/", view: ThingIndex }, // matches `/things`
|
|
555
|
+
{ path: "/{#id}", view: ThingDetails }, // matches `/things/{#id}`
|
|
556
|
+
{ path: "/{#id}/edit", view: ThingEdit }, // matches `/things/{#id}/edit`
|
|
557
|
+
{ path: "/{#id}/delete", view: ThingDelete }, // matches `/things/{#id}/delete`
|
|
558
|
+
],
|
|
952
559
|
},
|
|
953
560
|
],
|
|
954
561
|
});
|
|
@@ -956,41 +563,89 @@ const app = App({
|
|
|
956
563
|
|
|
957
564
|
As you may have inferred from the code above, when the URL matches a pattern the corresponding view is displayed. If we
|
|
958
565
|
visit `/people/john`, we will see the `PersonDetails` view and the params will be `{ name: "john" }`. Params can be
|
|
959
|
-
accessed
|
|
566
|
+
accessed anywhere through `Dolla.router`.
|
|
960
567
|
|
|
961
568
|
```js
|
|
962
569
|
function PersonDetails(props, ctx) {
|
|
963
|
-
// `router` store allows you to work with the router from inside the app.
|
|
964
|
-
const router = ctx.getStore(RouterStore);
|
|
965
|
-
|
|
966
570
|
// Info about the current route is exported as a set of Readables. Query params are also Writable through $$query:
|
|
967
|
-
const { $path, $pattern, $params,
|
|
571
|
+
const { $path, $pattern, $params, $query } = Dolla.router;
|
|
968
572
|
|
|
969
|
-
//
|
|
970
|
-
|
|
573
|
+
Dolla.router.back(); // Step back in the history to the previous route, if any.
|
|
574
|
+
Dolla.router.back(2); // Hit the back button twice.
|
|
971
575
|
|
|
972
|
-
|
|
973
|
-
|
|
576
|
+
Dolla.router.forward(); // Step forward in the history to the next route, if any.
|
|
577
|
+
Dolla.router.forward(4); // Hit the forward button 4 times.
|
|
974
578
|
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
navigate("/things/152"); // Navigate to another path within the same app.
|
|
979
|
-
navigate("https://www.example.com/another/site"); // Navigate to another domain entirely.
|
|
579
|
+
Dolla.router.go("/things/152"); // Navigate to another path within the same app.
|
|
580
|
+
Dolla.router.go("https://www.example.com/another/site"); // Navigate to another domain entirely.
|
|
980
581
|
|
|
981
582
|
// Three ways to confirm with the user that they wish to navigate before actually doing it.
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
583
|
+
Dolla.router.go("/another/page", { prompt: true });
|
|
584
|
+
Dolla.router.go("/another/page", { prompt: "Are you sure you want to leave and go to /another/page?" });
|
|
585
|
+
Dolla.router.go("/another/page", { prompt: PromptView });
|
|
985
586
|
|
|
986
587
|
// Get the live value of `{name}` from the current path.
|
|
987
|
-
const $name =
|
|
588
|
+
const $name = Dolla.derive([$params], (p) => p.name);
|
|
988
589
|
|
|
989
590
|
// Render it into a <p> tag. The name portion will update if the URL changes.
|
|
990
591
|
return <p>The person is: {$name}</p>;
|
|
991
592
|
}
|
|
992
593
|
```
|
|
993
594
|
|
|
595
|
+
## HTTP Client
|
|
596
|
+
|
|
597
|
+
```js
|
|
598
|
+
// Middleware!
|
|
599
|
+
Dolla.http.use((request, next) => {
|
|
600
|
+
// Add auth header for all requests going to the API.
|
|
601
|
+
if (request.url.pathname.startsWith("/api")) {
|
|
602
|
+
request.headers.set("authorization", `Bearer ${authToken}`);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
const response = await next();
|
|
606
|
+
|
|
607
|
+
// Could do something with the response here.
|
|
608
|
+
|
|
609
|
+
return response;
|
|
610
|
+
});
|
|
611
|
+
|
|
612
|
+
const exampleResponse = await Dolla.http.get("/api/example");
|
|
613
|
+
|
|
614
|
+
// Body is already parsed from JSON into an object.
|
|
615
|
+
exampleResponse.body.someValue;
|
|
616
|
+
```
|
|
617
|
+
|
|
618
|
+
## Localization
|
|
619
|
+
|
|
620
|
+
```js
|
|
621
|
+
import Dolla, { html, t } from "@manyducks.co/dolla";
|
|
622
|
+
|
|
623
|
+
function Counter(props, ctx) {
|
|
624
|
+
const [$count, setCount] = Dolla.createState(0);
|
|
625
|
+
|
|
626
|
+
function increment() {
|
|
627
|
+
setCount((count) => count + 1);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
return html`
|
|
631
|
+
<div>
|
|
632
|
+
<p>Clicks: ${$count}</p>
|
|
633
|
+
<button onclick=${increment}>${t("buttonLabel")}</button>
|
|
634
|
+
</div>
|
|
635
|
+
`;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
Dolla.i18n.setup({
|
|
639
|
+
locale: "en",
|
|
640
|
+
translations: [
|
|
641
|
+
{ locale: "en", strings: { buttonLabel: "Click here to increment" } },
|
|
642
|
+
{ locale: "ja", strings: { buttonLabel: "ここに押して増加する" } },
|
|
643
|
+
],
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
Dolla.mount(document.body, Counter);
|
|
647
|
+
```
|
|
648
|
+
|
|
994
649
|
---
|
|
995
650
|
|
|
996
651
|
[🦆](https://www.manyducks.co)
|