@manyducks.co/dolla 2.0.0-alpha.3 → 2.0.0-alpha.31
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 +31 -951
- package/dist/core/batch.d.ts +17 -0
- package/dist/core/context.d.ts +94 -0
- package/dist/core/dolla.d.ts +161 -0
- package/dist/core/markup.d.ts +91 -0
- package/dist/core/nodes/dom.d.ts +13 -0
- package/dist/core/nodes/html.d.ts +39 -0
- package/dist/core/nodes/observer.d.ts +30 -0
- package/dist/core/nodes/outlet.d.ts +19 -0
- package/dist/core/nodes/portal.d.ts +22 -0
- package/dist/core/nodes/repeat.d.ts +36 -0
- package/dist/core/nodes/view.d.ts +92 -0
- package/dist/core/ref.d.ts +29 -0
- package/dist/core/state.d.ts +126 -0
- package/dist/core/stats.d.ts +31 -0
- package/dist/core/store.d.ts +62 -0
- package/dist/core/symbols.d.ts +7 -0
- package/dist/index.d.ts +18 -11
- package/dist/index.js +1157 -1159
- package/dist/index.js.map +1 -1
- package/dist/jsx-dev-runtime.d.ts +2 -2
- package/dist/jsx-dev-runtime.js +2 -2
- package/dist/jsx-dev-runtime.js.map +1 -1
- package/dist/jsx-runtime.d.ts +3 -3
- package/dist/jsx-runtime.js +2 -2
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/markup-D1i09ddt.js +1563 -0
- package/dist/markup-D1i09ddt.js.map +1 -0
- package/dist/modules/http.d.ts +5 -5
- package/dist/modules/i18n.d.ts +129 -0
- package/dist/modules/router.d.ts +37 -48
- package/dist/typeChecking.d.ts +2 -2
- package/dist/types.d.ts +12 -13
- package/dist/utils.d.ts +14 -2
- package/dist/views/default-crash-view.d.ts +1 -1
- package/dist/views/passthrough.d.ts +2 -2
- package/docs/http.md +29 -0
- package/docs/i18n.md +38 -0
- package/docs/index.md +10 -0
- package/docs/router.md +77 -0
- package/docs/setup.md +31 -0
- package/docs/state.md +141 -0
- package/docs/stores.md +62 -0
- package/docs/views.md +308 -0
- package/index.d.ts +2 -2
- package/notes/TODO.md +6 -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 +195 -7
- package/notes/stores.md +73 -0
- package/package.json +12 -14
- package/tests/{signals.test.js → state.test.js} +6 -6
- package/vite.config.js +0 -10
- package/dist/markup.d.ts +0 -100
- package/dist/modules/dolla.d.ts +0 -111
- package/dist/modules/language.d.ts +0 -41
- package/dist/modules/render.d.ts +0 -17
- package/dist/nodes/cond.d.ts +0 -26
- package/dist/nodes/html.d.ts +0 -26
- package/dist/nodes/observer.d.ts +0 -29
- package/dist/nodes/outlet.d.ts +0 -22
- package/dist/nodes/portal.d.ts +0 -19
- package/dist/nodes/repeat.d.ts +0 -34
- package/dist/nodes/text.d.ts +0 -19
- package/dist/passthrough-DrtCifRF.js +0 -1228
- package/dist/passthrough-DrtCifRF.js.map +0 -1
- package/dist/signals.d.ts +0 -101
- package/dist/view.d.ts +0 -50
- /package/dist/{routing.d.ts → modules/router.utils.d.ts} +0 -0
- /package/dist/{routing.test.d.ts → modules/router.utils.test.d.ts} +0 -0
package/dist/modules/router.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import type { Dolla } from "../core/dolla.js";
|
|
2
|
+
import { type ViewFunction } from "../core/nodes/view.js";
|
|
3
|
+
import { IS_ROUTER } from "../core/symbols.js";
|
|
4
|
+
import type { Stringable } from "../types.js";
|
|
4
5
|
export interface RouteMatchContext {
|
|
5
6
|
/**
|
|
6
7
|
* Redirects the user to a different route instead of matching the current one.
|
|
@@ -29,14 +30,15 @@ export interface Route {
|
|
|
29
30
|
*/
|
|
30
31
|
beforeMatch?: (ctx: RouteMatchContext) => void | Promise<void>;
|
|
31
32
|
}
|
|
33
|
+
export interface RouteMeta {
|
|
34
|
+
redirect?: string | ((ctx: RouteRedirectContext) => string) | ((ctx: RouteRedirectContext) => Promise<string>);
|
|
35
|
+
pattern?: string;
|
|
36
|
+
layers?: RouteLayer[];
|
|
37
|
+
beforeMatch?: (ctx: RouteMatchContext) => void | Promise<void>;
|
|
38
|
+
}
|
|
32
39
|
export interface RouteConfig {
|
|
33
40
|
pattern: string;
|
|
34
|
-
meta:
|
|
35
|
-
redirect?: string | ((ctx: RouteRedirectContext) => string) | ((ctx: RouteRedirectContext) => Promise<string>);
|
|
36
|
-
pattern?: string;
|
|
37
|
-
layers?: RouteLayer[];
|
|
38
|
-
beforeMatch?: (ctx: RouteMatchContext) => void | Promise<void>;
|
|
39
|
-
};
|
|
41
|
+
meta: RouteMeta;
|
|
40
42
|
}
|
|
41
43
|
export interface RouteLayer {
|
|
42
44
|
id: number;
|
|
@@ -63,11 +65,6 @@ export interface RouteRedirectContext {
|
|
|
63
65
|
*/
|
|
64
66
|
query: Record<string, string | number | boolean | undefined>;
|
|
65
67
|
}
|
|
66
|
-
interface ParsedParams {
|
|
67
|
-
[key: string]: string | number | boolean | (string | number | boolean | null)[] | null;
|
|
68
|
-
}
|
|
69
|
-
interface ParsedQuery extends ParsedParams {
|
|
70
|
-
}
|
|
71
68
|
export interface NavigateOptions {
|
|
72
69
|
/**
|
|
73
70
|
* Replace the current item in the history stack instead of adding a new one.
|
|
@@ -79,57 +76,41 @@ export interface NavigateOptions {
|
|
|
79
76
|
*/
|
|
80
77
|
preserveQuery?: boolean;
|
|
81
78
|
}
|
|
82
|
-
export
|
|
83
|
-
/**
|
|
84
|
-
* Constructs routes like "https://www.example.com/#/sub/route" which work without any server-side setup.
|
|
85
|
-
* A good choice if your app has no backend.
|
|
86
|
-
*/
|
|
87
|
-
hash = "hash",
|
|
88
|
-
/**
|
|
89
|
-
* Constructs routes like "https://www.example.com/sub/route" which look nicer (subjective?) than hash routes and are what most users will expect URLs to look like, but require additional backend setup.
|
|
90
|
-
* Path routing requires you to configure your backend to redirect to the app's index.html when subpaths are requested.
|
|
91
|
-
*/
|
|
92
|
-
path = "path"
|
|
93
|
-
}
|
|
94
|
-
export interface RouterSetupOptions {
|
|
79
|
+
export interface RouterOptions {
|
|
95
80
|
routes: Route[];
|
|
96
81
|
/**
|
|
97
|
-
*
|
|
82
|
+
* When true, the router will construct routes like "https://www.example.com/#/sub/route" which work without any backend intervention.
|
|
98
83
|
*/
|
|
99
|
-
|
|
100
|
-
}
|
|
101
|
-
export interface RouterElements {
|
|
102
|
-
readonly rootElement?: HTMLElement;
|
|
103
|
-
readonly rootView?: ViewNode;
|
|
84
|
+
hash?: boolean;
|
|
104
85
|
}
|
|
86
|
+
export declare function createRouter(options: RouterOptions): Router;
|
|
87
|
+
declare const ROUTER_MOUNT: unique symbol;
|
|
88
|
+
declare const ROUTER_UNMOUNT: unique symbol;
|
|
89
|
+
export declare function _isRouter(value: any): value is Router;
|
|
90
|
+
export declare function _mountRouter(router: Router, dolla: Dolla): Promise<void>;
|
|
91
|
+
export declare function _unmountRouter(router: Router): Promise<void>;
|
|
105
92
|
export declare class Router {
|
|
106
93
|
#private;
|
|
94
|
+
[IS_ROUTER]: boolean;
|
|
107
95
|
/**
|
|
108
96
|
* The currently matched route pattern, if any.
|
|
109
97
|
*/
|
|
110
|
-
$pattern: import("../
|
|
98
|
+
$pattern: import("../core/state.js").State<string | undefined>;
|
|
111
99
|
/**
|
|
112
100
|
* The current URL path.
|
|
113
101
|
*/
|
|
114
|
-
$path: import("../
|
|
102
|
+
$path: import("../core/state.js").State<string>;
|
|
115
103
|
/**
|
|
116
104
|
* The current named path params.
|
|
117
105
|
*/
|
|
118
|
-
$params: import("../
|
|
106
|
+
$params: import("../core/state.js").State<Record<string, string | number>>;
|
|
119
107
|
/**
|
|
120
108
|
* The current query params. Changes to this object will be reflected in the URL.
|
|
121
109
|
*/
|
|
122
|
-
$query: import("../
|
|
123
|
-
constructor(
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
* Navigates to another route.
|
|
127
|
-
*
|
|
128
|
-
* @example
|
|
129
|
-
* navigate("/login"); // navigate to `/login`
|
|
130
|
-
* navigate(["/users", 215], { replace: true }); // replace current history entry with `/users/215`
|
|
131
|
-
*/
|
|
132
|
-
go(path: Stringable | Stringable[], options?: NavigateOptions): void;
|
|
110
|
+
$query: import("../core/state.js").State<Record<string, string | number | boolean>>;
|
|
111
|
+
constructor(options: RouterOptions);
|
|
112
|
+
[ROUTER_MOUNT](dolla: Dolla): Promise<void>;
|
|
113
|
+
[ROUTER_UNMOUNT](): Promise<void>;
|
|
133
114
|
/**
|
|
134
115
|
* Navigate backward. Pass a number of steps to hit the back button that many times.
|
|
135
116
|
*/
|
|
@@ -138,6 +119,14 @@ export declare class Router {
|
|
|
138
119
|
* Navigate forward. Pass a number of steps to hit the forward button that many times.
|
|
139
120
|
*/
|
|
140
121
|
forward(steps?: number): void;
|
|
122
|
+
/**
|
|
123
|
+
* Navigates to another route.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* Dolla.router.go("/login"); // navigate to `/login`
|
|
127
|
+
* Dolla.router.go["/users", 215], { replace: true }); // replace current history entry with `/users/215`
|
|
128
|
+
*/
|
|
129
|
+
go(path: Stringable | Stringable[], options?: NavigateOptions): void;
|
|
141
130
|
}
|
|
142
131
|
/**
|
|
143
132
|
* Intercepts links within the root node.
|
|
@@ -148,5 +137,5 @@ export declare class Router {
|
|
|
148
137
|
* @param callback - Function to call when a click event is intercepted
|
|
149
138
|
* @param _window - (optional) Override for global window object
|
|
150
139
|
*/
|
|
151
|
-
export declare function catchLinks(root:
|
|
140
|
+
export declare function catchLinks(root: Element, callback: (anchor: HTMLAnchorElement) => void, _window?: Window & typeof globalThis): () => void;
|
|
152
141
|
export {};
|
package/dist/typeChecking.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
type TypeNames = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "null" | "array" | "class" | "promise" | "NaN";
|
|
1
|
+
type TypeNames = "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "null" | "array" | "class" | "promise" | "map" | "set" | "NaN";
|
|
2
2
|
/**
|
|
3
3
|
* Extends `typeof` operator with more specific and useful type distinctions.
|
|
4
4
|
*/
|
|
5
|
-
export declare function typeOf(value:
|
|
5
|
+
export declare function typeOf(value: any): TypeNames;
|
|
6
6
|
/**
|
|
7
7
|
* Throws a TypeError unless `condition` is truthy.
|
|
8
8
|
*
|
package/dist/types.d.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import type * as CSS from "csstype";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import type { Markup } from "./core/markup.js";
|
|
3
|
+
import type { State } from "./core/state.js";
|
|
4
4
|
/**
|
|
5
5
|
* Represents everything that can be handled as a DOM node.
|
|
6
6
|
* These are all the items considered valid to pass as children to any element.
|
|
7
7
|
*/
|
|
8
|
-
export type Renderable = string | number | Markup | false | null | undefined |
|
|
8
|
+
export type Renderable = string | number | Markup | false | null | undefined | State<any> | (string | number | Markup | false | null | undefined | State<any>)[];
|
|
9
9
|
export type Stringable = {
|
|
10
10
|
toString(): string;
|
|
11
11
|
};
|
|
12
|
-
type
|
|
13
|
-
type OptionalProperty<T> = T |
|
|
14
|
-
type RequiredProperty<T> = T |
|
|
12
|
+
type MaybeState<T> = T | State<T> | State<T | undefined>;
|
|
13
|
+
type OptionalProperty<T> = T | State<T> | State<T | undefined>;
|
|
14
|
+
type RequiredProperty<T> = T | State<T>;
|
|
15
15
|
type AutocapitalizeValues = "off" | "on" | "none" | "sentences" | "words" | "characters";
|
|
16
16
|
type ContentEditableValues = true | false | "true" | "false" | "plaintext-only" | "inherit";
|
|
17
17
|
type ClassListValues = string | ClassMap | Array<string | ClassMap | (string | ClassMap)[]>;
|
|
@@ -110,7 +110,7 @@ export interface ElementProps {
|
|
|
110
110
|
*
|
|
111
111
|
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/style
|
|
112
112
|
*/
|
|
113
|
-
style?: string | CSSProperties |
|
|
113
|
+
style?: string | CSSProperties | State<string> | State<CSSProperties> | State<string | CSSProperties> | State<string | undefined> | State<CSSProperties | undefined> | State<string | CSSProperties | undefined>;
|
|
114
114
|
/**
|
|
115
115
|
* Fired when a CSS animation unexpectedly aborts.
|
|
116
116
|
*
|
|
@@ -1210,7 +1210,7 @@ export type CSSProperties = {
|
|
|
1210
1210
|
[K in keyof Styles]: OptionalProperty<Styles[K]>;
|
|
1211
1211
|
};
|
|
1212
1212
|
export interface ClassMap {
|
|
1213
|
-
[className: string]:
|
|
1213
|
+
[className: string]: MaybeState<any>;
|
|
1214
1214
|
}
|
|
1215
1215
|
export type EventHandler<E> = (event: E) => void;
|
|
1216
1216
|
export interface PropertiesOf<E extends HTMLElement> extends HTMLElementProps {
|
|
@@ -1219,9 +1219,9 @@ export interface PropertiesOf<E extends HTMLElement> extends HTMLElementProps {
|
|
|
1219
1219
|
*/
|
|
1220
1220
|
children?: any;
|
|
1221
1221
|
/**
|
|
1222
|
-
*
|
|
1222
|
+
* Receives a reference to the DOM node when rendered.
|
|
1223
1223
|
*/
|
|
1224
|
-
ref?:
|
|
1224
|
+
ref?: ((value: E | undefined) => void) | ((value: HTMLElement | undefined) => void) | ((value: Element | undefined) => void) | ((value: Node | undefined) => void);
|
|
1225
1225
|
}
|
|
1226
1226
|
/**
|
|
1227
1227
|
* The following elements are defined based on the WHATWG HTML spec:
|
|
@@ -2098,7 +2098,7 @@ interface HTMLMediaElementProps<T extends HTMLMediaElement> extends HTMLElementP
|
|
|
2098
2098
|
*
|
|
2099
2099
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject
|
|
2100
2100
|
*/
|
|
2101
|
-
srcObject?: MediaStream | MediaSource | Blob | File |
|
|
2101
|
+
srcObject?: MediaStream | MediaSource | Blob | File | State<MediaStream> | State<MediaStream | undefined> | State<MediaSource> | State<MediaSource | undefined> | State<Blob> | State<Blob | undefined> | State<File> | State<File | undefined>;
|
|
2102
2102
|
/**
|
|
2103
2103
|
* The current audio volume of the media element. Must be a number between 0 and 1.
|
|
2104
2104
|
*
|
|
@@ -2597,7 +2597,7 @@ interface HTMLImageElementProps extends PropertiesOf<HTMLImageElement> {
|
|
|
2597
2597
|
*
|
|
2598
2598
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/sizes
|
|
2599
2599
|
*/
|
|
2600
|
-
sizes?:
|
|
2600
|
+
sizes?: MaybeState<string>;
|
|
2601
2601
|
/**
|
|
2602
2602
|
* The image URL.
|
|
2603
2603
|
*
|
|
@@ -3143,7 +3143,6 @@ interface HTMLInputElementProps extends PropertiesOf<HTMLInputElement> {
|
|
|
3143
3143
|
step?: OptionalProperty<number>;
|
|
3144
3144
|
type?: OptionalProperty<InputType>;
|
|
3145
3145
|
value?: OptionalProperty<string>;
|
|
3146
|
-
$$value?: SettableSignal<any>;
|
|
3147
3146
|
width?: OptionalProperty<string | number> | OptionalProperty<string> | OptionalProperty<number>;
|
|
3148
3147
|
title?: OptionalProperty<string>;
|
|
3149
3148
|
/**
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
export declare const noOp: () => void;
|
|
2
|
-
export declare function
|
|
2
|
+
export declare function getUniqueId(): string;
|
|
3
|
+
/**
|
|
4
|
+
* Equality check that passes if both values are the same object.
|
|
5
|
+
* This is the default equality check for states.
|
|
6
|
+
*/
|
|
7
|
+
export declare function strictEqual(a: any, b: any): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Equality check that passes if both values are the same object, or if both are objects or arrays with equal keys and values.
|
|
10
|
+
*/
|
|
11
|
+
export declare function shallowEqual(a: any, b: any): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Equality check that passes if two objects have equal values, even if they are not the same object.
|
|
14
|
+
*/
|
|
15
|
+
export declare const deepEqual: (a: any, b: any) => boolean;
|
|
3
16
|
/**
|
|
4
17
|
* Takes an old value and a new value. Returns a merged copy if both are objects, otherwise returns the new value.
|
|
5
18
|
*/
|
|
@@ -13,7 +26,6 @@ export declare function merge(one: unknown, two: unknown): any;
|
|
|
13
26
|
* @param object - An object to clone without the omitted keys.
|
|
14
27
|
*/
|
|
15
28
|
export declare function omit<O extends Record<any, any>>(keys: (keyof O)[], object: O): Record<any, any>;
|
|
16
|
-
export declare function getDefaultConsole(): any;
|
|
17
29
|
export declare function colorFromString(value: string): string;
|
|
18
30
|
export type MatcherFunction = (value: string) => boolean;
|
|
19
31
|
/**
|
|
@@ -15,4 +15,4 @@ export type CrashViewProps = {
|
|
|
15
15
|
*/
|
|
16
16
|
uid?: string;
|
|
17
17
|
};
|
|
18
|
-
export declare function DefaultCrashView(props: CrashViewProps): import("../markup.js").Markup | import("../markup.js").Markup[];
|
|
18
|
+
export declare function DefaultCrashView(props: CrashViewProps): import("../core/markup.js").Markup | import("../core/markup.js").Markup[];
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type ViewContext } from "../view.js";
|
|
1
|
+
import { type ViewContext } from "../core/nodes/view.js";
|
|
2
2
|
/**
|
|
3
3
|
* A utility view that simply displays its children.
|
|
4
4
|
*/
|
|
5
|
-
export declare function Passthrough(_: {}, ctx: ViewContext): import("../
|
|
5
|
+
export declare function Passthrough(_: {}, ctx: ViewContext): import("../index.js").Markup;
|
package/docs/http.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# HTTP Client
|
|
2
|
+
|
|
3
|
+
> TODO: Write me.
|
|
4
|
+
|
|
5
|
+
This page goes into detail on how to use the built in HTTP client.
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import Dolla from "@manyducks.co/dolla";
|
|
9
|
+
|
|
10
|
+
Dolla.http.use(async (req, next) => {
|
|
11
|
+
// Apply auth header to all API routes.
|
|
12
|
+
if (req.url.pathname.startsWith("/api/")) {
|
|
13
|
+
req.headers.set("authorization", `Bearer ${localStorage.getItem("api-key")}`);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
await next();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
const res = await Dolla.http.get("/api/some-api-route");
|
|
20
|
+
res.body; // body is already parsed as JSON if server responded with JSON
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
End.
|
|
26
|
+
|
|
27
|
+
- [🗂️ Docs](./index.md)
|
|
28
|
+
- [🏠 README](../README.md)
|
|
29
|
+
- [🦆 That's a lot of ducks.](https://www.manyducks.co)
|
package/docs/i18n.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Internationalization (i18n) Support
|
|
2
|
+
|
|
3
|
+
```jsx
|
|
4
|
+
import Dolla, { createView, createState, t } from "@manyducks.co/dolla";
|
|
5
|
+
|
|
6
|
+
const CounterView = createView(function (props) {
|
|
7
|
+
const [$count, setCount] = createState(0);
|
|
8
|
+
|
|
9
|
+
const increment = () => {
|
|
10
|
+
setCount((count) => count + 1);
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<div>
|
|
15
|
+
<p>Clicks: {$count}</p>
|
|
16
|
+
<button onClick={increment}>{t("buttonLabel")}</button>
|
|
17
|
+
</div>
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
Dolla.i18n.setup({
|
|
22
|
+
locale: "en",
|
|
23
|
+
translations: [
|
|
24
|
+
{ locale: "en", strings: { buttonLabel: "Click here to increment" } },
|
|
25
|
+
{ locale: "ja", strings: { buttonLabel: "ここに押して増加する" } },
|
|
26
|
+
],
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
Dolla.mount(document.body, CounterView);
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
End.
|
|
35
|
+
|
|
36
|
+
- [🗂️ Docs](./index.md)
|
|
37
|
+
- [🏠 README](../README.md)
|
|
38
|
+
- [🦆 That's a lot of ducks.](https://www.manyducks.co)
|
package/docs/index.md
ADDED
package/docs/router.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Router
|
|
2
|
+
|
|
3
|
+
Dolla makes heavy use of client-side routing. You can define as many routes as you have views, and the URL
|
|
4
|
+
will determine which one the app shows at any given time. By building an app around routes, lots of things one expects
|
|
5
|
+
from a web app will just work; back and forward buttons, sharable URLs, bookmarks, etc.
|
|
6
|
+
|
|
7
|
+
Routes are matched by highest specificity regardless of the order they were registered.
|
|
8
|
+
This avoids some confusing situations that come up with order-based routers like that of `express`.
|
|
9
|
+
On the other hand, order-based routers can support regular expressions as patterns which Dolla's router cannot.
|
|
10
|
+
|
|
11
|
+
## Route Patterns
|
|
12
|
+
|
|
13
|
+
Routes are defined with strings called patterns. A pattern defines the shape the URL path must match, with special
|
|
14
|
+
placeholders for variables that appear within the route. Values matched by those placeholders are parsed out and exposed
|
|
15
|
+
to your code (`router` store, `$params` readable). Below are some examples of patterns and how they work.
|
|
16
|
+
|
|
17
|
+
- Static: `/this/is/static` has no params and will match only when the route is exactly `/this/is/static`.
|
|
18
|
+
- Numeric params: `/users/{#id}/edit` has the named param `{#id}` which matches numbers only, such as `123` or `52`. The
|
|
19
|
+
resulting value will be parsed as a number.
|
|
20
|
+
- Generic params: `/users/{name}` has the named param `{name}` which matches anything in that position in the path. The
|
|
21
|
+
resulting value will be a string.
|
|
22
|
+
- Wildcard: `/users/*` will match anything beginning with `/users` and store everything after that in params
|
|
23
|
+
as `wildcard`. `*` is valid only at the end of a route.
|
|
24
|
+
|
|
25
|
+
Now, here are some route examples in the context of an app:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import Dolla from "@manyducks.co/dolla";
|
|
29
|
+
import { ThingIndex, ThingDetails, ThingEdit, ThingDelete } from "./views.js";
|
|
30
|
+
|
|
31
|
+
Dolla.router.setup({
|
|
32
|
+
routes: [
|
|
33
|
+
{
|
|
34
|
+
// A `null` component with subroutes acts as a namespace for those subroutes.
|
|
35
|
+
// Passing a view instead of `null` results in subroutes being rendered inside that view wherever `ctx.outlet()` is called.
|
|
36
|
+
path: "/things",
|
|
37
|
+
view: null,
|
|
38
|
+
routes: [
|
|
39
|
+
{ path: "/", view: ThingIndex }, // matches `/things`
|
|
40
|
+
{ path: "/{#id}", view: ThingDetails }, // matches `/things/{#id}`
|
|
41
|
+
{ path: "/{#id}/edit", view: ThingEdit }, // matches `/things/{#id}/edit`
|
|
42
|
+
{ path: "/{#id}/delete", view: ThingDelete }, // matches `/things/{#id}/delete`
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
// All routes that don't match anything else will redirect to `/things`
|
|
46
|
+
{ path: "*", redirect: "/things" },
|
|
47
|
+
],
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
When the URL matches a pattern the corresponding view is displayed. If we visit `/people/john`,
|
|
52
|
+
we will see the `PersonDetails` view and the params will be `{ name: "john" }`. Params can be
|
|
53
|
+
accessed from anywhere in the app through `Dolla.router`.
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
import Dolla from "@manyducks.co/dolla";
|
|
57
|
+
|
|
58
|
+
// Info about the current route is exported as a set of Readables. Query params are also Writable through $$query:
|
|
59
|
+
const { $path, $pattern, $params, $query } = Dolla.router;
|
|
60
|
+
|
|
61
|
+
Dolla.router.back(); // Step back in the history to the previous route, if any.
|
|
62
|
+
Dolla.router.back(2); // Hit the back button twice.
|
|
63
|
+
|
|
64
|
+
Dolla.router.forward(); // Step forward in the history to the next route, if any.
|
|
65
|
+
Dolla.router.forward(4); // Hit the forward button 4 times.
|
|
66
|
+
|
|
67
|
+
Dolla.router.go("/things/152"); // Navigate to another path within the same app.
|
|
68
|
+
Dolla.router.go("https://www.example.com/another/site"); // Navigate to another domain entirely.
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
End.
|
|
74
|
+
|
|
75
|
+
- [🗂️ Docs](./index.md)
|
|
76
|
+
- [🏠 README](../README.md)
|
|
77
|
+
- [🦆 That's a lot of ducks.](https://www.manyducks.co)
|
package/docs/setup.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# Setting up Dolla
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
Dolla is published on npm as `@manyducks.co/dolla`. You can install it in your project with the following command:
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm i @manyducks.co/dolla
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## JSX
|
|
12
|
+
|
|
13
|
+
If you want to use JSX in your app you can add the following options to your `tsconfig.json` or `jsconfig.json`. Modern build systems like [Vite](https://vite.dev) will pick these up automatically.
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"compilerOptions": {
|
|
18
|
+
// ... other options ...
|
|
19
|
+
"jsx": "react-jsx",
|
|
20
|
+
"jsxImportSource": "@manyducks.co/dolla"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
End.
|
|
28
|
+
|
|
29
|
+
- [🗂️ Docs](./index.md)
|
|
30
|
+
- [🏠 README](../README.md)
|
|
31
|
+
- [🦆 That's a lot of ducks.](https://www.manyducks.co)
|
package/docs/state.md
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
## ⚡ Reactive Updates with `State`
|
|
2
|
+
|
|
3
|
+
Dolla sets out to solve the challenge of keeping your UI in sync with your data. All apps have state that changes at runtime, and as those values change your UI must update itself to stay in sync with that state. JavaScript frameworks all have their own ways of meeting this challenge, but there are two main ones; virtual DOM and signals.
|
|
4
|
+
|
|
5
|
+
[React](https://react.dev) and similar frameworks make use of a [virtual DOM](https://svelte.dev/blog/virtual-dom-is-pure-overhead), in which every state change causes a "diff" of the real DOM nodes on the page against a lightweight representation of what those nodes _should_ look like, followed by a "patch" where the minimal updates are performed to bring the DOM in line with the ideal virtual DOM.
|
|
6
|
+
|
|
7
|
+
[Solid](https://www.solidjs.com) and similar frameworks make use of [signals](https://dev.to/this-is-learning/the-evolution-of-signals-in-javascript-8ob), which are containers for data that will change over time. Signal values are accessed through special getter functions that can be called inside of a "scope" to track their values. When the value of a tracked signal changes, any computations that happened in scopes that depend on those signals are re-run. In an app like this, all of your DOM updates are performed with pinpoint accuracy without diffing as signal values change.
|
|
8
|
+
|
|
9
|
+
Dolla uses a concept of a `State`, which is a signal-like container for values that change over time. Where `State` differs from signals, however, is that there is no magical scope tracking going on behind the scenes. All States that depend on others do so explicity, so your code is easier to read and understand.
|
|
10
|
+
|
|
11
|
+
The `State` API has just four functions:
|
|
12
|
+
|
|
13
|
+
- `createState` to create a new state and a linked setter function.
|
|
14
|
+
- `derive` to create a new state whose value depends on one or more other states.
|
|
15
|
+
- `toState` to ensure that a value is a state object.
|
|
16
|
+
- `toValue` to ensure that a value is a plain value.
|
|
17
|
+
|
|
18
|
+
### Basic State API
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
import { createState } from "@manyducks.co/dolla";
|
|
22
|
+
|
|
23
|
+
// Equivalent to React's `useState` or Solid's `createSignal`.
|
|
24
|
+
// A new read-only State and linked Setter are created.
|
|
25
|
+
const [$count, setCount] = createState(72);
|
|
26
|
+
|
|
27
|
+
// Get the current value.
|
|
28
|
+
$count.get(): // 72
|
|
29
|
+
|
|
30
|
+
// Set a new value.
|
|
31
|
+
setCount(300);
|
|
32
|
+
|
|
33
|
+
// The State now reflects the latest value.
|
|
34
|
+
$count.get(); // 300
|
|
35
|
+
|
|
36
|
+
// Data can also be updated by passing a function.
|
|
37
|
+
// This function takes the current state and returns a new one.
|
|
38
|
+
setCount((current) => current + 1);
|
|
39
|
+
$count.get(); // 301
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Deriving States from other States
|
|
43
|
+
|
|
44
|
+
#### Example 1: Doubled
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
import { createState, derive } from "@manyducks.co/dolla";
|
|
48
|
+
|
|
49
|
+
const [$count, setCount] = createState(1);
|
|
50
|
+
|
|
51
|
+
const $doubled = derive([$count], (count) => count * 2);
|
|
52
|
+
|
|
53
|
+
setCount(10);
|
|
54
|
+
$doubled.get(); // 20
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
That was a typical toy example where we create a `$doubled` state that always contains the value of `$count`... doubled! This is the essential basic example of computed properties, as written in Dolla.
|
|
58
|
+
|
|
59
|
+
#### Example 2: Selecting a User
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
import { createState, derive } from "@manyducks.co/dolla";
|
|
63
|
+
|
|
64
|
+
const [$users, setUsers] = createState([
|
|
65
|
+
{ id: 1, name: "Audie" },
|
|
66
|
+
{ id: 2, name: "Bob" },
|
|
67
|
+
{ id: 3, name: "Cabel" },
|
|
68
|
+
]);
|
|
69
|
+
const [$selectedUserId, setSelectedUserId] = createState(1);
|
|
70
|
+
|
|
71
|
+
const $selectedUser = derive([$users, $selectedUserId], (users, id) => {
|
|
72
|
+
return users.find((user) => user.id === id);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
$selectedUser.get(); // { id: 1, name: "Audie" }
|
|
76
|
+
|
|
77
|
+
setSelectedId(3);
|
|
78
|
+
|
|
79
|
+
$selectedUser.get(); // { id: 3, name: "Cabel" }
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
That was a more realistic example you might actually use in real life. Here we are selecting a user from a list based on its `id` field. This is kind of similar to a `JOIN` operation in a SQL database. I use this kind of pattern constantly in my apps.
|
|
83
|
+
|
|
84
|
+
The strength of setting up a join like this is that the `$users` array can be updated (by API call, websockets, etc.) and your `$selectedUser` will always be pointing to the latest version of the user data.
|
|
85
|
+
|
|
86
|
+
#### Example 3: Narrowing Complex Data
|
|
87
|
+
|
|
88
|
+
```jsx
|
|
89
|
+
import { createState, derive } from "@manyducks.co/dolla";
|
|
90
|
+
|
|
91
|
+
const [$user, setUser] = createState({ id: 1, name: "Audie" });
|
|
92
|
+
|
|
93
|
+
const $name = derive([$user], (user) => user.name);
|
|
94
|
+
|
|
95
|
+
$name.get(); // "Audie"
|
|
96
|
+
|
|
97
|
+
// In a view:
|
|
98
|
+
<span class="user-name">{$name}</span>;
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Another common pattern. In a real app, most data is stored as arrays of objects. But what you need in order to slot it into a view is just a string. In the example above we've selected the user's name and slotted it into a `span`. If the `$user` value ever changes, the name will stay in sync.
|
|
102
|
+
|
|
103
|
+
### Converting to and from States
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
import { createState, toState, toValue } from "@manyducks.co/dolla";
|
|
107
|
+
|
|
108
|
+
const [$count, setCount] = createState(512);
|
|
109
|
+
|
|
110
|
+
// Unwrap the value of $count. Returns 512.
|
|
111
|
+
const count = toValue($count);
|
|
112
|
+
// Passing a non-state value will simply return it.
|
|
113
|
+
const name = toValue("World");
|
|
114
|
+
|
|
115
|
+
// Wrap "Hello" into a State containing "Hello"
|
|
116
|
+
const $value = toState("Hello");
|
|
117
|
+
// Passing a state will simply return that same state.
|
|
118
|
+
const $number = toState($count);
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### In Views
|
|
122
|
+
|
|
123
|
+
```jsx
|
|
124
|
+
import { derive, createView } from "@manyducks.co/dolla";
|
|
125
|
+
|
|
126
|
+
const UserNameView = createView(function (props) {
|
|
127
|
+
const $name = derive([props.$user], (user) => user.name);
|
|
128
|
+
|
|
129
|
+
return <span class={{ "user-name": true, "is-selected": props.$selected }}>{$name}</span>;
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
In the example above we've displayed the `name` field from a `$user` object inside of a span. We are also assigning an `is-selected` class dynamically based on whether the `$selected` prop contains a truthy or falsy value.
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
End.
|
|
138
|
+
|
|
139
|
+
- [🗂️ Docs](./index.md)
|
|
140
|
+
- [🏠 README](../README.md)
|
|
141
|
+
- [🦆 That's a lot of ducks.](https://www.manyducks.co)
|