@grafctory/clay 0.0.0-stage → 0.5.0
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/LICENSE +201 -0
- package/README.md +57 -2
- package/dist/asset.d.ts +24 -0
- package/dist/asset.d.ts.map +1 -0
- package/dist/asset.js +69 -0
- package/dist/asset.js.map +1 -0
- package/dist/board.d.ts +176 -0
- package/dist/board.d.ts.map +1 -0
- package/dist/board.js +222 -0
- package/dist/board.js.map +1 -0
- package/dist/fetch.d.ts +70 -0
- package/dist/fetch.d.ts.map +1 -0
- package/dist/fetch.js +97 -0
- package/dist/fetch.js.map +1 -0
- package/dist/focus.d.ts +70 -0
- package/dist/focus.d.ts.map +1 -0
- package/dist/focus.js +65 -0
- package/dist/focus.js.map +1 -0
- package/dist/index.d.ts +78 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +137 -0
- package/dist/index.js.map +1 -0
- package/dist/model.d.ts +110 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +148 -0
- package/dist/model.js.map +1 -0
- package/dist/router.d.ts +207 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +828 -0
- package/dist/router.js.map +1 -0
- package/dist/storage.d.ts +38 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +83 -0
- package/dist/storage.js.map +1 -0
- package/dist/transfer.d.ts +402 -0
- package/dist/transfer.d.ts.map +1 -0
- package/dist/transfer.js +732 -0
- package/dist/transfer.js.map +1 -0
- package/package.json +92 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
import { resolve } from "@grafctory/core/resource";
|
|
17
|
+
import { immutable } from "@grafctory/core/values";
|
|
18
|
+
/**
|
|
19
|
+
* Matches the fields that keep their keys and presses for moving a caret or changing a value: enabled text and value
|
|
20
|
+
* controls, native or carrying a matching ARIA role, and editable regions such as rich text editors.
|
|
21
|
+
*/
|
|
22
|
+
const Fields = `
|
|
23
|
+
input:not(
|
|
24
|
+
[disabled],
|
|
25
|
+
[type=button],
|
|
26
|
+
[type=checkbox],
|
|
27
|
+
[type=color],
|
|
28
|
+
[type=file],
|
|
29
|
+
[type=image],
|
|
30
|
+
[type=radio],
|
|
31
|
+
[type=reset],
|
|
32
|
+
[type=submit]
|
|
33
|
+
),
|
|
34
|
+
select:not([disabled]),
|
|
35
|
+
textarea:not([disabled]),
|
|
36
|
+
[contenteditable]:not([contenteditable=false]),
|
|
37
|
+
[role~=combobox],
|
|
38
|
+
[role~=scrollbar],
|
|
39
|
+
[role~=searchbox],
|
|
40
|
+
[role~=slider],
|
|
41
|
+
[role~=spinbutton],
|
|
42
|
+
[role~=textbox]
|
|
43
|
+
`.replace(/\s+/g, " ");
|
|
44
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
45
|
+
/**
|
|
46
|
+
* Holds each entry of {@link app}, once first read.
|
|
47
|
+
*/
|
|
48
|
+
const cache = {};
|
|
49
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
50
|
+
/**
|
|
51
|
+
* What the app says about itself, as its document states it.
|
|
52
|
+
*
|
|
53
|
+
* Read on first access and immutable thereafter, so a later edit to the document is never picked up.
|
|
54
|
+
*/
|
|
55
|
+
export const app = immutable({
|
|
56
|
+
/**
|
|
57
|
+
* The absolute root URL of the site, with a trailing slash.
|
|
58
|
+
*/
|
|
59
|
+
get root() {
|
|
60
|
+
return cache.root ??= resolve(location.href, "/");
|
|
61
|
+
},
|
|
62
|
+
/**
|
|
63
|
+
* The absolute base URL the app is published at, with a trailing slash, as `<base>` states it, or the site root.
|
|
64
|
+
*/
|
|
65
|
+
get base() {
|
|
66
|
+
return cache.base ??= resolve(app.root, (document.querySelector("base")?.href || "/").replace(/\/*$/, "/"));
|
|
67
|
+
},
|
|
68
|
+
/**
|
|
69
|
+
* The app name, as the `<title>` tag states it, or `undefined` if the tag is missing or empty.
|
|
70
|
+
*/
|
|
71
|
+
get name() {
|
|
72
|
+
return "name" in cache ? cache.name : cache.name = document.title || undefined;
|
|
73
|
+
},
|
|
74
|
+
/**
|
|
75
|
+
* The URL of the app icon, as the `<link rel="icon">` tag states it, or `undefined` if the tag is missing.
|
|
76
|
+
*/
|
|
77
|
+
get icon() {
|
|
78
|
+
return "icon" in cache ? cache.icon
|
|
79
|
+
: cache.icon = document.querySelector("link[rel=icon]")?.href || undefined;
|
|
80
|
+
},
|
|
81
|
+
/**
|
|
82
|
+
* The app description, as the `<meta name="description">` tag states it, or `undefined` if missing or empty.
|
|
83
|
+
*/
|
|
84
|
+
get info() {
|
|
85
|
+
return "info" in cache ? cache.info
|
|
86
|
+
: cache.info = document.querySelector("meta[name=description]")?.content || undefined;
|
|
87
|
+
},
|
|
88
|
+
/**
|
|
89
|
+
* The app copyright, as the `<meta name="copyright">` tag states it, or `undefined` if missing or empty.
|
|
90
|
+
*/
|
|
91
|
+
get copy() {
|
|
92
|
+
return "copy" in cache ? cache.copy
|
|
93
|
+
: cache.copy = document.querySelector("meta[name=copyright]")?.content || undefined;
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
97
|
+
/**
|
|
98
|
+
* Checks whether an event is reserved for the field it lands in.
|
|
99
|
+
*
|
|
100
|
+
* A widget handling keys or presses on its own, for example moving the focus with arrow keys or dragging an item by
|
|
101
|
+
* its surface, leaves reserved events alone. A field inside the widget then keeps the keys and presses that move its
|
|
102
|
+
* caret or change its value, and works there as it does anywhere else on the page.
|
|
103
|
+
*
|
|
104
|
+
* Fields are enabled text and value controls, native or carrying a matching ARIA role, and editable regions. Buttons,
|
|
105
|
+
* checkboxes, radio buttons and the other controls acting on a single press are not fields.
|
|
106
|
+
*
|
|
107
|
+
* @param event The key or pointer event to check
|
|
108
|
+
*
|
|
109
|
+
* @returns true if `event` targets a field or an element inside one; false otherwise
|
|
110
|
+
*/
|
|
111
|
+
export function isReserved(event) {
|
|
112
|
+
return event.target instanceof Element && event.target.closest(Fields) !== null;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Settles the custom element a page is rendered into.
|
|
116
|
+
*
|
|
117
|
+
* Hands out a stable root for a rendering call: the same element answers every call naming it, so that repeated
|
|
118
|
+
* calls, as issued on hot reload, replace the page rather than adding another copy of it.
|
|
119
|
+
*
|
|
120
|
+
* An element created here takes the height of whatever holds it and stretches the frame rendered into it across its
|
|
121
|
+
* whole box, so the frame fills the page rather than standing as tall as its content: an app states once, in its own
|
|
122
|
+
* document, that the body is as tall as the window, and gets a root already fitted to it. An element the document
|
|
123
|
+
* already carries is handed back as it stands, leaving its layout to the app that placed it.
|
|
124
|
+
*
|
|
125
|
+
* @param name The name of the hosting custom element, hyphenated as the DOM requires
|
|
126
|
+
*
|
|
127
|
+
* @returns The element named `name`, appended to the body and fitted to it unless the document already carries one
|
|
128
|
+
*/
|
|
129
|
+
export function host(name) {
|
|
130
|
+
return document.querySelector(name) ?? fitted(document.body.appendChild(document.createElement(name)));
|
|
131
|
+
// the root is styled here rather than in a stylesheet, its name being the caller's and so unknown to any rule
|
|
132
|
+
function fitted(element) {
|
|
133
|
+
element.setAttribute("style", "display: grid; grid-template: minmax(0, 1fr) / minmax(0, 1fr); height: 100%");
|
|
134
|
+
return element;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAkBH,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAGnD;;;GAGG;AACH,MAAM,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;;CAqBd,CAAC,OAAO,CACR,MAAM,EAAE,GAAG,CACX,CAAC;AAGF,wHAAwH;AAExH;;GAEG;AACH,MAAM,KAAK,GAUP,EAAE,CAAC;AAGP,wHAAwH;AAExH;;;;GAIG;AACH,MAAM,CAAC,MAAM,GAAG,GAAG,SAAS,CAAC;IAE5B;;OAEG;IACH,IAAI,IAAI;QACP,OAAO,KAAK,CAAC,IAAI,KAAK,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;IACnD,CAAC;IAED;;OAEG;IACH,IAAI,IAAI;QACP,OAAO,KAAK,CAAC,IAAI,KAAK,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,EAAE,IAAI,IAAI,GAAG,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;IAC7G,CAAC;IAGD;;OAEG;IACH,IAAI,IAAI;QACP,OAAO,MAAM,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,GAAG,QAAQ,CAAC,KAAK,IAAI,SAAS,CAAC;IAChF,CAAC;IAED;;OAEG;IACH,IAAI,IAAI;QACP,OAAO,MAAM,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI;YAClC,CAAC,CAAC,KAAK,CAAC,IA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|
+
*
|
|
4
|
+
* Turns an immutable state object into the state of a component: {@link useModel} holds it for the lifetime of the
|
|
5
|
+
* component and renders again after every transition that changes something, so behaviour defined once as a state
|
|
6
|
+
* object drives any component that adopts it.
|
|
7
|
+
*
|
|
8
|
+
* **Usage**
|
|
9
|
+
*
|
|
10
|
+
* Declare what the component is responsible for as a state interface, with read-only data and transition methods:
|
|
11
|
+
*
|
|
12
|
+
* ```typescript
|
|
13
|
+
* interface Counter {
|
|
14
|
+
*
|
|
15
|
+
* readonly value: number;
|
|
16
|
+
*
|
|
17
|
+
* up(): this;
|
|
18
|
+
* reset(): this;
|
|
19
|
+
*
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* Give it a factory, which may share the interface name, as types and values live in separate namespaces:
|
|
24
|
+
*
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { createState } from "@grafctory/core/state";
|
|
27
|
+
*
|
|
28
|
+
* function Counter() {
|
|
29
|
+
* return createState<Counter>({
|
|
30
|
+
*
|
|
31
|
+
* value: 0,
|
|
32
|
+
*
|
|
33
|
+
* up() { return { value: this.value + 1 }; },
|
|
34
|
+
* reset() { return { value: 0 }; }
|
|
35
|
+
*
|
|
36
|
+
* });
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* Where a component already answers to `Counter`, name the factory `createCounter`: a module cannot import both.
|
|
41
|
+
*
|
|
42
|
+
* Then read data and transitions off the model: a transition is bound to the state it was read from, so a
|
|
43
|
+
* zero-argument one goes straight to a handler, and any of them may be stored and passed on:
|
|
44
|
+
*
|
|
45
|
+
* ```tsx
|
|
46
|
+
* export function CounterButton() {
|
|
47
|
+
*
|
|
48
|
+
* const { value, up, reset } = useModel(Counter);
|
|
49
|
+
*
|
|
50
|
+
* return <>
|
|
51
|
+
* <button onClick={up}>+</button>
|
|
52
|
+
* <button onClick={reset}>~</button>
|
|
53
|
+
* <output>{value}</output>
|
|
54
|
+
* </>;
|
|
55
|
+
*
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
59
|
+
* **Transitions do not accumulate**
|
|
60
|
+
*
|
|
61
|
+
* Every transition starts from the state of the render that read it, so taking one twice lands where once does:
|
|
62
|
+
*
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const twice = () => {
|
|
65
|
+
* up(); // value + 1
|
|
66
|
+
* up(); // value + 1 again, not value + 2
|
|
67
|
+
* };
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* A transition also notifies asynchronously, so data read alongside keeps its earlier value until the next render:
|
|
71
|
+
*
|
|
72
|
+
* ```typescript
|
|
73
|
+
* const report = () => {
|
|
74
|
+
* up();
|
|
75
|
+
* console.log(value); // still the value before the transition
|
|
76
|
+
* };
|
|
77
|
+
* ```
|
|
78
|
+
*
|
|
79
|
+
* **Unmounting**
|
|
80
|
+
*
|
|
81
|
+
* A model needs no cleanup when the component goes away, nothing outliving it to detach from: the model, its
|
|
82
|
+
* observers and the setter they notify become unreachable together. This holds as long as models stay local to the
|
|
83
|
+
* component, so a model is never parked in a module-level cache or handed to an external store. A transition taken
|
|
84
|
+
* just before unmounting notifies after it, which the state module absorbs without effect.
|
|
85
|
+
*
|
|
86
|
+
* @module
|
|
87
|
+
*/
|
|
88
|
+
import { type Lazy } from "@grafctory/core";
|
|
89
|
+
import { type Instance, type State } from "@grafctory/core/state";
|
|
90
|
+
/**
|
|
91
|
+
* Adopts a state object as the state of a component.
|
|
92
|
+
*
|
|
93
|
+
* Renders the component again after every transition that changes something, never after one returning the same state.
|
|
94
|
+
*
|
|
95
|
+
* @typeParam T The state interface
|
|
96
|
+
*
|
|
97
|
+
* A model resolved from props is kept in step with them by listing them as dependencies: whenever one of them changes,
|
|
98
|
+
* the model is created again from the factory and the state it reached is discarded, so a list handed a new query
|
|
99
|
+
* starts again from its first page. The new model is the one returned by the render the change shows up in.
|
|
100
|
+
*
|
|
101
|
+
* @param model The model, or a factory creating it; resolved when the component first renders, and again whenever
|
|
102
|
+
* one of `dependencies` changes, so a factory may be written inline and close over props: a prop left out of
|
|
103
|
+
* `dependencies` reaches the model as it stood when the model was last created
|
|
104
|
+
* @param dependencies The values the model is created from, compared for {@link equals deep equality} with the ones
|
|
105
|
+
* of the previous render; the model is kept for the lifetime of the component if omitted
|
|
106
|
+
*
|
|
107
|
+
* @returns The model as it stands for this render, superseded by the next after every transition that changes something
|
|
108
|
+
*/
|
|
109
|
+
export declare function useModel<T extends State<T>>(model: Lazy<Instance<T>>, dependencies?: readonly unknown[]): Instance<T>;
|
|
110
|
+
//# sourceMappingURL=model.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model.d.ts","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAgBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsFG;AAEH,OAAO,EAAS,KAAK,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,KAAK,QAAQ,EAAe,KAAK,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAK/E;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,YAAY,CAAC,EAAE,SAAS,OAAO,EAAE,GAAG,QAAQ,CAAC,CAAC,CAAC,CAmCrH"}
|
package/dist/model.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright © 2026 Metreeca srl
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Headless component state.
|
|
18
|
+
*
|
|
19
|
+
* Turns an immutable state object into the state of a component: {@link useModel} holds it for the lifetime of the
|
|
20
|
+
* component and renders again after every transition that changes something, so behaviour defined once as a state
|
|
21
|
+
* object drives any component that adopts it.
|
|
22
|
+
*
|
|
23
|
+
* **Usage**
|
|
24
|
+
*
|
|
25
|
+
* Declare what the component is responsible for as a state interface, with read-only data and transition methods:
|
|
26
|
+
*
|
|
27
|
+
* ```typescript
|
|
28
|
+
* interface Counter {
|
|
29
|
+
*
|
|
30
|
+
* readonly value: number;
|
|
31
|
+
*
|
|
32
|
+
* up(): this;
|
|
33
|
+
* reset(): this;
|
|
34
|
+
*
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* Give it a factory, which may share the interface name, as types and values live in separate namespaces:
|
|
39
|
+
*
|
|
40
|
+
* ```typescript
|
|
41
|
+
* import { createState } from "@grafctory/core/state";
|
|
42
|
+
*
|
|
43
|
+
* function Counter() {
|
|
44
|
+
* return createState<Counter>({
|
|
45
|
+
*
|
|
46
|
+
* value: 0,
|
|
47
|
+
*
|
|
48
|
+
* up() { return { value: this.value + 1 }; },
|
|
49
|
+
* reset() { return { value: 0 }; }
|
|
50
|
+
*
|
|
51
|
+
* });
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* Where a component already answers to `Counter`, name the factory `createCounter`: a module cannot import both.
|
|
56
|
+
*
|
|
57
|
+
* Then read data and transitions off the model: a transition is bound to the state it was read from, so a
|
|
58
|
+
* zero-argument one goes straight to a handler, and any of them may be stored and passed on:
|
|
59
|
+
*
|
|
60
|
+
* ```tsx
|
|
61
|
+
* export function CounterButton() {
|
|
62
|
+
*
|
|
63
|
+
* const { value, up, reset } = useModel(Counter);
|
|
64
|
+
*
|
|
65
|
+
* return <>
|
|
66
|
+
* <button onClick={up}>+</button>
|
|
67
|
+
* <button onClick={reset}>~</button>
|
|
68
|
+
* <output>{value}</output>
|
|
69
|
+
* </>;
|
|
70
|
+
*
|
|
71
|
+
* }
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* **Transitions do not accumulate**
|
|
75
|
+
*
|
|
76
|
+
* Every transition starts from the state of the render that read it, so taking one twice lands where once does:
|
|
77
|
+
*
|
|
78
|
+
* ```typescript
|
|
79
|
+
* const twice = () => {
|
|
80
|
+
* up(); // value + 1
|
|
81
|
+
* up(); // value + 1 again, not value + 2
|
|
82
|
+
* };
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* A transition also notifies asynchronously, so data read alongside keeps its earlier value until the next render:
|
|
86
|
+
*
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const report = () => {
|
|
89
|
+
* up();
|
|
90
|
+
* console.log(value); // still the value before the transition
|
|
91
|
+
* };
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* **Unmounting**
|
|
95
|
+
*
|
|
96
|
+
* A model needs no cleanup when the component goes away, nothing outliving it to detach from: the model, its
|
|
97
|
+
* observers and the setter they notify become unreachable together. This holds as long as models stay local to the
|
|
98
|
+
* component, so a model is never parked in a module-level cache or handed to an external store. A transition taken
|
|
99
|
+
* just before unmounting notifies after it, which the state module absorbs without effect.
|
|
100
|
+
*
|
|
101
|
+
* @module
|
|
102
|
+
*/
|
|
103
|
+
import { eager } from "@grafctory/core";
|
|
104
|
+
import { manageState } from "@grafctory/core/state";
|
|
105
|
+
import { equals } from "@grafctory/core/values";
|
|
106
|
+
import { useState } from "preact/hooks";
|
|
107
|
+
/**
|
|
108
|
+
* Adopts a state object as the state of a component.
|
|
109
|
+
*
|
|
110
|
+
* Renders the component again after every transition that changes something, never after one returning the same state.
|
|
111
|
+
*
|
|
112
|
+
* @typeParam T The state interface
|
|
113
|
+
*
|
|
114
|
+
* A model resolved from props is kept in step with them by listing them as dependencies: whenever one of them changes,
|
|
115
|
+
* the model is created again from the factory and the state it reached is discarded, so a list handed a new query
|
|
116
|
+
* starts again from its first page. The new model is the one returned by the render the change shows up in.
|
|
117
|
+
*
|
|
118
|
+
* @param model The model, or a factory creating it; resolved when the component first renders, and again whenever
|
|
119
|
+
* one of `dependencies` changes, so a factory may be written inline and close over props: a prop left out of
|
|
120
|
+
* `dependencies` reaches the model as it stood when the model was last created
|
|
121
|
+
* @param dependencies The values the model is created from, compared for {@link equals deep equality} with the ones
|
|
122
|
+
* of the previous render; the model is kept for the lifetime of the component if omitted
|
|
123
|
+
*
|
|
124
|
+
* @returns The model as it stands for this render, superseded by the next after every transition that changes something
|
|
125
|
+
*/
|
|
126
|
+
export function useModel(model, dependencies) {
|
|
127
|
+
const [entry, setEntry] = useState(create);
|
|
128
|
+
const current = equals(entry.key, dependencies) ? entry : create();
|
|
129
|
+
/*
|
|
130
|
+
* A model created again is returned by this very render rather than the next one, so the component never shows
|
|
131
|
+
* the state it is discarding alongside the props that discarded it; storing it here keeps it for the renders after.
|
|
132
|
+
*/
|
|
133
|
+
if (current !== entry) {
|
|
134
|
+
setEntry(current);
|
|
135
|
+
}
|
|
136
|
+
return current.model;
|
|
137
|
+
/*
|
|
138
|
+
* Every notification carries the key the model was created under, so one arriving from a model already superseded
|
|
139
|
+
* leaves the entry in place: transitions do not survive the model they were taken on.
|
|
140
|
+
*/
|
|
141
|
+
function create() {
|
|
142
|
+
return {
|
|
143
|
+
key: dependencies,
|
|
144
|
+
model: manageState(eager(model)).attach(state => setEntry(entry => entry.key === dependencies ? { ...entry, model: state } : entry))
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=model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SAEF,CAAC;IACH,CAAC;AAEF,CAAC"}
|
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { type ComponentChildren, type VNode } from "preact";
|
|
2
|
+
/**
|
|
3
|
+
* Route navigator.
|
|
4
|
+
*
|
|
5
|
+
* Moves the page to another route, updating the browser location, the page title and the history state together.
|
|
6
|
+
*/
|
|
7
|
+
export interface Router {
|
|
8
|
+
/**
|
|
9
|
+
* Navigates to a route.
|
|
10
|
+
*
|
|
11
|
+
* The title names the page on show. {@link useTitle} returns it tidied, and the document title qualifies it with
|
|
12
|
+
* the {@link app} name, as `Title | App`, so every page names the app alongside itself. An empty title, or one
|
|
13
|
+
* equal to the app name, leaves the document title to the app name alone, with no page name ahead of it.
|
|
14
|
+
*
|
|
15
|
+
* Passing a title alone sets it without navigating and renders again only the components reading it. The views
|
|
16
|
+
* below the enclosing {@link Router} render again only if the route actually changes, so a view may safely set its
|
|
17
|
+
* title from an effect as it mounts, without rendering itself again or losing the state it holds.
|
|
18
|
+
*
|
|
19
|
+
* @param route The route or the navigation details; a route relative to the current one resolves as a link would
|
|
20
|
+
*/
|
|
21
|
+
(route: string | {
|
|
22
|
+
/**
|
|
23
|
+
* True to replace the current history entry rather than add one; navigating to the current route always does.
|
|
24
|
+
*
|
|
25
|
+
* @defaultValue false
|
|
26
|
+
*/
|
|
27
|
+
replace?: boolean;
|
|
28
|
+
/**
|
|
29
|
+
* The route to navigate to, along with any annex it carries; the current route if omitted.
|
|
30
|
+
*/
|
|
31
|
+
route?: string;
|
|
32
|
+
/**
|
|
33
|
+
* The title of the page navigated to; if omitted, the current title while the route stays the same, whatever
|
|
34
|
+
* its query or annex, and none on reaching another route, until its view sets one.
|
|
35
|
+
*/
|
|
36
|
+
title?: string;
|
|
37
|
+
/**
|
|
38
|
+
* The history state; the current state if omitted, cleared if `null`.
|
|
39
|
+
*/
|
|
40
|
+
state?: unknown;
|
|
41
|
+
}): void;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Tracks the current route.
|
|
45
|
+
*
|
|
46
|
+
* Provides the current route, its annex, the page title and a navigator to the components below it, through
|
|
47
|
+
* {@link useRoute}, {@link useAnnex}, {@link useTitle} and {@link useRouter}. The views render again whenever the
|
|
48
|
+
* route or its annex changes, whether through navigation or browser history; a change of title alone renders again
|
|
49
|
+
* only the components reading it. The {@link Routes} nested below it select the views for the current route, at any
|
|
50
|
+
* depth and wherever the page layout places them, so a layout is free to arrange the views it wraps.
|
|
51
|
+
*
|
|
52
|
+
* The title belongs to the route it was set on. Reaching another route clears it, whether by a link, by browser
|
|
53
|
+
* history or by a navigation naming no title, so the title of the page left behind is never taken for the new one.
|
|
54
|
+
* The view arrived on sets its own title again as it mounts. This also covers browser history, since the browser
|
|
55
|
+
* keeps no title for a history entry. A redirection carries the title along to the route it lands on.
|
|
56
|
+
*
|
|
57
|
+
* > [!IMPORTANT]
|
|
58
|
+
* >
|
|
59
|
+
* > A title is expected to be set by a view that {@link Routes} renders, since such a view mounts afresh on every
|
|
60
|
+
* > route. A component staying mounted across routes, such as a layout wrapping the views, has to set its title again
|
|
61
|
+
* > on every change of route, for instance from an effect depending on {@link useRoute}. Otherwise the title is cleared
|
|
62
|
+
* > on the first navigation away, leaving the document title to the app name alone.
|
|
63
|
+
*
|
|
64
|
+
* A page loaded or reloaded on a fragment is brought to the element the fragment names, if the view holds it on its
|
|
65
|
+
* first render. The browser cannot do this on its own, as it looks for the element before the view is rendered.
|
|
66
|
+
*
|
|
67
|
+
* Going back or forward through the browser history returns the reader to where they were reading. The window gets
|
|
68
|
+
* back the position it had as the entry was left, and so does every box carrying an `id` within the `main` landmark,
|
|
69
|
+
* the landmark included. A box without an `id` opens at its top. A box meant to keep its place, such as the body a list
|
|
70
|
+
* scrolls in, needs an `id` that stays the same each time its view mounts, unlike one generated by `useId`. Restoring
|
|
71
|
+
* waits for content the view loads after landing, so a list grows back to its length before its position is restored.
|
|
72
|
+
* It stops once the reader scrolls, points or presses a key, or after a few seconds, never moving the page under them.
|
|
73
|
+
*
|
|
74
|
+
* Positions are kept per history entry in `sessionStorage`, leaving the history state to the app. Restoring them takes
|
|
75
|
+
* the [Navigation API](https://developer.mozilla.org/docs/Web/API/Navigation_API): browsers lacking it fall back on
|
|
76
|
+
* their own scroll restoring, which brings back the position of the window alone and leaves every box at its top.
|
|
77
|
+
*
|
|
78
|
+
* Takes over plain clicks anywhere in the page, leaving clicks with a modifier key or already handled to the browser:
|
|
79
|
+
*
|
|
80
|
+
* - a link to the same site is followed without reloading the page, unless it points at a fragment of the current
|
|
81
|
+
* page or targets another browsing context through a `target` attribute other than `_self`; a link leading to
|
|
82
|
+
* another route, or back to the current place, then lands the page as a page loaded afresh would, at its top or on
|
|
83
|
+
* the element its fragment names once the view shows it, so the reader is never left halfway down the view they
|
|
84
|
+
* left; the top is the start of the document, of the `main` landmark and of every box scrolling inside it, so an app
|
|
85
|
+
* scrolling its content in a box of its own lands there as well, while a box scrolling outside the landmark, such
|
|
86
|
+
* as a side column, keeps its place; a link changing the query or the annex alone, as a filter or an alternate view
|
|
87
|
+
* does, keeps the reader in place on whatever they were reading before following it
|
|
88
|
+
* - a page landed afresh also moves the focus to its `main` landmark, so a screen reader announces the page arrived
|
|
89
|
+
* on; the landmark takes the focus only if it is focusable, as a `tabindex` of `-1` makes it without adding a tab
|
|
90
|
+
* stop, and may pass it on to an element within itself, such as the box scrolling its content; otherwise the focus
|
|
91
|
+
* is cleared, so the next keystroke never acts on a control of the page left behind
|
|
92
|
+
* - a link to another site opens in a new browsing context
|
|
93
|
+
* - an image outside any link toggles its `active` attribute, which the `@grafctory/skin` stylesheet reads to show it
|
|
94
|
+
* over the whole viewport; an image inside a link is left to the link
|
|
95
|
+
*
|
|
96
|
+
* An enlarged image is restored by another click on it, by `Escape`, or as soon as the focus moves, so a keyboard user
|
|
97
|
+
* never ends up on a control the image covers. Enlarging is a visual convenience over content the image already
|
|
98
|
+
* carries, through its alternative text and the browser zoom, so images are not made reachable from the keyboard.
|
|
99
|
+
*
|
|
100
|
+
* A page mounts a single router, as each one takes over the clicks and keys of the whole page.
|
|
101
|
+
*
|
|
102
|
+
* @param options The router configuration
|
|
103
|
+
*
|
|
104
|
+
* @returns The rendered children
|
|
105
|
+
*/
|
|
106
|
+
export declare function Router({ children }: {
|
|
107
|
+
/**
|
|
108
|
+
* The content rendered within the router, nesting the {@link Routes} that select the views for the current route.
|
|
109
|
+
*/
|
|
110
|
+
children?: ComponentChildren;
|
|
111
|
+
}): VNode<{
|
|
112
|
+
value: Readonly<{
|
|
113
|
+
/**
|
|
114
|
+
* The route in the browser address bar without its annex, as returned by {@link useRoute}.
|
|
115
|
+
*/
|
|
116
|
+
route: string;
|
|
117
|
+
/**
|
|
118
|
+
* The annex of the route in the browser address bar, as returned by {@link useAnnex}.
|
|
119
|
+
*/
|
|
120
|
+
annex: string;
|
|
121
|
+
/**
|
|
122
|
+
* The route without its annex and the query the last navigation moved to, keying the views {@link Routes} renders:
|
|
123
|
+
* a navigation changing the query mounts them afresh, while a change of annex alone, or a query replaced outside
|
|
124
|
+
* the router through `history.replaceState`, leaves them mounted with the state they reached.
|
|
125
|
+
*/
|
|
126
|
+
place: string;
|
|
127
|
+
/**
|
|
128
|
+
* The navigator, as returned by {@link useRouter}.
|
|
129
|
+
*/
|
|
130
|
+
navigate: Router;
|
|
131
|
+
}>;
|
|
132
|
+
children?: ComponentChildren;
|
|
133
|
+
}>;
|
|
134
|
+
/**
|
|
135
|
+
* Renders the view for the current route.
|
|
136
|
+
*
|
|
137
|
+
* Selects the view through a table of route patterns, moving the location along any redirection on the way, and
|
|
138
|
+
* renders it wherever the page layout places it; within a view, a nested table routes what the view pattern leaves
|
|
139
|
+
* over as a section of its own. Patterns, redirections and sections are described in the module overview.
|
|
140
|
+
*
|
|
141
|
+
* Renders below a {@link Router}, which keeps sole charge of the location, the history and the page clicks.
|
|
142
|
+
*
|
|
143
|
+
* Renders nothing during a redirection, so neither it nor a nested {@link Routes} shows a route on its way out.
|
|
144
|
+
*
|
|
145
|
+
* Mounts the view afresh whenever a navigation moves the location, the query included, so that a view reads the query
|
|
146
|
+
* as it mounts: a link from `/courses/` to `/courses/?~label=art` mounts the catalogue again with the new criteria. A
|
|
147
|
+
* query replaced through `history.replaceState` outside the router, as a view recording its own state in the location
|
|
148
|
+
* does, leaves the view mounted with the state it reached, until the next navigation moves the location again. A
|
|
149
|
+
* navigation changing the annex alone leaves the view mounted too, rendering again only what reads the annex.
|
|
150
|
+
*
|
|
151
|
+
* @param options The routes configuration
|
|
152
|
+
*
|
|
153
|
+
* @returns The view for the current route
|
|
154
|
+
*
|
|
155
|
+
* @throws {@link !Error Error} If a pattern other than `*` lacks a leading `/`, holds `*`, or holds `;` anywhere but
|
|
156
|
+
* before a non-empty annex on its last step, as soon as the table renders, whatever the route
|
|
157
|
+
* @throws {@link !Error Error} If the route, or one it redirects to, matches no pattern and no enclosing catch-all
|
|
158
|
+
* @throws {@link !Error Error} If redirections lead back to a route already visited
|
|
159
|
+
*/
|
|
160
|
+
export declare function Routes({ children }: {
|
|
161
|
+
/**
|
|
162
|
+
* The views for the routes, as a table mapping route patterns to views, rendered as they are, or to redirections,
|
|
163
|
+
* as routes to move to instead; a route is handled by the first pattern it matches, in table order.
|
|
164
|
+
*/
|
|
165
|
+
children: {
|
|
166
|
+
readonly [pattern: string]: string | VNode;
|
|
167
|
+
};
|
|
168
|
+
}): ComponentChildren;
|
|
169
|
+
/**
|
|
170
|
+
* Retrieves the route navigator.
|
|
171
|
+
*
|
|
172
|
+
* The navigator stays the same for as long as the {@link Router} providing it is mounted, so a component reading it
|
|
173
|
+
* renders again only as its own state requires, and a handler may keep it across navigations.
|
|
174
|
+
*
|
|
175
|
+
* @returns The navigator provided by the innermost enclosing {@link Router}; a no-op outside any router
|
|
176
|
+
*/
|
|
177
|
+
export declare function useRouter(): Router;
|
|
178
|
+
/**
|
|
179
|
+
* Retrieves the current route.
|
|
180
|
+
*
|
|
181
|
+
* Renders the component again whenever the route changes, whether through navigation or browser history.
|
|
182
|
+
*
|
|
183
|
+
* @returns The current route, as carried by the location path without its annex; an empty string outside any router
|
|
184
|
+
*/
|
|
185
|
+
export declare function useRoute(): string;
|
|
186
|
+
/**
|
|
187
|
+
* Retrieves the annex of the current route.
|
|
188
|
+
*
|
|
189
|
+
* Renders the component again whenever the annex changes, whether through navigation or browser history. The views
|
|
190
|
+
* below the router stay mounted, so a view switching presentation keeps the state it reached.
|
|
191
|
+
*
|
|
192
|
+
* @returns The annex after the `;` on the last step of the current route, as it appears in the location path and left
|
|
193
|
+
* to the caller to parse; an empty string if the route carries none, or outside any router
|
|
194
|
+
*/
|
|
195
|
+
export declare function useAnnex(): string;
|
|
196
|
+
/**
|
|
197
|
+
* Retrieves the title of the page on show.
|
|
198
|
+
*
|
|
199
|
+
* Renders the component again whenever the title changes, whether set through the navigator, alone or along with a
|
|
200
|
+
* route, or cleared on reaching another route. Lets a component shared by every page, such as a feedback or share
|
|
201
|
+
* action, name the page it is shown on without being handed the title the page already gave the navigator.
|
|
202
|
+
*
|
|
203
|
+
* @returns The title last passed to the navigator for the current route, tidied and without the {@link app} name; an
|
|
204
|
+
* empty string if none was set, if it equals the app name, or outside any router
|
|
205
|
+
*/
|
|
206
|
+
export declare function useTitle(): string;
|
|
207
|
+
//# sourceMappingURL=router.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.ts"],"names":[],"mappings":"AAsJA,OAAO,EAAE,KAAK,iBAAiB,EAAgC,KAAK,KAAK,EAAE,MAAM,QAAQ,CAAC;AAsH1F;;;;GAIG;AACH,MAAM,WAAW,MAAM;IAEtB;;;;;;;;;;;;OAYG;IACH,CAAC,KAAK,EAAE,MAAM,GAAG;QAEhB;;;;WAIG;QACH,OAAO,CAAC,EAAE,OAAO,CAAA;QAGjB;;WAEG;QACH,KAAK,CAAC,EAAE,MAAM,CAAA;QAEd;;;WAGG;QACH,KAAK,CAAC,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