@manyducks.co/dolla 2.0.0-alpha.28 → 2.0.0-alpha.29
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 +29 -527
- package/dist/core/nodes/repeat.d.ts +1 -1
- package/dist/core/nodes/view.d.ts +4 -0
- package/dist/core/store.d.ts +18 -1
- package/dist/index.js +60 -56
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.js +2 -2
- package/dist/jsx-runtime.js +2 -2
- package/dist/{passthrough-1MySicTa.js → passthrough-eH8w4zJi.js} +471 -436
- package/dist/passthrough-eH8w4zJi.js.map +1 -0
- package/docs/http.md +8 -0
- package/docs/i18n.md +35 -2
- package/docs/index.md +10 -0
- package/docs/router.md +65 -12
- package/docs/setup.md +8 -0
- package/docs/state.md +11 -3
- package/docs/stores.md +62 -0
- package/docs/views.md +305 -2
- package/package.json +1 -1
- package/dist/passthrough-1MySicTa.js.map +0 -1
package/README.md
CHANGED
|
@@ -7,23 +7,37 @@
|
|
|
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 with [State](./docs/state.md). Inspired by Signals, but with
|
|
10
|
+
- ⚡ Reactive DOM updates with [State](./docs/state.md). Inspired by Signals, but with explicit tracking.
|
|
11
11
|
- 📦 Reusable components with [Views](./docs/views.md).
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
12
|
+
- 💾 Reusable state management with [Stores](./docs/stores.md).
|
|
13
|
+
- 🔀 Built-in [routing](./docs/router.md) with nested routes and middleware support (check login status, preload data, etc).
|
|
14
|
+
- 🐕 Built-in [HTTP](./docs/http.md) client with middleware support (set auth headers, etc).
|
|
15
|
+
- 📍 Built-in [localization](./docs/i18n.md) system (store translated strings in JSON files and call the `t` function to get them).
|
|
15
16
|
- 🍳 Build system optional. [Write views in JSX](./docs/setup.md) or use `html` tagged template literals.
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
Dolla's goals include:
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
- Be fun to create with.
|
|
21
|
+
- Be snappy and responsive for real life apps.
|
|
22
|
+
- Be compact as possible but not at the expense of necessary features.
|
|
20
23
|
|
|
21
|
-
|
|
24
|
+
## Why Dolla?
|
|
25
|
+
|
|
26
|
+
> TODO: Write about why Dolla was started and what it's all about.
|
|
27
|
+
|
|
28
|
+
- Borne of frustration using React and similar libs (useEffect, referential equality, a pain to integrate other libs into its lifecycle, need to hunt for libraries to move beyond Hello World).
|
|
29
|
+
- Merges ideas from my favorite libraries and frameworks (Solid/Knockout, Choo, Svelte, i18next, etc) into one curated set designed to work well together.
|
|
30
|
+
- Opinionated (with the _correct_ opinions).
|
|
31
|
+
- Many mainstream libraries seem too big for what they do. The entirety of Dolla is less than half the size of [`react-router`](https://bundlephobia.com/package/react-router@7.1.5).
|
|
32
|
+
|
|
33
|
+
## An Example
|
|
34
|
+
|
|
35
|
+
A basic view. Note that the view function is called exactly once when the view is first mounted. All changes to DOM nodes thereafter happen as a result of `$state` values changing.
|
|
22
36
|
|
|
23
37
|
```js
|
|
24
|
-
import Dolla, { createState } from "@manyducks.co/dolla";
|
|
38
|
+
import Dolla, { createState, createView } from "@manyducks.co/dolla";
|
|
25
39
|
|
|
26
|
-
|
|
40
|
+
const Counter = createView(function (props) {
|
|
27
41
|
const [$count, setCount] = createState(0);
|
|
28
42
|
|
|
29
43
|
function increment() {
|
|
@@ -43,532 +57,20 @@ function Counter(props, ctx) {
|
|
|
43
57
|
<p>Clicks: {$count}</p>
|
|
44
58
|
<div>
|
|
45
59
|
<button onClick={decrement}>-1</button>
|
|
46
|
-
<button onClick={reset}>
|
|
47
|
-
<button onClick={increment}
|
|
60
|
+
<button onClick={reset}>Reset</button>
|
|
61
|
+
<button onClick={increment}>+1</button>
|
|
48
62
|
</div>
|
|
49
63
|
</div>
|
|
50
64
|
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
Dolla.mount(document.body, Counter);
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
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.
|
|
57
|
-
|
|
58
|
-
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.
|
|
59
|
-
|
|
60
|
-
## Advanced Componentry
|
|
61
|
-
|
|
62
|
-
Component functions take two arguments; props and a `Context` object. Props are passed from parent components to child components, and `Context` is provided by the app.
|
|
63
|
-
|
|
64
|
-
> The following examples are shown in TypeScript for clarity. Feel free to omit the type annotations in your own code if you prefer vanilla JS.
|
|
65
|
-
|
|
66
|
-
### Props
|
|
67
|
-
|
|
68
|
-
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.
|
|
69
|
-
|
|
70
|
-
```tsx
|
|
71
|
-
import { type State, type Context, html } from "@manyducks.co/dolla";
|
|
72
|
-
|
|
73
|
-
type HeadingProps = {
|
|
74
|
-
$text: State<string>;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
function Heading(props: HeadingProps, c: Context) {
|
|
78
|
-
return html`<h1>${props.$text}</h1>`;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function Layout() {
|
|
82
|
-
const [$text, setText] = signal("HELLO THERE!");
|
|
83
|
-
|
|
84
|
-
return (
|
|
85
|
-
<section>
|
|
86
|
-
<Heading $text={$text}>
|
|
87
|
-
</section>
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### Context
|
|
93
|
-
|
|
94
|
-
```tsx
|
|
95
|
-
import { type State, type Context, html } from "@manyducks.co/dolla";
|
|
96
|
-
|
|
97
|
-
type HeadingProps = {
|
|
98
|
-
$text: State<string>;
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
function Heading(props: HeadingProps, c: Context) {
|
|
102
|
-
// A full compliment of logging functions:
|
|
103
|
-
// Log levels that get printed can be set at the app level.
|
|
104
|
-
|
|
105
|
-
c.trace("What's going on? Let's find out.");
|
|
106
|
-
c.info("This is low priority info.");
|
|
107
|
-
c.log("This is normal priority info.");
|
|
108
|
-
c.warn("Hey! This could be serious.");
|
|
109
|
-
c.error("NOT GOOD! DEFINITELY NOT GOOD!!1");
|
|
110
|
-
|
|
111
|
-
// And sometimes things are just too borked to press on:
|
|
112
|
-
c.crash(new Error("STOP THE PRESSES! BURN IT ALL DOWN!!!"));
|
|
113
|
-
|
|
114
|
-
// The four lifecycle hooks:
|
|
115
|
-
|
|
116
|
-
// c.beforeMount(() => {
|
|
117
|
-
// c.info("Heading is going to be mounted. Good time to set things up.");
|
|
118
|
-
// });
|
|
119
|
-
|
|
120
|
-
c.onMount(() => {
|
|
121
|
-
c.info("Heading has just been mounted. Good time to access the DOM and finalize setup.");
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
// c.beforeUnmount(() => {
|
|
125
|
-
// c.info("Heading is going to be unmounted. Good time to begin teardown.");
|
|
126
|
-
// });
|
|
127
|
-
|
|
128
|
-
c.onUnmount(() => {
|
|
129
|
-
c.info("Heading has just been unmounted. Good time to finalize teardown.");
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
// States can be watched by the component context.
|
|
133
|
-
// Watchers created this way are cleaned up automatically when the component unmounts.
|
|
134
|
-
|
|
135
|
-
c.watch(props.$text, (value) => {
|
|
136
|
-
c.warn(`text has changed to: ${value}`);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
return html`<h1>${props.$text}</h1>`;
|
|
140
|
-
}
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
## Signals
|
|
144
|
-
|
|
145
|
-
Basics
|
|
146
|
-
|
|
147
|
-
```jsx
|
|
148
|
-
const [$count, setCount] = signal(0);
|
|
149
|
-
|
|
150
|
-
// Set the value directly.
|
|
151
|
-
setCount(1);
|
|
152
|
-
setCount(2);
|
|
153
|
-
|
|
154
|
-
// Transform the previous value into a new one.
|
|
155
|
-
setCount((current) => current + 1);
|
|
156
|
-
|
|
157
|
-
// This can be used to create easy helper functions:
|
|
158
|
-
function increment(amount = 1) {
|
|
159
|
-
setCount((current) => current + amount);
|
|
160
|
-
}
|
|
161
|
-
increment();
|
|
162
|
-
increment(5);
|
|
163
|
-
increment(-362);
|
|
164
|
-
|
|
165
|
-
// Get the current value
|
|
166
|
-
$count.get(); // -354
|
|
167
|
-
|
|
168
|
-
// Watch for new values. Don't forget to call stop() to clean up!
|
|
169
|
-
const stop = $count.watch((current) => {
|
|
170
|
-
console.log(`count is now ${current}`);
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
increment(); // "count is now -353"
|
|
174
|
-
increment(); // "count is now -352"
|
|
175
|
-
|
|
176
|
-
stop();
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
Derive
|
|
180
|
-
|
|
181
|
-
```jsx
|
|
182
|
-
import { signal, derive } from "@manyducks.co/dolla";
|
|
183
|
-
|
|
184
|
-
const [$names, setNames] = signal(["Morg", "Ton", "Bon"]);
|
|
185
|
-
const [$index, setIndex] = signal(0);
|
|
186
|
-
|
|
187
|
-
// Create a new signal that depends on two existing signals:
|
|
188
|
-
const $selected = derive([$names, $index], (names, index) => names[index]);
|
|
189
|
-
|
|
190
|
-
$selected.get(); // "Morg"
|
|
191
|
-
|
|
192
|
-
setIndex(2);
|
|
193
|
-
|
|
194
|
-
$selected.get(); // "Bon"
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
Proxy
|
|
198
|
-
|
|
199
|
-
```jsx
|
|
200
|
-
import { createState, createProxyState } from "@manyducks.co/dolla";
|
|
201
|
-
|
|
202
|
-
const [$names, setNames] = createState(["Morg", "Ton", "Bon"]);
|
|
203
|
-
const [$index, setIndex] = createState(0);
|
|
204
|
-
|
|
205
|
-
const [$selected, setSelected] = createProxyState([$names, $index], {
|
|
206
|
-
get(names, index) {
|
|
207
|
-
return names[index];
|
|
208
|
-
},
|
|
209
|
-
set(next, names, _) {
|
|
210
|
-
const index = names.indexOf(next);
|
|
211
|
-
if (index === -1) {
|
|
212
|
-
throw new Error("Name is not in the list!");
|
|
213
|
-
}
|
|
214
|
-
setIndex(index);
|
|
215
|
-
},
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
$selected.get(); // "Morg"
|
|
219
|
-
$index.get(); // 0
|
|
220
|
-
|
|
221
|
-
// Set selected directly by name through the proxy.
|
|
222
|
-
setSelected("Ton");
|
|
223
|
-
|
|
224
|
-
// Selected and the index have been updated to match.
|
|
225
|
-
$selected.get(); // "Ton"
|
|
226
|
-
$index.get(); // 1
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
## Views
|
|
230
|
-
|
|
231
|
-
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.
|
|
232
|
-
|
|
233
|
-
At its most basic, a view is a function that returns elements.
|
|
234
|
-
|
|
235
|
-
```jsx
|
|
236
|
-
function ExampleView() {
|
|
237
|
-
return <h1>Hello World!</h1>;
|
|
238
|
-
}
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
#### View Props
|
|
242
|
-
|
|
243
|
-
A view function takes a `props` object as its first argument. This object contains all properties passed to the view when it's invoked.
|
|
244
|
-
|
|
245
|
-
```js
|
|
246
|
-
import { html } from "@manyducks.co/dolla";
|
|
247
|
-
|
|
248
|
-
function ListView(props, ctx) {
|
|
249
|
-
return html`
|
|
250
|
-
<ul>
|
|
251
|
-
<${ListItemView} label="Squirrel" />
|
|
252
|
-
<${ListItemView} label="Chipmunk" />
|
|
253
|
-
<${ListItemView} label="Groundhog" />
|
|
254
|
-
</ul>
|
|
255
|
-
`;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
function ListItemView(props, ctx) {
|
|
259
|
-
return html`<li>${props.label}</li>`;
|
|
260
|
-
}
|
|
261
|
-
```
|
|
262
|
-
|
|
263
|
-
```jsx
|
|
264
|
-
function ListView() {
|
|
265
|
-
return (
|
|
266
|
-
<ul>
|
|
267
|
-
<ListItemView label="Squirrel" />
|
|
268
|
-
<ListItemView label="Chipmunk" />
|
|
269
|
-
<ListItemView label="Groundhog" />
|
|
270
|
-
</ul>
|
|
271
|
-
);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
function ListItemView(props) {
|
|
275
|
-
return <li>{props.label}</li>;
|
|
276
|
-
}
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
As you may have guessed, you can pass States as props and slot them in in exactly the same way. This is important because Views do not re-render the way you might expect from other frameworks. Whatever you pass as props is what the View gets for its entire lifecycle.
|
|
280
|
-
|
|
281
|
-
### View Helpers
|
|
282
|
-
|
|
283
|
-
#### `cond($condition, whenTruthy, whenFalsy)`
|
|
284
|
-
|
|
285
|
-
The `cond` helper does conditional rendering. When `$condition` is truthy, the second argument is rendered. When `$condition` is falsy the third argument is rendered. Either case can be left null or undefined if you don't want to render something for that condition.
|
|
286
|
-
|
|
287
|
-
```jsx
|
|
288
|
-
function ConditionalListView({ $show }) {
|
|
289
|
-
return (
|
|
290
|
-
<div>
|
|
291
|
-
{cond(
|
|
292
|
-
$show,
|
|
293
|
-
|
|
294
|
-
// Visible when truthy
|
|
295
|
-
<ul>
|
|
296
|
-
<ListItemView label="Squirrel" />
|
|
297
|
-
<ListItemView label="Chipmunk" />
|
|
298
|
-
<ListItemView label="Groundhog" />
|
|
299
|
-
</ul>,
|
|
300
|
-
|
|
301
|
-
// Visible when falsy
|
|
302
|
-
<span>List is hidden</span>,
|
|
303
|
-
)}
|
|
304
|
-
</div>
|
|
305
|
-
);
|
|
306
|
-
}
|
|
307
|
-
```
|
|
308
|
-
|
|
309
|
-
#### `repeat($items, keyFn, renderFn)`
|
|
310
|
-
|
|
311
|
-
The `repeat` helper repeats a render function for each item in a list. The `keyFn` takes an item's value and returns a number, string or Symbol that uniquely identifies that list item. If `$items` changes or gets reordered, all rendered items with matching keys will be reused, those no longer in the list will be removed and those that didn't previously have a matching key are created.
|
|
312
|
-
|
|
313
|
-
```jsx
|
|
314
|
-
function RepeatedListView() {
|
|
315
|
-
const $items = Dolla.toState(["Squirrel", "Chipmunk", "Groundhog"]);
|
|
316
|
-
|
|
317
|
-
return (
|
|
318
|
-
<ul>
|
|
319
|
-
{repeat(
|
|
320
|
-
$items,
|
|
321
|
-
(item) => item, // Using the string itself as the key
|
|
322
|
-
($item, $index, ctx) => {
|
|
323
|
-
return <ListItemView label={$item} />;
|
|
324
|
-
},
|
|
325
|
-
)}
|
|
326
|
-
</ul>
|
|
327
|
-
);
|
|
328
|
-
}
|
|
329
|
-
```
|
|
330
|
-
|
|
331
|
-
#### `portal(content, parentNode)`
|
|
332
|
-
|
|
333
|
-
The `portal` helper displays DOM elements from a view as children of a parent element elsewhere in the document. Portals are typically used to display modals and other content that needs to appear at the top level of a document.
|
|
334
|
-
|
|
335
|
-
```jsx
|
|
336
|
-
function PortalView() {
|
|
337
|
-
const content = (
|
|
338
|
-
<div class="modal">
|
|
339
|
-
<p>This is a modal.</p>
|
|
340
|
-
</div>
|
|
341
|
-
);
|
|
342
|
-
|
|
343
|
-
// Content will be appended to `document.body` while this view is connected.
|
|
344
|
-
return portal(document.body, content);
|
|
345
|
-
}
|
|
346
|
-
```
|
|
347
|
-
|
|
348
|
-
### View Context
|
|
349
|
-
|
|
350
|
-
A view function takes a context object as its second argument. The context provides a set of functions you can use to respond to lifecycle events, observe dynamic data, print debug messages and display child elements among other things.
|
|
351
|
-
|
|
352
|
-
#### Printing Debug Messages
|
|
353
|
-
|
|
354
|
-
```jsx
|
|
355
|
-
function ExampleView(props, ctx) {
|
|
356
|
-
// Set the name of this view's context. Console messages are prefixed with name.
|
|
357
|
-
ctx.name = "CustomName";
|
|
358
|
-
|
|
359
|
-
// Print messages to the console. These are suppressed by default in the app's "production" mode.
|
|
360
|
-
// You can also change which of these are printed and filter messages from certain contexts in the `createApp` options object.
|
|
361
|
-
ctx.info("Verbose debugging info that might be useful to know");
|
|
362
|
-
ctx.log("Standard messages");
|
|
363
|
-
ctx.warn("Something bad might be happening");
|
|
364
|
-
ctx.error("Uh oh!");
|
|
365
|
-
|
|
366
|
-
// If you encounter a bad enough situation, you can halt and disconnect the entire app.
|
|
367
|
-
ctx.crash(new Error("BOOM"));
|
|
368
|
-
|
|
369
|
-
return <h1>Hello World!</h1>;
|
|
370
|
-
}
|
|
371
|
-
```
|
|
372
|
-
|
|
373
|
-
#### Lifecycle Events
|
|
374
|
-
|
|
375
|
-
```jsx
|
|
376
|
-
function ExampleView(props, ctx) {
|
|
377
|
-
ctx.beforeConnect(() => {
|
|
378
|
-
// Do something before this view's DOM nodes are created.
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
ctx.onConnected(() => {
|
|
382
|
-
// Do something immediately after this view is connected to the DOM.
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
ctx.beforeDisconnect(() => {
|
|
386
|
-
// Do something before removing this view from the DOM.
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
ctx.onDisconnected(() => {
|
|
390
|
-
// Do some cleanup after this view is disconnected from the DOM.
|
|
391
|
-
});
|
|
392
|
-
|
|
393
|
-
return <h1>Hello World!</h1>;
|
|
394
|
-
}
|
|
395
|
-
```
|
|
396
|
-
|
|
397
|
-
#### Displaying Children
|
|
398
|
-
|
|
399
|
-
The context object has an `outlet` function that can be used to display children at a location of your choosing.
|
|
400
|
-
|
|
401
|
-
```js
|
|
402
|
-
function LayoutView(props, ctx) {
|
|
403
|
-
return (
|
|
404
|
-
<div className="layout">
|
|
405
|
-
<OtherView />
|
|
406
|
-
<div className="content">{ctx.outlet()}</div>
|
|
407
|
-
</div>
|
|
408
|
-
);
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
function ExampleView() {
|
|
412
|
-
// <h1> and <p> are displayed inside LayoutView's outlet.
|
|
413
|
-
return (
|
|
414
|
-
<LayoutView>
|
|
415
|
-
<h1>Hello</h1>
|
|
416
|
-
<p>This is inside the box.</p>
|
|
417
|
-
</LayoutView>
|
|
418
|
-
);
|
|
419
|
-
}
|
|
420
|
-
```
|
|
421
|
-
|
|
422
|
-
#### Observing States
|
|
423
|
-
|
|
424
|
-
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.
|
|
425
|
-
|
|
426
|
-
```jsx
|
|
427
|
-
function ExampleView(props, ctx) {
|
|
428
|
-
const { $someValue } = ctx.getStore(SomeStore);
|
|
429
|
-
|
|
430
|
-
ctx.observe($someValue, (value) => {
|
|
431
|
-
ctx.log("someValue is now", value);
|
|
432
|
-
});
|
|
433
|
-
|
|
434
|
-
return <h1>Hello World!</h1>;
|
|
435
|
-
}
|
|
436
|
-
```
|
|
437
|
-
|
|
438
|
-
#### Routing
|
|
439
|
-
|
|
440
|
-
Dolla makes heavy use of client-side routing. You can define as many routes as you have views, and the URL
|
|
441
|
-
will determine which one the app shows at any given time. By building an app around routes, lots of things one expects
|
|
442
|
-
from a web app will just work; back and forward buttons, sharable URLs, bookmarks, etc.
|
|
443
|
-
|
|
444
|
-
Routes are matched by highest specificity regardless of the order they were registered.
|
|
445
|
-
This avoids some confusing situations that come up with order-based routers like that of `express`.
|
|
446
|
-
On the other hand, order-based routers can support regular expressions as patterns which Dolla's router cannot.
|
|
447
|
-
|
|
448
|
-
#### Route Patterns
|
|
449
|
-
|
|
450
|
-
Routes are defined with strings called patterns. A pattern defines the shape the URL path must match, with special
|
|
451
|
-
placeholders for variables that appear within the route. Values matched by those placeholders are parsed out and exposed
|
|
452
|
-
to your code (`router` store, `$params` readable). Below are some examples of patterns and how they work.
|
|
453
|
-
|
|
454
|
-
- Static: `/this/is/static` has no params and will match only when the route is exactly `/this/is/static`.
|
|
455
|
-
- Numeric params: `/users/{#id}/edit` has the named param `{#id}` which matches numbers only, such as `123` or `52`. The
|
|
456
|
-
resulting value will be parsed as a number.
|
|
457
|
-
- Generic params: `/users/{name}` has the named param `{name}` which matches anything in that position in the path. The
|
|
458
|
-
resulting value will be a string.
|
|
459
|
-
- Wildcard: `/users/*` will match anything beginning with `/users` and store everything after that in params
|
|
460
|
-
as `wildcard`. `*` is valid only at the end of a route.
|
|
461
|
-
|
|
462
|
-
Now, here are some route examples in the context of an app:
|
|
463
|
-
|
|
464
|
-
```js
|
|
465
|
-
import Dolla from "@manyducks.co/dolla";
|
|
466
|
-
import { PersonDetails, ThingIndex, ThingDetails, ThingEdit, ThingDelete } from "./views.js";
|
|
467
|
-
|
|
468
|
-
Dolla.router.setup({
|
|
469
|
-
routes: [
|
|
470
|
-
{ path: "/people/{name}", view: PersonDetails },
|
|
471
|
-
{
|
|
472
|
-
// A `null` component with subroutes acts as a namespace for those subroutes.
|
|
473
|
-
// Passing a view instead of `null` results in subroutes being rendered inside that view wherever `ctx.outlet()` is called.
|
|
474
|
-
path: "/things",
|
|
475
|
-
view: null,
|
|
476
|
-
routes: [
|
|
477
|
-
{ path: "/", view: ThingIndex }, // matches `/things`
|
|
478
|
-
{ path: "/{#id}", view: ThingDetails }, // matches `/things/{#id}`
|
|
479
|
-
{ path: "/{#id}/edit", view: ThingEdit }, // matches `/things/{#id}/edit`
|
|
480
|
-
{ path: "/{#id}/delete", view: ThingDelete }, // matches `/things/{#id}/delete`
|
|
481
|
-
],
|
|
482
|
-
},
|
|
483
|
-
],
|
|
484
|
-
});
|
|
485
|
-
```
|
|
486
|
-
|
|
487
|
-
As you may have inferred from the code above, when the URL matches a pattern the corresponding view is displayed. If we
|
|
488
|
-
visit `/people/john`, we will see the `PersonDetails` view and the params will be `{ name: "john" }`. Params can be
|
|
489
|
-
accessed anywhere through `Dolla.router`.
|
|
490
|
-
|
|
491
|
-
```js
|
|
492
|
-
function PersonDetails(props, ctx) {
|
|
493
|
-
// Info about the current route is exported as a set of Readables. Query params are also Writable through $$query:
|
|
494
|
-
const { $path, $pattern, $params, $query } = Dolla.router;
|
|
495
|
-
|
|
496
|
-
Dolla.router.back(); // Step back in the history to the previous route, if any.
|
|
497
|
-
Dolla.router.back(2); // Hit the back button twice.
|
|
498
|
-
|
|
499
|
-
Dolla.router.forward(); // Step forward in the history to the next route, if any.
|
|
500
|
-
Dolla.router.forward(4); // Hit the forward button 4 times.
|
|
501
|
-
|
|
502
|
-
Dolla.router.go("/things/152"); // Navigate to another path within the same app.
|
|
503
|
-
Dolla.router.go("https://www.example.com/another/site"); // Navigate to another domain entirely.
|
|
504
|
-
|
|
505
|
-
// Three ways to confirm with the user that they wish to navigate before actually doing it.
|
|
506
|
-
Dolla.router.go("/another/page", { prompt: true });
|
|
507
|
-
Dolla.router.go("/another/page", { prompt: "Are you sure you want to leave and go to /another/page?" });
|
|
508
|
-
Dolla.router.go("/another/page", { prompt: PromptView });
|
|
509
|
-
|
|
510
|
-
// Get the live value of `{name}` from the current path.
|
|
511
|
-
const $name = Dolla.derive([$params], (p) => p.name);
|
|
512
|
-
|
|
513
|
-
// Render it into a <p> tag. The name portion will update if the URL changes.
|
|
514
|
-
return <p>The person is: {$name}</p>;
|
|
515
|
-
}
|
|
516
|
-
```
|
|
517
|
-
|
|
518
|
-
## HTTP Client
|
|
519
|
-
|
|
520
|
-
```js
|
|
521
|
-
// Middleware!
|
|
522
|
-
Dolla.http.use((request, next) => {
|
|
523
|
-
// Add auth header for all requests going to the API.
|
|
524
|
-
if (request.url.pathname.startsWith("/api")) {
|
|
525
|
-
request.headers.set("authorization", `Bearer ${authToken}`);
|
|
526
|
-
}
|
|
527
|
-
|
|
528
|
-
const response = await next();
|
|
529
|
-
|
|
530
|
-
// Could do something with the response here.
|
|
531
|
-
|
|
532
|
-
return response;
|
|
533
65
|
});
|
|
534
66
|
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
// Body is already parsed from JSON into an object.
|
|
538
|
-
exampleResponse.body.someValue;
|
|
67
|
+
Dolla.mount(document.body, Counter);
|
|
539
68
|
```
|
|
540
69
|
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
```js
|
|
544
|
-
import Dolla, { html, t } from "@manyducks.co/dolla";
|
|
545
|
-
|
|
546
|
-
function Counter(props, ctx) {
|
|
547
|
-
const [$count, setCount] = Dolla.createState(0);
|
|
70
|
+
> TODO: Show small examples for routing and stores.
|
|
548
71
|
|
|
549
|
-
|
|
550
|
-
setCount((count) => count + 1);
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
return html`
|
|
554
|
-
<div>
|
|
555
|
-
<p>Clicks: ${$count}</p>
|
|
556
|
-
<button onclick=${increment}>${t("buttonLabel")}</button>
|
|
557
|
-
</div>
|
|
558
|
-
`;
|
|
559
|
-
}
|
|
560
|
-
|
|
561
|
-
Dolla.i18n.setup({
|
|
562
|
-
locale: "en",
|
|
563
|
-
translations: [
|
|
564
|
-
{ locale: "en", strings: { buttonLabel: "Click here to increment" } },
|
|
565
|
-
{ locale: "ja", strings: { buttonLabel: "ここに押して増加する" } },
|
|
566
|
-
],
|
|
567
|
-
});
|
|
568
|
-
|
|
569
|
-
Dolla.mount(document.body, Counter);
|
|
570
|
-
```
|
|
72
|
+
For more detail [check out the Docs](./docs/index.md).
|
|
571
73
|
|
|
572
74
|
---
|
|
573
75
|
|
|
574
|
-
[🦆](https://www.manyducks.co)
|
|
76
|
+
[🦆 That's a lot of ducks.](https://www.manyducks.co)
|
|
@@ -24,7 +24,7 @@ export declare class Repeat<T> implements MarkupElement {
|
|
|
24
24
|
stopCallback?: StopFunction;
|
|
25
25
|
connectedItems: ConnectedItem<T>[];
|
|
26
26
|
elementContext: ElementContext;
|
|
27
|
-
renderFn: ($value: State<T>, $index: State<number>,
|
|
27
|
+
renderFn: (this: ViewContext, $value: State<T>, $index: State<number>, context: ViewContext) => ViewResult;
|
|
28
28
|
keyFn: (value: T, index: number) => string | number | symbol;
|
|
29
29
|
get isMounted(): boolean;
|
|
30
30
|
constructor({ elementContext, $items, renderFn, keyFn }: RepeatOptions<T>);
|
|
@@ -30,6 +30,10 @@ export interface ViewContext extends Logger, StorableContext {
|
|
|
30
30
|
* Sets the name of the view's built in logger.
|
|
31
31
|
*/
|
|
32
32
|
setName(name: string): ViewContext;
|
|
33
|
+
/**
|
|
34
|
+
* True while this view is connected to the DOM.
|
|
35
|
+
*/
|
|
36
|
+
readonly isMounted: boolean;
|
|
33
37
|
/**
|
|
34
38
|
* Registers a callback to run just before this view is mounted. DOM nodes are not yet attached to the page.
|
|
35
39
|
*/
|
package/dist/core/store.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { Emitter } from "@manyducks.co/emitter";
|
|
2
2
|
import { type ComponentContext, type ElementContext } from "./context.js";
|
|
3
3
|
import type { Logger } from "./dolla.js";
|
|
4
|
+
import { type MaybeState, type StateValues, type StopFunction } from "./state.js";
|
|
4
5
|
export type StoreFunction<Options, Value> = (this: StoreContext, options: Options, context: StoreContext) => Value;
|
|
5
6
|
export type StoreFactory<Options, Value> = Options extends undefined ? () => Store<Options, Value> : (options: Options) => Store<Options, Value>;
|
|
6
7
|
export interface StoreContext extends Logger, ComponentContext {
|
|
8
|
+
/**
|
|
9
|
+
* True while this store is attached to a context that is currently mounted in the view tree.
|
|
10
|
+
*/
|
|
11
|
+
readonly isMounted: boolean;
|
|
7
12
|
/**
|
|
8
13
|
* Registers a callback to run just after this store is mounted.
|
|
9
14
|
*/
|
|
@@ -12,6 +17,11 @@ export interface StoreContext extends Logger, ComponentContext {
|
|
|
12
17
|
* Registers a callback to run just after this store is unmounted.
|
|
13
18
|
*/
|
|
14
19
|
onUnmount(callback: () => void): void;
|
|
20
|
+
/**
|
|
21
|
+
* Watch a set of states. The callback is called when any of the states receive a new value.
|
|
22
|
+
* Watchers will be automatically stopped when this store is unmounted.
|
|
23
|
+
*/
|
|
24
|
+
watch<T extends MaybeState<any>[]>(states: [...T], callback: (...values: StateValues<T>) => void): StopFunction;
|
|
15
25
|
}
|
|
16
26
|
type StoreEvents = {
|
|
17
27
|
mounted: [];
|
|
@@ -25,11 +35,18 @@ export declare class Store<Options, Value> {
|
|
|
25
35
|
* Value is guaranteed to be set after `attach` is called.
|
|
26
36
|
*/
|
|
27
37
|
value: Value;
|
|
38
|
+
isMounted: boolean;
|
|
28
39
|
_elementContext: ElementContext;
|
|
29
40
|
_emitter: Emitter<StoreEvents>;
|
|
30
41
|
_logger: Logger;
|
|
42
|
+
_watcher: import("./state.js").StateWatcher;
|
|
43
|
+
get name(): string;
|
|
31
44
|
constructor(key: string, fn: StoreFunction<Options, Value>, options: Options);
|
|
32
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Attaches this Store to the elementContext.
|
|
47
|
+
* Returns false if there was already an instance attached, and true otherwise.
|
|
48
|
+
*/
|
|
49
|
+
attach(elementContext: ElementContext): boolean;
|
|
33
50
|
handleMount(): void;
|
|
34
51
|
handleUnmount(): void;
|
|
35
52
|
}
|