@k8ordo/router 1.2.0 → 1.3.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/dist/navigation.d.mts +10 -4
- package/dist/navigation.mjs +87 -9
- package/dist/router.mjs +16 -6
- package/dist/tree-pathname.d.mts +4 -0
- package/dist/tree-pathname.mjs +16 -0
- package/docs/GUIDE.md +32 -12
- package/docs/llms.txt +3 -2
- package/package.json +1 -1
package/dist/navigation.d.mts
CHANGED
|
@@ -40,14 +40,17 @@ export type NavigationHandler<T> = {
|
|
|
40
40
|
* load — the page showing reads the search, and the search moved. Without
|
|
41
41
|
* one, such a navigation is a state change: nothing loads. With one, it
|
|
42
42
|
* loads and applies like a page change, but as a state change otherwise:
|
|
43
|
-
* no
|
|
43
|
+
* no focus reset and no transition types, and the viewport stays where it
|
|
44
|
+
* is — except going back or forward, where the browser restores the
|
|
45
|
+
* position that entry was left at once the page loaded again is on screen.
|
|
44
46
|
*/
|
|
45
47
|
refresh?: (url: URL) => boolean;
|
|
46
48
|
};
|
|
47
49
|
/**
|
|
48
50
|
* Where the viewport goes once the new page is on screen. A traversal is the
|
|
49
|
-
* browser's to restore, so it carries no instruction
|
|
50
|
-
*
|
|
51
|
+
* browser's to restore, so it carries no instruction — the browser restores
|
|
52
|
+
* it once the handler settles; a fragment scrolls to its target; anything
|
|
53
|
+
* else starts at the top, the way a document load does.
|
|
51
54
|
*/
|
|
52
55
|
export type ScrollPlan = {
|
|
53
56
|
kind: 'top';
|
|
@@ -91,7 +94,10 @@ export declare const NavigationGeneration: import("react").Context<number>;
|
|
|
91
94
|
* is what makes `navigation.navigate().finished` mean "the page is showing".
|
|
92
95
|
* A navigation that keeps the pathname on screen — `@k8ordo/state`'s
|
|
93
96
|
* `update()` — is intercepted with no handler, so its `finished` settles as
|
|
94
|
-
* soon as the navigation commits.
|
|
97
|
+
* soon as the navigation commits. Going back or forward between two entries
|
|
98
|
+
* of that page is the exception: it settles with the render the entry change
|
|
99
|
+
* starts in the background, so the browser restores the position onto what
|
|
100
|
+
* that entry renders.
|
|
95
101
|
*
|
|
96
102
|
* The new tree renders at the lane `useDeferredValue` gives it, not in a
|
|
97
103
|
* transition. While any async action is pending, React holds every
|
package/dist/navigation.mjs
CHANGED
|
@@ -50,6 +50,36 @@ const applyScroll = (plan) => {
|
|
|
50
50
|
top: -1
|
|
51
51
|
});
|
|
52
52
|
};
|
|
53
|
+
let anchoringHeld = 0;
|
|
54
|
+
let anchoringBefore = "";
|
|
55
|
+
const keepPosition = (entry) => {
|
|
56
|
+
const left = window.scrollX;
|
|
57
|
+
const top = window.scrollY;
|
|
58
|
+
setTimeout(() => {
|
|
59
|
+
if (navigation.currentEntry?.key !== entry) return;
|
|
60
|
+
if (window.scrollX !== left || window.scrollY !== top) window.scrollTo({
|
|
61
|
+
left,
|
|
62
|
+
top
|
|
63
|
+
});
|
|
64
|
+
}, 0);
|
|
65
|
+
};
|
|
66
|
+
const holdAnchoring = () => {
|
|
67
|
+
const { style } = document.documentElement;
|
|
68
|
+
if (anchoringHeld === 0) {
|
|
69
|
+
anchoringBefore = style.overflowAnchor;
|
|
70
|
+
style.overflowAnchor = "none";
|
|
71
|
+
}
|
|
72
|
+
anchoringHeld += 1;
|
|
73
|
+
let released = false;
|
|
74
|
+
return () => {
|
|
75
|
+
if (released) return;
|
|
76
|
+
released = true;
|
|
77
|
+
anchoringHeld -= 1;
|
|
78
|
+
if (anchoringHeld > 0) return;
|
|
79
|
+
document.documentElement.getBoundingClientRect();
|
|
80
|
+
style.overflowAnchor = anchoringBefore;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
53
83
|
/**
|
|
54
84
|
* The pathname a page change is loading, in the table's terms — `null` when
|
|
55
85
|
* none is. One per document, like the navigation it follows: there is one
|
|
@@ -96,7 +126,10 @@ const NavigationGeneration = createContext(-1);
|
|
|
96
126
|
* is what makes `navigation.navigate().finished` mean "the page is showing".
|
|
97
127
|
* A navigation that keeps the pathname on screen — `@k8ordo/state`'s
|
|
98
128
|
* `update()` — is intercepted with no handler, so its `finished` settles as
|
|
99
|
-
* soon as the navigation commits.
|
|
129
|
+
* soon as the navigation commits. Going back or forward between two entries
|
|
130
|
+
* of that page is the exception: it settles with the render the entry change
|
|
131
|
+
* starts in the background, so the browser restores the position onto what
|
|
132
|
+
* that entry renders.
|
|
100
133
|
*
|
|
101
134
|
* The new tree renders at the lane `useDeferredValue` gives it, not in a
|
|
102
135
|
* transition. While any async action is pending, React holds every
|
|
@@ -121,8 +154,11 @@ function useInterceptedNavigation(handler) {
|
|
|
121
154
|
const count = useRef(0);
|
|
122
155
|
const [applied, setApplied] = useState(-1);
|
|
123
156
|
const onScreen = useDeferredValue(applied);
|
|
157
|
+
const [traversed, setTraversed] = useState(-1);
|
|
158
|
+
const traversedOnScreen = useDeferredValue(traversed);
|
|
124
159
|
const shown = useRef(null);
|
|
125
160
|
const latest = useRef(-1);
|
|
161
|
+
const onScreenEntry = useRef(null);
|
|
126
162
|
useEffect(() => {
|
|
127
163
|
shown.current = normalizePathname(location.pathname);
|
|
128
164
|
const onNavigate = (event) => {
|
|
@@ -130,14 +166,44 @@ function useInterceptedNavigation(handler) {
|
|
|
130
166
|
const url = new URL(event.destination.url);
|
|
131
167
|
const pathname = normalizePathname(url.pathname);
|
|
132
168
|
const inPlace = pathname === shown.current;
|
|
169
|
+
const traverse = event.navigationType === "traverse";
|
|
133
170
|
if (inPlace && !refresh(url)) {
|
|
171
|
+
const calledOff = event.destination.key === (onScreenEntry.current ?? navigation.currentEntry?.key);
|
|
172
|
+
onScreenEntry.current = null;
|
|
173
|
+
if (!traverse || calledOff) {
|
|
174
|
+
event.intercept({
|
|
175
|
+
scroll: "manual",
|
|
176
|
+
focusReset: "manual"
|
|
177
|
+
});
|
|
178
|
+
if (calledOff) keepPosition(event.destination.key);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const id = count.current++;
|
|
182
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
183
|
+
promise.catch(() => void 0);
|
|
184
|
+
const release = holdAnchoring();
|
|
185
|
+
pending.current.set(id, {
|
|
186
|
+
resolve: () => {
|
|
187
|
+
resolve(void 0);
|
|
188
|
+
},
|
|
189
|
+
scroll: null,
|
|
190
|
+
types: [],
|
|
191
|
+
release
|
|
192
|
+
});
|
|
193
|
+
event.signal.addEventListener("abort", () => {
|
|
194
|
+
pending.current.delete(id);
|
|
195
|
+
release();
|
|
196
|
+
reject(event.signal.reason);
|
|
197
|
+
}, { once: true });
|
|
198
|
+
setTraversed(id);
|
|
134
199
|
event.intercept({
|
|
135
|
-
|
|
136
|
-
|
|
200
|
+
focusReset: "manual",
|
|
201
|
+
handler: () => promise
|
|
137
202
|
});
|
|
138
203
|
return;
|
|
139
204
|
}
|
|
140
205
|
if (!claim(url)) return;
|
|
206
|
+
onScreenEntry.current ??= navigation.currentEntry?.key ?? null;
|
|
141
207
|
const id = count.current++;
|
|
142
208
|
latest.current = id;
|
|
143
209
|
setPendingPathname(normalizePathname(withoutBase(url.pathname) ?? url.pathname));
|
|
@@ -145,9 +211,11 @@ function useInterceptedNavigation(handler) {
|
|
|
145
211
|
if (latest.current === id) setPendingPathname(null);
|
|
146
212
|
};
|
|
147
213
|
const scroll = inPlace ? null : scrollPlanFor(event.navigationType, url.hash);
|
|
214
|
+
const release = inPlace && traverse ? holdAnchoring() : void 0;
|
|
215
|
+
if (release !== void 0) event.signal.addEventListener("abort", release, { once: true });
|
|
148
216
|
event.intercept({
|
|
149
217
|
...inPlace ? { focusReset: "manual" } : {},
|
|
150
|
-
scroll:
|
|
218
|
+
scroll: traverse ? "after-transition" : "manual",
|
|
151
219
|
handler: async () => {
|
|
152
220
|
event.signal.addEventListener("abort", settled, { once: true });
|
|
153
221
|
let value;
|
|
@@ -155,6 +223,7 @@ function useInterceptedNavigation(handler) {
|
|
|
155
223
|
value = await load(url, event.signal);
|
|
156
224
|
} catch (error) {
|
|
157
225
|
settled();
|
|
226
|
+
release?.();
|
|
158
227
|
throw error;
|
|
159
228
|
}
|
|
160
229
|
if (event.signal.aborted) throw event.signal.reason;
|
|
@@ -162,7 +231,8 @@ function useInterceptedNavigation(handler) {
|
|
|
162
231
|
pending.current.set(id, {
|
|
163
232
|
resolve,
|
|
164
233
|
scroll,
|
|
165
|
-
types: inPlace ? [] : transitionTypesFor(event.navigationType)
|
|
234
|
+
types: inPlace ? [] : transitionTypesFor(event.navigationType),
|
|
235
|
+
...release === void 0 ? {} : { release }
|
|
166
236
|
});
|
|
167
237
|
event.signal.addEventListener("abort", () => {
|
|
168
238
|
pending.current.delete(id);
|
|
@@ -180,14 +250,22 @@ function useInterceptedNavigation(handler) {
|
|
|
180
250
|
navigation.removeEventListener("navigate", onNavigate);
|
|
181
251
|
};
|
|
182
252
|
}, []);
|
|
183
|
-
|
|
184
|
-
const entry = pending.current.get(
|
|
253
|
+
const settle = useEffectEvent((id) => {
|
|
254
|
+
const entry = pending.current.get(id);
|
|
185
255
|
if (entry === void 0) return;
|
|
186
|
-
pending.current.delete(
|
|
256
|
+
pending.current.delete(id);
|
|
257
|
+
onScreenEntry.current = null;
|
|
258
|
+
entry.release?.();
|
|
187
259
|
if (entry.scroll !== null) applyScroll(entry.scroll);
|
|
188
|
-
if (latest.current ===
|
|
260
|
+
if (latest.current === id) setPendingPathname(null);
|
|
189
261
|
entry.resolve();
|
|
262
|
+
});
|
|
263
|
+
useLayoutEffect(() => {
|
|
264
|
+
settle(onScreen);
|
|
190
265
|
}, [onScreen]);
|
|
266
|
+
useLayoutEffect(() => {
|
|
267
|
+
settle(traversedOnScreen);
|
|
268
|
+
}, [traversedOnScreen]);
|
|
191
269
|
useLayoutEffect(() => {
|
|
192
270
|
if (applied === onScreen) return;
|
|
193
271
|
const types = pending.current.get(applied)?.types ?? [];
|
package/dist/router.mjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use client";
|
|
2
|
+
import { normalizePathname } from "./paths.mjs";
|
|
2
3
|
import { withoutBase } from "./base.mjs";
|
|
3
4
|
import { NavigationGeneration, useInterceptedNavigation } from "./navigation.mjs";
|
|
4
5
|
import { setBoundaryOutlet } from "./define-routes.mjs";
|
|
5
6
|
import { PathnameProvider, appPathname } from "./location.mjs";
|
|
7
|
+
import { TreePathname } from "./tree-pathname.mjs";
|
|
6
8
|
import { createContext, use, useDeferredValue, useMemo, useState } from "react";
|
|
7
9
|
import { jsx } from "react/jsx-runtime";
|
|
8
10
|
//#region src/router.tsx
|
|
@@ -35,6 +37,10 @@ const matchUrl = (routes, pathname) => {
|
|
|
35
37
|
const own = withoutBase(pathname);
|
|
36
38
|
return own === null ? null : routes.match(own);
|
|
37
39
|
};
|
|
40
|
+
const appliedFor = (routes, pathname) => ({
|
|
41
|
+
match: matchUrl(routes, pathname),
|
|
42
|
+
pathname: normalizePathname(pathname)
|
|
43
|
+
});
|
|
38
44
|
/**
|
|
39
45
|
* Mounts a route table on the Navigation API: every same-origin navigation
|
|
40
46
|
* that the table claims is handled in the client, and the rest is left to the
|
|
@@ -46,13 +52,14 @@ const matchUrl = (routes, pathname) => {
|
|
|
46
52
|
* mounts.
|
|
47
53
|
*/
|
|
48
54
|
function Router({ routes }) {
|
|
49
|
-
const [latest, setLatest] = useState(() =>
|
|
55
|
+
const [latest, setLatest] = useState(() => appliedFor(routes, location.pathname));
|
|
50
56
|
const { generation } = useInterceptedNavigation({
|
|
51
57
|
claim: (url) => matchUrl(routes, url.pathname) !== null,
|
|
52
|
-
load: (url) =>
|
|
58
|
+
load: (url) => appliedFor(routes, url.pathname),
|
|
53
59
|
apply: setLatest
|
|
54
60
|
});
|
|
55
|
-
const
|
|
61
|
+
const current = useDeferredValue(latest);
|
|
62
|
+
const { match } = current;
|
|
56
63
|
const value = useMemo(() => ({
|
|
57
64
|
routes,
|
|
58
65
|
match
|
|
@@ -65,9 +72,12 @@ function Router({ routes }) {
|
|
|
65
72
|
pathname: appPathname(),
|
|
66
73
|
children: /* @__PURE__ */ jsx(NavigationGeneration, {
|
|
67
74
|
value: generation,
|
|
68
|
-
children: /* @__PURE__ */ jsx(
|
|
69
|
-
value,
|
|
70
|
-
children:
|
|
75
|
+
children: /* @__PURE__ */ jsx(TreePathname, {
|
|
76
|
+
value: current.pathname,
|
|
77
|
+
children: /* @__PURE__ */ jsx(RouterContext, {
|
|
78
|
+
value,
|
|
79
|
+
children: stack
|
|
80
|
+
})
|
|
71
81
|
})
|
|
72
82
|
})
|
|
73
83
|
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext } from "react";
|
|
3
|
+
//#region src/tree-pathname.ts
|
|
4
|
+
/**
|
|
5
|
+
* The pathname the tree a component renders in was applied for, normalized
|
|
6
|
+
* and in the URL's terms (`withBase`'s, not the table's), or `null` where no
|
|
7
|
+
* router says. Under interception the URL commits before the next tree
|
|
8
|
+
* arrives, so the two differ exactly while the tree on screen is being left.
|
|
9
|
+
* Not exported: `@k8ordo/state` reads it to keep a page state's values with
|
|
10
|
+
* the tree on screen, and `@k8ordo/framework`'s runtime provides it as
|
|
11
|
+
* `<Router>` does.
|
|
12
|
+
*/
|
|
13
|
+
const KEY = Symbol.for("@k8ordo/router/tree-pathname");
|
|
14
|
+
const TreePathname = globalThis[KEY] ??= createContext(null);
|
|
15
|
+
//#endregion
|
|
16
|
+
export { TreePathname };
|
package/docs/GUIDE.md
CHANGED
|
@@ -415,15 +415,22 @@ then, before the chunk is in.
|
|
|
415
415
|
|
|
416
416
|
**A state change is not a page change.** When only the search or the entry
|
|
417
417
|
state moved, the pathname is the one whose tree is on screen: the route tree
|
|
418
|
-
is left alone, nothing remounts, and
|
|
419
|
-
is why a search update never scrolls
|
|
420
|
-
|
|
421
|
-
`@k8ordo/state`'s `update().finished` included — settles as
|
|
422
|
-
navigation commits, with no render to wait for.
|
|
418
|
+
is left alone, nothing remounts, and focus is not disturbed. A push or a
|
|
419
|
+
replace leaves scroll alone too — this is why a search update never scrolls
|
|
420
|
+
the page back to the top — and is intercepted with no handler, so its
|
|
421
|
+
`finished` — `@k8ordo/state`'s `update().finished` included — settles as
|
|
422
|
+
soon as the navigation commits, with no render to wait for. Going back or
|
|
423
|
+
forward between two entries of the page is the exception: the browser
|
|
424
|
+
restores the position that entry was left at, once the page has rendered
|
|
425
|
+
the entry — what it renders through `useDeferredValue` included — so a
|
|
426
|
+
filtered list comes back to where it was read, and that `finished` waits for
|
|
427
|
+
the render. The comparison is against the
|
|
423
428
|
page showing, not against the address bar — interception commits the URL
|
|
424
429
|
first, so a state update issued while another page is still loading is a page
|
|
425
430
|
change: it lets that page finish arriving, and its `finished` waits for that
|
|
426
|
-
render.
|
|
431
|
+
render. Going back while another page is still loading, to the entry of the
|
|
432
|
+
page still on screen, calls that page change off: the page stays where the
|
|
433
|
+
visitor scrolled it.
|
|
427
434
|
|
|
428
435
|
**Route changes render in the background.** The new tree renders at the
|
|
429
436
|
priority `useDeferredValue` gives it, so the old page stays on screen and
|
|
@@ -436,17 +443,28 @@ tagged with `addTransitionType` — `navigation`, and one of `navigation-push`,
|
|
|
436
443
|
`navigation-replace`, `navigation-traverse` — which is what a
|
|
437
444
|
`<ViewTransition>` reads to animate a page change and nothing else (below).
|
|
438
445
|
|
|
446
|
+
**The page being left keeps its own values.** `<Router>` tells
|
|
447
|
+
`@k8ordo/state`, per tree, the pathname it was applied for, so a page state
|
|
448
|
+
in the page still on screen keeps the values its URL had as it was left,
|
|
449
|
+
while the URL already names the next page; the next page renders with its
|
|
450
|
+
own from its first render. `usePathname()` follows the URL all the same. A
|
|
451
|
+
host of your own built on `useInterceptedNavigation` tells nothing, and a
|
|
452
|
+
page state under it follows the URL.
|
|
453
|
+
|
|
439
454
|
**A new page starts at the top.** Once the new tree is on screen, the
|
|
440
455
|
viewport goes where a document load would have put it: the top, or the
|
|
441
456
|
element a `#fragment` names. Back and forward are left to the browser, which
|
|
442
|
-
restores the position it saved
|
|
443
|
-
|
|
444
|
-
back to the top.
|
|
457
|
+
restores the position it saved — between two entries of the same page too.
|
|
458
|
+
Any other state change never moves the viewport — same pathname, same
|
|
459
|
+
place — so a filter update does not scroll the reader back to the top.
|
|
445
460
|
|
|
446
461
|
**Superseded navigations abort.** A second navigation aborts the first through
|
|
447
462
|
the platform's own signal: the overtaken `finished` rejects with the abort
|
|
448
463
|
reason, and its tree never reaches the screen even if its load had already
|
|
449
|
-
come back.
|
|
464
|
+
come back. Going back or forward within a page counts too: replaced by a
|
|
465
|
+
navigation of its own before the page has rendered the entry — an effect
|
|
466
|
+
that corrects the URL, say — its `finished` rejects, and the position is not
|
|
467
|
+
restored. `load` receives that signal, so a payload fetch under the framework
|
|
450
468
|
is cancelled outright. A `React.lazy` chunk cannot be — a dynamic import takes
|
|
451
469
|
no signal — so in a client application it finishes in the background and is
|
|
452
470
|
kept for the next visit, while the page it belonged to is never shown.
|
|
@@ -549,8 +567,10 @@ A fourth, optional member, `refresh: (url) => boolean`, says whether a
|
|
|
549
567
|
navigation that keeps the pathname on screen still has to load — the page
|
|
550
568
|
showing reads what moved. Without it such a navigation is a state change and
|
|
551
569
|
loads nothing; with it, it loads and applies like a page change but as a
|
|
552
|
-
state change otherwise: no
|
|
553
|
-
|
|
570
|
+
state change otherwise: no focus reset and no transition types, and no
|
|
571
|
+
scroll but going back or forward, where the browser restores the position
|
|
572
|
+
once the page loaded again is on screen. The framework's runtime answers it
|
|
573
|
+
for a page that exports `search`.
|
|
554
574
|
|
|
555
575
|
A host renders what `apply` set through `useDeferredValue`, in the same
|
|
556
576
|
component that calls the hook. That is what renders the new page in the
|
package/docs/llms.txt
CHANGED
|
@@ -94,8 +94,9 @@ When installed via npm, these docs live under `node_modules/@k8ordo/router/docs/
|
|
|
94
94
|
uses to render server-produced trees instead; its host renders what
|
|
95
95
|
`apply` set through `useDeferredValue`; its optional `refresh(url)` says
|
|
96
96
|
whether a same-pathname navigation still has to load (the page showing reads
|
|
97
|
-
the search), which then loads in place — no
|
|
98
|
-
|
|
97
|
+
the search), which then loads in place — no focus reset or transition
|
|
98
|
+
types, and no scroll but going back or forward, where the browser restores
|
|
99
|
+
the position once the page loaded again is on screen.
|
|
99
100
|
Exported types, by group — the table: `Routes` (`{ kind, record, match }`),
|
|
100
101
|
`RoutesRecord`, `RouteNode`, `RouteComponent`, `Match`, `ErrorComponent`,
|
|
101
102
|
`ErrorProps`; patterns: `PatternOf`, `NavigablePatternOf`, `NavigablePath`,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k8ordo/router",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "The pathname axis of the URL, owned. One route table derives the types, the matching, the links, and the navigation, over the Navigation API and URLPattern.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"navigation-api",
|