@k8ordo/router 1.4.3 → 2.0.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/define-routes.d.mts +10 -8
- package/dist/define-routes.mjs +12 -27
- package/dist/index.d.mts +4 -2
- package/dist/index.mjs +5 -3
- package/dist/left-at.d.mts +7 -0
- package/dist/left-at.mjs +31 -0
- package/dist/location.d.mts +3 -3
- package/dist/location.mjs +3 -3
- package/dist/match.mjs +2 -9
- package/dist/navigation.d.mts +6 -18
- package/dist/navigation.mjs +85 -92
- package/dist/outlet.d.mts +10 -0
- package/dist/outlet.mjs +27 -0
- package/dist/paths.d.mts +2 -0
- package/dist/paths.mjs +9 -1
- package/dist/router.d.mts +1 -3
- package/dist/router.mjs +3 -26
- package/dist/tree-pathname.mjs +3 -3
- package/docs/GUIDE.md +13 -7
- package/docs/llms.txt +4 -4
- package/package.json +1 -1
package/dist/define-routes.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ErrorComponent } from "./boundary.mjs";
|
|
2
2
|
import { Join, PathMatching } from "./paths.mjs";
|
|
3
|
-
import { ComponentType
|
|
3
|
+
import { ComponentType } from "react";
|
|
4
4
|
/**
|
|
5
5
|
* Any component, whatever props it declares. The table is written once and
|
|
6
6
|
* rendered by different renderers — a client app renders leaves with no
|
|
@@ -57,14 +57,16 @@ export type Routes<R extends RoutesRecord = RoutesRecord> = {
|
|
|
57
57
|
* operation that needs the table's value: links and navigation build from
|
|
58
58
|
* the pattern string alone (`href` / `navigateTo`), so only `<Router>`
|
|
59
59
|
* ever holds this object.
|
|
60
|
-
*
|
|
61
|
-
* `accept` lets the caller decline a match, in which case the walk goes
|
|
62
|
-
* on to the next pattern as if this one had not fit — a param a schema
|
|
63
|
-
* refuses is a pathname the pattern does not answer, and what answers it
|
|
64
|
-
* instead is whatever the table declares next, the catch-all included.
|
|
65
60
|
*/
|
|
66
|
-
match: (pathname: string
|
|
61
|
+
match: (pathname: string) => Match | null;
|
|
62
|
+
/**
|
|
63
|
+
* Every pattern that fits a pathname, in declaration order, matched as the
|
|
64
|
+
* iteration reaches it. `match` is its first. A caller that can still turn
|
|
65
|
+
* a fit down — a param a schema refuses is a pathname the pattern does not
|
|
66
|
+
* answer — walks on to whatever the table declares next, the catch-all
|
|
67
|
+
* included.
|
|
68
|
+
*/
|
|
69
|
+
matchAll: (pathname: string) => IteratorObject<Match, undefined>;
|
|
67
70
|
};
|
|
68
|
-
export declare const setBoundaryOutlet: (render: () => ReactNode) => void;
|
|
69
71
|
export declare function defineRoutes<R extends RoutesRecord>(record: R): Routes<R>;
|
|
70
72
|
export {};
|
package/dist/define-routes.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { isGroupKey, joinPattern, normalizePathname } from "./paths.mjs";
|
|
1
|
+
import { decodeOrKeep, isGroupKey, joinPattern, normalizePathname } from "./paths.mjs";
|
|
2
2
|
import { RouteErrorBoundary } from "./boundary.mjs";
|
|
3
|
+
import { Outlet } from "./outlet.mjs";
|
|
3
4
|
import { Suspense, createElement } from "react";
|
|
4
5
|
// src/define-routes.ts
|
|
5
6
|
const isBranch = (node) => typeof node === "object" && "children" in node;
|
|
@@ -11,7 +12,7 @@ const isBranch = (node) => typeof node === "object" && "children" in node;
|
|
|
11
12
|
* `children`, and the boundary leaves the hole to the next element.
|
|
12
13
|
*/
|
|
13
14
|
const boundaryFor = (fallback) => {
|
|
14
|
-
const Boundary = ({ children }) => createElement(RouteErrorBoundary, { fallback }, children === void 0 ?
|
|
15
|
+
const Boundary = ({ children }) => createElement(RouteErrorBoundary, { fallback }, children === void 0 ? createElement(Outlet) : children);
|
|
15
16
|
return Boundary;
|
|
16
17
|
};
|
|
17
18
|
/**
|
|
@@ -23,25 +24,9 @@ const boundaryFor = (fallback) => {
|
|
|
23
24
|
* and when a navigation mounts it anew.
|
|
24
25
|
*/
|
|
25
26
|
const loadingFor = (Fallback) => {
|
|
26
|
-
const Loading = ({ children }) => createElement(Suspense, { fallback: createElement(Fallback) }, children === void 0 ?
|
|
27
|
+
const Loading = ({ children }) => createElement(Suspense, { fallback: createElement(Fallback) }, children === void 0 ? createElement(Outlet) : children);
|
|
27
28
|
return Loading;
|
|
28
29
|
};
|
|
29
|
-
const decode = (value) => {
|
|
30
|
-
try {
|
|
31
|
-
return decodeURIComponent(value);
|
|
32
|
-
} catch {
|
|
33
|
-
return value;
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
/**
|
|
37
|
-
* What a boundary renders when a renderer hands it no children. Set once by
|
|
38
|
-
* the client router, which is the one renderer that nests by context rather
|
|
39
|
-
* than by prop; the framework always passes `children`.
|
|
40
|
-
*/
|
|
41
|
-
let outlet = () => null;
|
|
42
|
-
const setBoundaryOutlet = (render) => {
|
|
43
|
-
outlet = render;
|
|
44
|
-
};
|
|
45
30
|
function defineRoutes(record) {
|
|
46
31
|
const entries = [];
|
|
47
32
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -71,26 +56,26 @@ function defineRoutes(record) {
|
|
|
71
56
|
}
|
|
72
57
|
};
|
|
73
58
|
walk(record, "", []);
|
|
74
|
-
|
|
59
|
+
function* matchAll(pathname) {
|
|
75
60
|
const normalized = normalizePathname(pathname);
|
|
76
61
|
for (const entry of entries) {
|
|
77
62
|
const result = entry.matcher.exec({ pathname: normalized });
|
|
78
63
|
if (result === null) continue;
|
|
79
64
|
const params = {};
|
|
80
|
-
for (const [name, value] of Object.entries(result.pathname.groups)) if (!/^\d+$/u.test(name) && value !== void 0) params[name] =
|
|
81
|
-
|
|
65
|
+
for (const [name, value] of Object.entries(result.pathname.groups)) if (!/^\d+$/u.test(name) && value !== void 0) params[name] = decodeOrKeep(value);
|
|
66
|
+
yield {
|
|
82
67
|
pattern: entry.pattern,
|
|
83
68
|
params,
|
|
84
69
|
stack: entry.stack
|
|
85
70
|
};
|
|
86
|
-
if (accept === void 0 || accept(found)) return found;
|
|
87
71
|
}
|
|
88
|
-
|
|
89
|
-
|
|
72
|
+
}
|
|
73
|
+
const match = (pathname) => matchAll(pathname).next().value ?? null;
|
|
90
74
|
return {
|
|
91
75
|
kind: "routes",
|
|
92
76
|
record,
|
|
93
|
-
match
|
|
77
|
+
match,
|
|
78
|
+
matchAll
|
|
94
79
|
};
|
|
95
80
|
}
|
|
96
|
-
export { defineRoutes
|
|
81
|
+
export { defineRoutes };
|
package/dist/index.d.mts
CHANGED
|
@@ -8,5 +8,7 @@ import { BrowserPathname, PathnameProvider, usePathname } from "./location.mjs";
|
|
|
8
8
|
import { MatchOptions, MatchablePattern, matchPath, useMatch } from "./match.mjs";
|
|
9
9
|
import { isNotFound, notFound } from "./not-found.mjs";
|
|
10
10
|
import { NavigationGeneration, NavigationHandler, useInterceptedNavigation, usePendingPathname } from "./navigation.mjs";
|
|
11
|
-
import { Outlet
|
|
12
|
-
|
|
11
|
+
import { Outlet } from "./outlet.mjs";
|
|
12
|
+
import { Router, useParams, useRoute } from "./router.mjs";
|
|
13
|
+
import { TreePathname } from "./tree-pathname.mjs";
|
|
14
|
+
export { type BoundLinks, type BoundParams, BrowserPathname, type ErrorComponent, type ErrorProps, type LayoutProps, type Match, type MatchOptions, type MatchablePattern, type NavigablePath, type NavigablePatternOf, type NavigateToOptions, NavigationGeneration, type NavigationHandler, Outlet, type PageProps, type ParamValue, type ParamsOf, type ParamsSchemaFor, type ParsedParams, type ParsedParamsMap, type PathFor, PathnameProvider, type PatternOf, type Register, type RegisteredNavigablePattern, type RegisteredPageParams, type RegisteredParams, type RegisteredPattern, type RouteComponent, type RouteContext, type RouteNode, Router, type Routes, type RoutesRecord, type SchemaOutput, type StandardSchemaLike, TreePathname, bindParams, defineRoutes, href, isNotFound, matchPath, navigateTo, normalizePathname, notFound, useInterceptedNavigation, useMatch, useParams, usePathname, usePendingPathname, useRoute, withBase, withoutBase };
|
package/dist/index.mjs
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { normalizePathname } from "./paths.mjs";
|
|
2
2
|
import { withBase, withoutBase } from "./base.mjs";
|
|
3
|
+
import { BrowserPathname, PathnameProvider, usePathname } from "./location.mjs";
|
|
3
4
|
import { NavigationGeneration, useInterceptedNavigation, usePendingPathname } from "./navigation.mjs";
|
|
5
|
+
import { Outlet } from "./outlet.mjs";
|
|
4
6
|
import { defineRoutes } from "./define-routes.mjs";
|
|
5
7
|
import { bindParams, href, navigateTo } from "./links.mjs";
|
|
6
|
-
import { BrowserPathname, PathnameProvider, usePathname } from "./location.mjs";
|
|
7
8
|
import { matchPath, useMatch } from "./match.mjs";
|
|
8
9
|
import { isNotFound, notFound } from "./not-found.mjs";
|
|
9
|
-
import {
|
|
10
|
-
|
|
10
|
+
import { TreePathname } from "./tree-pathname.mjs";
|
|
11
|
+
import { Router, useParams, useRoute } from "./router.mjs";
|
|
12
|
+
export { BrowserPathname, NavigationGeneration, Outlet, PathnameProvider, Router, TreePathname, bindParams, defineRoutes, href, isNotFound, matchPath, navigateTo, normalizePathname, notFound, useInterceptedNavigation, useMatch, useParams, usePathname, usePendingPathname, useRoute, withBase, withoutBase };
|
package/dist/left-at.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/left-at.ts
|
|
2
|
+
const positions = /* @__PURE__ */ new Map();
|
|
3
|
+
const STORAGE_KEY = "@k8ordo/router:left-at";
|
|
4
|
+
const LIMIT = 100;
|
|
5
|
+
const restoreLeftAt = () => {
|
|
6
|
+
try {
|
|
7
|
+
const saved = sessionStorage.getItem(STORAGE_KEY);
|
|
8
|
+
if (saved === null) return;
|
|
9
|
+
for (const [entry, position] of JSON.parse(saved)) positions.set(entry, position);
|
|
10
|
+
} catch {}
|
|
11
|
+
};
|
|
12
|
+
const leftAt = (entry) => positions.get(entry);
|
|
13
|
+
const rememberLeftAt = (entry) => {
|
|
14
|
+
positions.delete(entry);
|
|
15
|
+
positions.set(entry, {
|
|
16
|
+
left: window.scrollX,
|
|
17
|
+
top: window.scrollY
|
|
18
|
+
});
|
|
19
|
+
if (positions.size > LIMIT) {
|
|
20
|
+
const listed = new Set(navigation.entries().map(({ key }) => key));
|
|
21
|
+
const keys = [...positions.keys()];
|
|
22
|
+
for (const key of [...keys.filter((k) => !listed.has(k)), ...keys]) {
|
|
23
|
+
if (positions.size <= LIMIT) break;
|
|
24
|
+
positions.delete(key);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
sessionStorage.setItem(STORAGE_KEY, JSON.stringify([...positions]));
|
|
29
|
+
} catch {}
|
|
30
|
+
};
|
|
31
|
+
export { leftAt, rememberLeftAt, restoreLeftAt };
|
package/dist/location.d.mts
CHANGED
|
@@ -22,11 +22,11 @@ export declare const BrowserPathname: ({ children }: {
|
|
|
22
22
|
children: ReactNode;
|
|
23
23
|
}) => ReactNode;
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
26
|
-
* document outside its own base is a deployment that went wrong, and the
|
|
25
|
+
* A pathname of the document's in the table's terms — Vite's `base` removed.
|
|
26
|
+
* A document outside its own base is a deployment that went wrong, and the
|
|
27
27
|
* pathname is shown as it is rather than failing the render over it.
|
|
28
28
|
*/
|
|
29
|
-
export declare const appPathname: () => string;
|
|
29
|
+
export declare const appPathname: (pathname?: string) => string;
|
|
30
30
|
/**
|
|
31
31
|
* Where the browser currently is, as a pathname in the table's terms — the
|
|
32
32
|
* one axis this package owns, with Vite's `base` removed so it compares with
|
package/dist/location.mjs
CHANGED
|
@@ -43,11 +43,11 @@ const subscribe = (onChange) => {
|
|
|
43
43
|
};
|
|
44
44
|
};
|
|
45
45
|
/**
|
|
46
|
-
*
|
|
47
|
-
* document outside its own base is a deployment that went wrong, and the
|
|
46
|
+
* A pathname of the document's in the table's terms — Vite's `base` removed.
|
|
47
|
+
* A document outside its own base is a deployment that went wrong, and the
|
|
48
48
|
* pathname is shown as it is rather than failing the render over it.
|
|
49
49
|
*/
|
|
50
|
-
const appPathname = () => normalizePathname(withoutBase(
|
|
50
|
+
const appPathname = (pathname = location.pathname) => normalizePathname(withoutBase(pathname) ?? pathname);
|
|
51
51
|
/**
|
|
52
52
|
* Where the browser currently is, as a pathname in the table's terms — the
|
|
53
53
|
* one axis this package owns, with Vite's `base` removed so it compares with
|
package/dist/match.mjs
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { normalizePathname } from "./paths.mjs";
|
|
2
|
+
import { decodeOrKeep, normalizePathname } from "./paths.mjs";
|
|
3
3
|
import { usePathname } from "./location.mjs";
|
|
4
4
|
// src/match.ts
|
|
5
|
-
const decode = (value) => {
|
|
6
|
-
try {
|
|
7
|
-
return decodeURIComponent(value);
|
|
8
|
-
} catch {
|
|
9
|
-
return value;
|
|
10
|
-
}
|
|
11
|
-
};
|
|
12
5
|
/**
|
|
13
6
|
* Whether a pathname is the one a pattern names, and with which params. Pure
|
|
14
7
|
* and table-free — the pattern is checked against `Register`, the pathname
|
|
@@ -24,7 +17,7 @@ const matchPath = (pattern, pathname, options) => {
|
|
|
24
17
|
const result = new URLPattern({ pathname: pattern }).exec({ pathname: normalizePathname(pathname) });
|
|
25
18
|
if (result === null) return null;
|
|
26
19
|
const params = {};
|
|
27
|
-
for (const [name, value] of Object.entries(result.pathname.groups)) if (!/^\d+$/u.test(name) && value !== void 0) params[name] =
|
|
20
|
+
for (const [name, value] of Object.entries(result.pathname.groups)) if (!/^\d+$/u.test(name) && value !== void 0) params[name] = decodeOrKeep(value);
|
|
28
21
|
return params;
|
|
29
22
|
};
|
|
30
23
|
/**
|
package/dist/navigation.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Position } from "./left-at.mjs";
|
|
1
2
|
/**
|
|
2
3
|
* What the decision below reads. Spelled out rather than taking the event,
|
|
3
4
|
* because a `NavigateEvent` cannot be constructed in a test — its destination
|
|
@@ -45,18 +46,7 @@ export type NavigationHandler<T> = {
|
|
|
45
46
|
*/
|
|
46
47
|
refresh?: (url: URL) => boolean;
|
|
47
48
|
};
|
|
48
|
-
|
|
49
|
-
readonly left: number;
|
|
50
|
-
readonly top: number;
|
|
51
|
-
};
|
|
52
|
-
/**
|
|
53
|
-
* Where the viewport goes once the new page is on screen. Going back or
|
|
54
|
-
* forward, it is where the visitor left that entry; otherwise a fragment
|
|
55
|
-
* scrolls to its target, and anything else starts at the top, the way a
|
|
56
|
-
* document load does — as does a traversal to an entry no page was ever
|
|
57
|
-
* left from, one called off while it loaded.
|
|
58
|
-
*/
|
|
59
|
-
export type ScrollPlan = {
|
|
49
|
+
type ScrollPlan = {
|
|
60
50
|
kind: 'top';
|
|
61
51
|
} | {
|
|
62
52
|
kind: 'fragment';
|
|
@@ -64,7 +54,7 @@ export type ScrollPlan = {
|
|
|
64
54
|
} | ({
|
|
65
55
|
kind: 'position';
|
|
66
56
|
} & Position);
|
|
67
|
-
export declare const scrollPlanFor: (navigationType: NavigationType, hash: string,
|
|
57
|
+
export declare const scrollPlanFor: (navigationType: NavigationType, hash: string, left: Position | undefined) => ScrollPlan;
|
|
68
58
|
/**
|
|
69
59
|
* What a `<ViewTransition>` in the application learns about the navigation
|
|
70
60
|
* that changed the tree: `navigation` names any page change, and the second
|
|
@@ -100,10 +90,7 @@ export declare const NavigationGeneration: import("react").Context<number>;
|
|
|
100
90
|
* is what makes `navigation.navigate().finished` mean "the page is showing".
|
|
101
91
|
* A navigation that keeps the pathname on screen — `@k8ordo/state`'s
|
|
102
92
|
* `update()` — is intercepted with no handler, so its `finished` settles as
|
|
103
|
-
* soon as the navigation commits.
|
|
104
|
-
* of that page is the exception: it settles with the render the entry change
|
|
105
|
-
* starts in the background, and the position that entry was left at comes
|
|
106
|
-
* back onto what it renders.
|
|
93
|
+
* soon as the navigation commits.
|
|
107
94
|
*
|
|
108
95
|
* The new tree renders at the lane `useDeferredValue` gives it, not in a
|
|
109
96
|
* transition. While any async action is pending, React holds every
|
|
@@ -119,4 +106,5 @@ export declare const NavigationGeneration: import("react").Context<number>;
|
|
|
119
106
|
*/
|
|
120
107
|
export declare function useInterceptedNavigation<T>(handler: NavigationHandler<T>): {
|
|
121
108
|
readonly generation: number;
|
|
122
|
-
};
|
|
109
|
+
};
|
|
110
|
+
export {};
|
package/dist/navigation.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { normalizePathname } from "./paths.mjs";
|
|
3
|
-
import {
|
|
2
|
+
import { decodeOrKeep, normalizePathname } from "./paths.mjs";
|
|
3
|
+
import { leftAt, rememberLeftAt, restoreLeftAt } from "./left-at.mjs";
|
|
4
|
+
import { appPathname } from "./location.mjs";
|
|
4
5
|
import { addTransitionType, createContext, startTransition, useDeferredValue, useEffect, useEffectEvent, useLayoutEffect, useRef, useState, useSyncExternalStore } from "react";
|
|
5
6
|
// src/navigation.ts
|
|
6
7
|
/**
|
|
@@ -13,21 +14,15 @@ import { addTransitionType, createContext, startTransition, useDeferredValue, us
|
|
|
13
14
|
* still come through here.
|
|
14
15
|
*/
|
|
15
16
|
const isOurs = (event) => event.canIntercept && !event.hashChange && event.downloadRequest === null && event.navigationType !== "reload" && event.formData === null;
|
|
16
|
-
const scrollPlanFor = (navigationType, hash,
|
|
17
|
-
if (navigationType === "traverse" &&
|
|
17
|
+
const scrollPlanFor = (navigationType, hash, left) => {
|
|
18
|
+
if (navigationType === "traverse" && left !== void 0) return {
|
|
18
19
|
kind: "position",
|
|
19
|
-
...
|
|
20
|
+
...left
|
|
21
|
+
};
|
|
22
|
+
if (hash.length > 1) return {
|
|
23
|
+
kind: "fragment",
|
|
24
|
+
id: decodeOrKeep(hash.slice(1))
|
|
20
25
|
};
|
|
21
|
-
if (hash.length > 1) {
|
|
22
|
-
let id = hash.slice(1);
|
|
23
|
-
try {
|
|
24
|
-
id = decodeURIComponent(id);
|
|
25
|
-
} catch {}
|
|
26
|
-
return {
|
|
27
|
-
kind: "fragment",
|
|
28
|
-
id
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
26
|
return { kind: "top" };
|
|
32
27
|
};
|
|
33
28
|
/**
|
|
@@ -61,34 +56,6 @@ const applyScroll = (plan) => {
|
|
|
61
56
|
top: -1
|
|
62
57
|
});
|
|
63
58
|
};
|
|
64
|
-
const leftAt = /* @__PURE__ */ new Map();
|
|
65
|
-
const LEFT_AT_KEY = "@k8ordo/router:left-at";
|
|
66
|
-
const LEFT_AT_LIMIT = 100;
|
|
67
|
-
const readLeftAt = () => {
|
|
68
|
-
try {
|
|
69
|
-
const saved = sessionStorage.getItem(LEFT_AT_KEY);
|
|
70
|
-
if (saved === null) return;
|
|
71
|
-
for (const [key, position] of JSON.parse(saved)) leftAt.set(key, position);
|
|
72
|
-
} catch {}
|
|
73
|
-
};
|
|
74
|
-
const rememberLeftAt = (entry) => {
|
|
75
|
-
leftAt.delete(entry);
|
|
76
|
-
leftAt.set(entry, {
|
|
77
|
-
left: window.scrollX,
|
|
78
|
-
top: window.scrollY
|
|
79
|
-
});
|
|
80
|
-
if (leftAt.size > LEFT_AT_LIMIT) {
|
|
81
|
-
const listed = new Set(navigation.entries().map(({ key }) => key));
|
|
82
|
-
const keys = [...leftAt.keys()];
|
|
83
|
-
for (const key of [...keys.filter((k) => !listed.has(k)), ...keys]) {
|
|
84
|
-
if (leftAt.size <= LEFT_AT_LIMIT) break;
|
|
85
|
-
leftAt.delete(key);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
try {
|
|
89
|
-
sessionStorage.setItem(LEFT_AT_KEY, JSON.stringify([...leftAt]));
|
|
90
|
-
} catch {}
|
|
91
|
-
};
|
|
92
59
|
const keepPosition = (entry) => {
|
|
93
60
|
const left = window.scrollX;
|
|
94
61
|
const top = window.scrollY;
|
|
@@ -100,6 +67,33 @@ const keepPosition = (entry) => {
|
|
|
100
67
|
});
|
|
101
68
|
}, 0);
|
|
102
69
|
};
|
|
70
|
+
const reloadOnUnannouncedSteps = (followsInPlace) => {
|
|
71
|
+
let reloading = false;
|
|
72
|
+
const reload = () => {
|
|
73
|
+
reloading = true;
|
|
74
|
+
location.reload();
|
|
75
|
+
};
|
|
76
|
+
let announced = null;
|
|
77
|
+
if (navigation.currentEntry === null && navigation.entries().length > 0) reload();
|
|
78
|
+
const onPopState = () => {
|
|
79
|
+
if (reloading || navigation.currentEntry === null) {
|
|
80
|
+
reload();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (announced === location.href) return;
|
|
84
|
+
if (!followsInPlace(new URL(location.href))) reload();
|
|
85
|
+
};
|
|
86
|
+
window.addEventListener("popstate", onPopState);
|
|
87
|
+
return {
|
|
88
|
+
heard: (destination) => {
|
|
89
|
+
announced = destination;
|
|
90
|
+
},
|
|
91
|
+
reloading: () => reloading,
|
|
92
|
+
stop: () => {
|
|
93
|
+
window.removeEventListener("popstate", onPopState);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
};
|
|
103
97
|
/**
|
|
104
98
|
* The pathname a page change is loading, in the table's terms — `null` when
|
|
105
99
|
* none is. One per document, like the navigation it follows: there is one
|
|
@@ -146,10 +140,7 @@ const NavigationGeneration = createContext(-1);
|
|
|
146
140
|
* is what makes `navigation.navigate().finished` mean "the page is showing".
|
|
147
141
|
* A navigation that keeps the pathname on screen — `@k8ordo/state`'s
|
|
148
142
|
* `update()` — is intercepted with no handler, so its `finished` settles as
|
|
149
|
-
* soon as the navigation commits.
|
|
150
|
-
* of that page is the exception: it settles with the render the entry change
|
|
151
|
-
* starts in the background, and the position that entry was left at comes
|
|
152
|
-
* back onto what it renders.
|
|
143
|
+
* soon as the navigation commits.
|
|
153
144
|
*
|
|
154
145
|
* The new tree renders at the lane `useDeferredValue` gives it, not in a
|
|
155
146
|
* transition. While any async action is pending, React holds every
|
|
@@ -178,22 +169,44 @@ function useInterceptedNavigation(handler) {
|
|
|
178
169
|
const traversedOnScreen = useDeferredValue(traversed);
|
|
179
170
|
const shown = useRef(null);
|
|
180
171
|
const latest = useRef(-1);
|
|
172
|
+
const applying = useRef(null);
|
|
181
173
|
const onScreenEntry = useRef(null);
|
|
182
174
|
useEffect(() => {
|
|
183
175
|
shown.current = normalizePathname(location.pathname);
|
|
184
|
-
|
|
176
|
+
restoreLeftAt();
|
|
177
|
+
const steps = reloadOnUnannouncedSteps((url) => normalizePathname(url.pathname) === shown.current && pendingPathname === null && !refresh(url));
|
|
178
|
+
const track = (id, entry, signal) => {
|
|
179
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
180
|
+
pending.current.set(id, {
|
|
181
|
+
...entry,
|
|
182
|
+
resolve: () => {
|
|
183
|
+
resolve(void 0);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
signal.addEventListener("abort", () => {
|
|
187
|
+
pending.current.delete(id);
|
|
188
|
+
resolve(void 0);
|
|
189
|
+
}, { once: true });
|
|
190
|
+
return promise;
|
|
191
|
+
};
|
|
185
192
|
const onNavigate = (event) => {
|
|
193
|
+
steps.heard(event.destination.url);
|
|
194
|
+
if (steps.reloading()) return;
|
|
186
195
|
const traverse = event.navigationType === "traverse";
|
|
187
|
-
|
|
196
|
+
const { key } = event.destination;
|
|
197
|
+
if (traverse && isOurs(event) && key !== "" && key === (onScreenEntry.current ?? navigation.currentEntry?.key) && applying.current === null) {
|
|
198
|
+
const calledOff = onScreenEntry.current !== null;
|
|
199
|
+
onScreenEntry.current = null;
|
|
188
200
|
event.intercept({
|
|
189
201
|
scroll: "manual",
|
|
190
202
|
focusReset: "manual"
|
|
191
203
|
});
|
|
204
|
+
if (calledOff) keepPosition(key);
|
|
192
205
|
return;
|
|
193
206
|
}
|
|
194
207
|
const leaving = onScreenEntry.current ?? navigation.currentEntry?.key;
|
|
195
208
|
if (leaving !== void 0 && leaving !== "") rememberLeftAt(leaving);
|
|
196
|
-
const keyless = traverse &&
|
|
209
|
+
const keyless = traverse && key === "";
|
|
197
210
|
if (!isOurs(event)) {
|
|
198
211
|
if (keyless) onScreenEntry.current = "";
|
|
199
212
|
else if (onScreenEntry.current === "") onScreenEntry.current = null;
|
|
@@ -202,37 +215,24 @@ function useInterceptedNavigation(handler) {
|
|
|
202
215
|
const url = new URL(event.destination.url);
|
|
203
216
|
const pathname = normalizePathname(url.pathname);
|
|
204
217
|
const inPlace = pathname === shown.current;
|
|
218
|
+
const plan = keyless || inPlace && !traverse ? null : scrollPlanFor(event.navigationType, url.hash, leftAt(key));
|
|
219
|
+
const scroll = keyless ? "after-transition" : "manual";
|
|
205
220
|
if (inPlace && !refresh(url)) {
|
|
206
|
-
const calledOff = event.destination.key !== "" && event.destination.key === onScreenEntry.current;
|
|
207
221
|
onScreenEntry.current = keyless ? "" : null;
|
|
208
|
-
if (!traverse
|
|
222
|
+
if (!traverse) {
|
|
209
223
|
event.intercept({
|
|
210
224
|
scroll: "manual",
|
|
211
225
|
focusReset: "manual"
|
|
212
226
|
});
|
|
213
|
-
if (calledOff) keepPosition(event.destination.key);
|
|
214
227
|
return;
|
|
215
228
|
}
|
|
216
229
|
const id = count.current++;
|
|
217
|
-
const
|
|
218
|
-
promise.catch(() => void 0);
|
|
219
|
-
pending.current.set(id, {
|
|
220
|
-
resolve: () => {
|
|
221
|
-
resolve(void 0);
|
|
222
|
-
},
|
|
223
|
-
scroll: keyless ? null : scrollPlanFor("traverse", url.hash, leftAt.get(event.destination.key)),
|
|
224
|
-
types: [],
|
|
225
|
-
keyless
|
|
226
|
-
});
|
|
227
|
-
event.signal.addEventListener("abort", () => {
|
|
228
|
-
pending.current.delete(id);
|
|
229
|
-
reject(event.signal.reason);
|
|
230
|
-
}, { once: true });
|
|
230
|
+
const arrived = track(id, { scroll: plan }, event.signal);
|
|
231
231
|
setTraversed(id);
|
|
232
232
|
event.intercept({
|
|
233
|
-
scroll
|
|
233
|
+
scroll,
|
|
234
234
|
focusReset: "manual",
|
|
235
|
-
handler: () =>
|
|
235
|
+
handler: () => arrived
|
|
236
236
|
});
|
|
237
237
|
return;
|
|
238
238
|
}
|
|
@@ -240,14 +240,13 @@ function useInterceptedNavigation(handler) {
|
|
|
240
240
|
onScreenEntry.current ??= navigation.currentEntry?.key ?? null;
|
|
241
241
|
const id = count.current++;
|
|
242
242
|
latest.current = id;
|
|
243
|
-
setPendingPathname(
|
|
243
|
+
setPendingPathname(appPathname(url.pathname));
|
|
244
244
|
const settled = () => {
|
|
245
245
|
if (latest.current === id) setPendingPathname(null);
|
|
246
246
|
};
|
|
247
|
-
const scroll = inPlace && !traverse || keyless ? null : scrollPlanFor(event.navigationType, url.hash, leftAt.get(event.destination.key));
|
|
248
247
|
event.intercept({
|
|
249
248
|
...inPlace ? { focusReset: "manual" } : {},
|
|
250
|
-
scroll
|
|
249
|
+
scroll,
|
|
251
250
|
handler: async () => {
|
|
252
251
|
event.signal.addEventListener("abort", settled, { once: true });
|
|
253
252
|
let value;
|
|
@@ -258,39 +257,33 @@ function useInterceptedNavigation(handler) {
|
|
|
258
257
|
throw error;
|
|
259
258
|
}
|
|
260
259
|
if (event.signal.aborted) throw event.signal.reason;
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
scroll,
|
|
260
|
+
const committed = track(id, {
|
|
261
|
+
scroll: plan,
|
|
262
|
+
change: {
|
|
265
263
|
types: inPlace ? [] : transitionTypesFor(event.navigationType),
|
|
266
264
|
keyless
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
setApplied(id);
|
|
275
|
-
});
|
|
265
|
+
}
|
|
266
|
+
}, event.signal);
|
|
267
|
+
shown.current = pathname;
|
|
268
|
+
applying.current = id;
|
|
269
|
+
apply(value);
|
|
270
|
+
setApplied(id);
|
|
271
|
+
await committed;
|
|
276
272
|
}
|
|
277
273
|
});
|
|
278
274
|
};
|
|
279
275
|
navigation.addEventListener("navigate", onNavigate);
|
|
280
|
-
const onPopState = () => {
|
|
281
|
-
if (navigation.currentEntry === null) location.reload();
|
|
282
|
-
};
|
|
283
|
-
window.addEventListener("popstate", onPopState);
|
|
284
276
|
return () => {
|
|
285
277
|
navigation.removeEventListener("navigate", onNavigate);
|
|
286
|
-
|
|
278
|
+
steps.stop();
|
|
287
279
|
};
|
|
288
280
|
}, []);
|
|
289
281
|
const settle = useEffectEvent((id) => {
|
|
282
|
+
if (applying.current === id) applying.current = null;
|
|
290
283
|
const entry = pending.current.get(id);
|
|
291
284
|
if (entry === void 0) return;
|
|
292
285
|
pending.current.delete(id);
|
|
293
|
-
onScreenEntry.current = entry.keyless ? "" : null;
|
|
286
|
+
if (entry.change !== void 0) onScreenEntry.current = entry.change.keyless ? "" : null;
|
|
294
287
|
if (entry.scroll !== null) applyScroll(entry.scroll);
|
|
295
288
|
if (latest.current === id) setPendingPathname(null);
|
|
296
289
|
entry.resolve();
|
|
@@ -303,7 +296,7 @@ function useInterceptedNavigation(handler) {
|
|
|
303
296
|
}, [traversedOnScreen]);
|
|
304
297
|
useLayoutEffect(() => {
|
|
305
298
|
if (applied === onScreen) return;
|
|
306
|
-
const types = pending.current.get(applied)?.types ?? [];
|
|
299
|
+
const types = pending.current.get(applied)?.change?.types ?? [];
|
|
307
300
|
startTransition(() => {
|
|
308
301
|
for (const type of types) addTransitionType(type);
|
|
309
302
|
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RouteComponent } from "./define-routes.mjs";
|
|
2
|
+
import { FC } from "react";
|
|
3
|
+
type StackValue = {
|
|
4
|
+
stack: readonly RouteComponent[];
|
|
5
|
+
index: number;
|
|
6
|
+
};
|
|
7
|
+
export declare const RenderStack: FC<StackValue>;
|
|
8
|
+
/** Renders the next element of the matched stack — a layout's hole. */
|
|
9
|
+
export declare const Outlet: FC;
|
|
10
|
+
export {};
|
package/dist/outlet.mjs
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, use, useMemo } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
// src/outlet.tsx
|
|
5
|
+
const StackContext = createContext(null);
|
|
6
|
+
const RenderStack = ({ stack, index }) => {
|
|
7
|
+
const value = useMemo(() => ({
|
|
8
|
+
stack,
|
|
9
|
+
index: index + 1
|
|
10
|
+
}), [stack, index]);
|
|
11
|
+
const next = stack[index];
|
|
12
|
+
if (next === void 0) return null;
|
|
13
|
+
return /* @__PURE__ */ jsx(StackContext, {
|
|
14
|
+
value,
|
|
15
|
+
children: /* @__PURE__ */ jsx(next, {})
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
/** Renders the next element of the matched stack — a layout's hole. */
|
|
19
|
+
const Outlet = () => {
|
|
20
|
+
const ctx = use(StackContext);
|
|
21
|
+
if (ctx === null) throw new Error("Outlet must render inside <Router>");
|
|
22
|
+
return /* @__PURE__ */ jsx(RenderStack, {
|
|
23
|
+
stack: ctx.stack,
|
|
24
|
+
index: ctx.index
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
export { Outlet, RenderStack };
|
package/dist/paths.d.mts
CHANGED
|
@@ -76,6 +76,8 @@ export declare const joinPattern: (prefix: string, key: string) => string;
|
|
|
76
76
|
* form, root excepted.
|
|
77
77
|
*/
|
|
78
78
|
export declare const normalizePathname: (pathname: string) => string;
|
|
79
|
+
/** A URL part decoded, or as written when it does not decode. */
|
|
80
|
+
export declare const decodeOrKeep: (value: string) => string;
|
|
79
81
|
/** What a param value may be on the way into a link: anything with one spelling. */
|
|
80
82
|
export type ParamValue = string | number | bigint | boolean;
|
|
81
83
|
export declare const buildHref: (pattern: string, params: Readonly<Record<string, unknown>> | undefined) => string;
|
package/dist/paths.mjs
CHANGED
|
@@ -11,6 +11,14 @@ const normalizePathname = (pathname) => {
|
|
|
11
11
|
while (end > 1 && pathname.charAt(end - 1) === "/") end -= 1;
|
|
12
12
|
return pathname.slice(0, end);
|
|
13
13
|
};
|
|
14
|
+
/** A URL part decoded, or as written when it does not decode. */
|
|
15
|
+
const decodeOrKeep = (value) => {
|
|
16
|
+
try {
|
|
17
|
+
return decodeURIComponent(value);
|
|
18
|
+
} catch {
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
14
22
|
const buildHref = (pattern, params) => {
|
|
15
23
|
if (pattern.includes("*")) throw new TypeError(`"${pattern}" is a wildcard — it has no href`);
|
|
16
24
|
return pattern.split("/").map((segment) => {
|
|
@@ -22,4 +30,4 @@ const buildHref = (pattern, params) => {
|
|
|
22
30
|
return encodeURIComponent(String(value));
|
|
23
31
|
}).join("/");
|
|
24
32
|
};
|
|
25
|
-
export { buildHref, isGroupKey, joinPattern, normalizePathname };
|
|
33
|
+
export { buildHref, decodeOrKeep, isGroupKey, joinPattern, normalizePathname };
|
package/dist/router.d.mts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { ParamsOf } from "./paths.mjs";
|
|
2
2
|
import { Match, Routes } from "./define-routes.mjs";
|
|
3
3
|
import { RegisteredPattern } from "./register.mjs";
|
|
4
|
-
import {
|
|
5
|
-
/** Renders the next element of the matched stack — a layout's hole. */
|
|
6
|
-
export declare const Outlet: FC;
|
|
4
|
+
import { ReactNode } from "react";
|
|
7
5
|
/**
|
|
8
6
|
* Mounts a route table on the Navigation API: every same-origin navigation
|
|
9
7
|
* that the table claims is handled in the client, and the rest is left to the
|
package/dist/router.mjs
CHANGED
|
@@ -1,37 +1,14 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { normalizePathname } from "./paths.mjs";
|
|
3
3
|
import { withoutBase } from "./base.mjs";
|
|
4
|
-
import { NavigationGeneration, useInterceptedNavigation } from "./navigation.mjs";
|
|
5
|
-
import { setBoundaryOutlet } from "./define-routes.mjs";
|
|
6
4
|
import { PathnameProvider, appPathname } from "./location.mjs";
|
|
5
|
+
import { NavigationGeneration, useInterceptedNavigation } from "./navigation.mjs";
|
|
6
|
+
import { RenderStack } from "./outlet.mjs";
|
|
7
7
|
import { TreePathname } from "./tree-pathname.mjs";
|
|
8
8
|
import { createContext, use, useDeferredValue, useMemo, useState } from "react";
|
|
9
9
|
import { jsx } from "react/jsx-runtime";
|
|
10
10
|
// src/router.tsx
|
|
11
11
|
const RouterContext = createContext(null);
|
|
12
|
-
const StackContext = createContext(null);
|
|
13
|
-
const RenderStack = ({ stack, index }) => {
|
|
14
|
-
const value = useMemo(() => ({
|
|
15
|
-
stack,
|
|
16
|
-
index: index + 1
|
|
17
|
-
}), [stack, index]);
|
|
18
|
-
const next = stack[index];
|
|
19
|
-
if (next === void 0) return null;
|
|
20
|
-
return /* @__PURE__ */ jsx(StackContext, {
|
|
21
|
-
value,
|
|
22
|
-
children: /* @__PURE__ */ jsx(next, {})
|
|
23
|
-
});
|
|
24
|
-
};
|
|
25
|
-
/** Renders the next element of the matched stack — a layout's hole. */
|
|
26
|
-
const Outlet = () => {
|
|
27
|
-
const ctx = use(StackContext);
|
|
28
|
-
if (ctx === null) throw new Error("Outlet must render inside <Router>");
|
|
29
|
-
return /* @__PURE__ */ jsx(RenderStack, {
|
|
30
|
-
stack: ctx.stack,
|
|
31
|
-
index: ctx.index
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
setBoundaryOutlet(() => /* @__PURE__ */ jsx(Outlet, {}));
|
|
35
12
|
/** The table's match for a URL's pathname; a URL outside the base has none. */
|
|
36
13
|
const matchUrl = (routes, pathname) => {
|
|
37
14
|
const own = withoutBase(pathname);
|
|
@@ -110,4 +87,4 @@ function useParams(pattern) {
|
|
|
110
87
|
if (current !== pattern) throw new Error(`useParams("${pattern}") rendered under "${current}"`);
|
|
111
88
|
return params;
|
|
112
89
|
}
|
|
113
|
-
export {
|
|
90
|
+
export { Router, useParams, useRoute };
|
package/dist/tree-pathname.mjs
CHANGED
|
@@ -6,9 +6,9 @@ import { createContext } from "react";
|
|
|
6
6
|
* and in the URL's terms (`withBase`'s, not the table's), or `null` where no
|
|
7
7
|
* router says. Under interception the URL commits before the next tree
|
|
8
8
|
* arrives, so the two differ exactly while the tree on screen is being left.
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
9
|
+
* `@k8ordo/framework`'s runtime provides it as `<Router>` does, and
|
|
10
|
+
* `@k8ordo/state` reads it to keep a page state's values with the tree on
|
|
11
|
+
* screen.
|
|
12
12
|
*/
|
|
13
13
|
const KEY = Symbol.for("@k8ordo/router/tree-pathname");
|
|
14
14
|
const TreePathname = globalThis[KEY] ??= createContext(null);
|
package/docs/GUIDE.md
CHANGED
|
@@ -474,9 +474,14 @@ another origin, it gives a traversal to an earlier entry no key, so the router
|
|
|
474
474
|
has no recorded position to look up and the browser restores it instead. If
|
|
475
475
|
the visitor scrolled the page still on screen while the next one loaded, the
|
|
476
476
|
browser does not restore it, and the page stays where they scrolled it. Coming
|
|
477
|
-
back from another site, WebKit does not report
|
|
478
|
-
|
|
479
|
-
|
|
477
|
+
back from another site, WebKit does not report some traversals at all — to an
|
|
478
|
+
entry from before that site, and, once the visitor has moved on, back to the
|
|
479
|
+
page they came back to, even before the next page has loaded — so the router
|
|
480
|
+
reloads the page: the browser restores the position of a page rendered on the
|
|
481
|
+
server, and a page rendered on the client starts at the top. A step within a
|
|
482
|
+
page that only moves the search is not reloaded unless `refresh(url)` says the
|
|
483
|
+
page reads it or a page is still loading. A link followed while the page
|
|
484
|
+
reloads opens as a document load.
|
|
480
485
|
|
|
481
486
|
Any other state change never moves the viewport — same pathname, same
|
|
482
487
|
place — so a filter update does not scroll the reader back to the top.
|
|
@@ -606,10 +611,11 @@ the table's `error` boundaries know when to let a failure go. `<Router>` does
|
|
|
606
611
|
this itself; the framework's runtime does too.
|
|
607
612
|
|
|
608
613
|
The table itself stays on the server, generated from `routes/`, and the
|
|
609
|
-
framework
|
|
610
|
-
|
|
611
|
-
`paramsSchema` along its stack refused
|
|
612
|
-
not fit, so the walk goes on to the next pattern, the catch-all
|
|
614
|
+
framework walks it with `matchAll(pathname)`: every pattern that fits, in
|
|
615
|
+
declaration order, where `match(pathname)` is only the first. A fit whose
|
|
616
|
+
params a `paramsSchema` along its stack refused is passed over as if the
|
|
617
|
+
pattern had not fit, so the walk goes on to the next pattern, the catch-all
|
|
618
|
+
included.
|
|
613
619
|
|
|
614
620
|
A framework `route.ts` — an answer that is not a page, a feed or JSON —
|
|
615
621
|
exports a function per request method, and `RouteContext<'/feed.xml'>` is
|
package/docs/llms.txt
CHANGED
|
@@ -26,10 +26,10 @@ When installed via npm, these docs live under `node_modules/@k8ordo/router/docs/
|
|
|
26
26
|
|
|
27
27
|
## Entry point
|
|
28
28
|
|
|
29
|
-
- `@k8ordo/router` — `defineRoutes(record)` returns a table
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
param a schema refused; `<Router routes>` mounts it on the Navigation API and
|
|
29
|
+
- `@k8ordo/router` — `defineRoutes(record)` returns a table that answers
|
|
30
|
+
`match(pathname)`, the first pattern that fits, and `matchAll(pathname)`,
|
|
31
|
+
every one in declaration order — the framework walks the second and passes
|
|
32
|
+
over a fit whose param a schema refused; `<Router routes>` mounts it on the Navigation API and
|
|
33
33
|
`<Outlet />` renders the rest of the matched stack; a branch's `error`
|
|
34
34
|
component (props `ErrorProps`, `{ error, reset }`; type `ErrorComponent`)
|
|
35
35
|
renders in the layout's hole when what is below throws; the next
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@k8ordo/router",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.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",
|