@oomfware/stacker 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -15
- package/dist/index.d.mts +49 -49
- package/dist/index.mjs +106 -81
- package/dist/testing.d.mts +3 -3
- package/dist/testing.mjs +17 -17
- package/dist/{types-kax-GwM6.d.mts → types--iOZjfC1.d.mts} +41 -41
- package/dist/{url-DerpiRjh.mjs → url-ghfWE9j6.mjs} +5 -5
- package/package.json +69 -68
package/README.md
CHANGED
|
@@ -38,17 +38,17 @@ const routes = defineRoutes({
|
|
|
38
38
|
app: layout({
|
|
39
39
|
component: AppShell,
|
|
40
40
|
children: {
|
|
41
|
-
Home: route({
|
|
41
|
+
Home: route({ path: '/', component: Home }),
|
|
42
42
|
Profile: route({
|
|
43
|
-
component: Profile,
|
|
44
|
-
params: { actor: string() },
|
|
45
43
|
path: '/profile/:actor',
|
|
44
|
+
params: { actor: string() },
|
|
45
|
+
component: Profile,
|
|
46
46
|
}),
|
|
47
47
|
},
|
|
48
48
|
}),
|
|
49
49
|
});
|
|
50
50
|
|
|
51
|
-
const router = new Router({ history: new NavigationHistory()
|
|
51
|
+
const router = new Router({ routes, history: new NavigationHistory() });
|
|
52
52
|
|
|
53
53
|
export const App = () => <RouterView router={router} />;
|
|
54
54
|
```
|
|
@@ -65,14 +65,14 @@ const routes = defineRoutes({
|
|
|
65
65
|
app: layout({
|
|
66
66
|
component: AppShell,
|
|
67
67
|
children: {
|
|
68
|
-
Home: route({
|
|
68
|
+
Home: route({ path: '/', component: Home }),
|
|
69
69
|
Profile: route({
|
|
70
|
-
|
|
70
|
+
path: '/profile/:actor',
|
|
71
71
|
params: { actor: string() },
|
|
72
72
|
query: { tab: optional(string()) },
|
|
73
|
-
|
|
73
|
+
component: Profile,
|
|
74
74
|
}),
|
|
75
|
-
Settings: route({
|
|
75
|
+
Settings: route({ path: '/settings', component: Settings }),
|
|
76
76
|
},
|
|
77
77
|
}),
|
|
78
78
|
});
|
|
@@ -85,10 +85,10 @@ configure `type` to control page instance lifetimes:
|
|
|
85
85
|
|
|
86
86
|
```ts
|
|
87
87
|
// page (default): new instance and state per history entry
|
|
88
|
-
route({
|
|
88
|
+
route({ path: '/profile/:actor', type: 'page', component: Profile });
|
|
89
89
|
|
|
90
90
|
// singleton: one instance per distinct parameter set
|
|
91
|
-
route({
|
|
91
|
+
route({ path: '/', type: 'singleton', component: Home });
|
|
92
92
|
```
|
|
93
93
|
|
|
94
94
|
### parameters
|
|
@@ -99,7 +99,6 @@ declare path and query parameters using codecs. types are inferred automatically
|
|
|
99
99
|
import { boolean, enumOf, integer, optional, route, string, withDefault } from '@oomfware/stacker';
|
|
100
100
|
|
|
101
101
|
route({
|
|
102
|
-
component: SearchPage,
|
|
103
102
|
path: '/search/:tag',
|
|
104
103
|
params: { tag: string() },
|
|
105
104
|
query: {
|
|
@@ -107,6 +106,7 @@ route({
|
|
|
107
106
|
sort: withDefault(enumOf(['new', 'top']), 'new'),
|
|
108
107
|
unread: optional(boolean()),
|
|
109
108
|
},
|
|
109
|
+
component: SearchPage,
|
|
110
110
|
});
|
|
111
111
|
```
|
|
112
112
|
|
|
@@ -117,7 +117,7 @@ end a path with `*name` to capture the rest of the URL, separators included. a s
|
|
|
117
117
|
its bare parent path, where the remainder is empty:
|
|
118
118
|
|
|
119
119
|
```ts
|
|
120
|
-
route({
|
|
120
|
+
route({ path: '/docs/*rest', params: { rest: string() }, component: Docs });
|
|
121
121
|
|
|
122
122
|
// /docs/guide/intro -> { rest: 'guide/intro' }
|
|
123
123
|
// /docs -> { rest: '' }
|
|
@@ -231,7 +231,7 @@ generate type-safe hooks bound to your route configuration using `createRouterHo
|
|
|
231
231
|
```tsx
|
|
232
232
|
import { createRouterHooks } from '@oomfware/stacker';
|
|
233
233
|
|
|
234
|
-
const {
|
|
234
|
+
const { useRouter, useParams } = createRouterHooks(routes);
|
|
235
235
|
|
|
236
236
|
const Profile = () => {
|
|
237
237
|
const [{ actor }, replace] = useParams('Profile');
|
|
@@ -373,9 +373,9 @@ parameters:
|
|
|
373
373
|
|
|
374
374
|
```ts
|
|
375
375
|
route({
|
|
376
|
-
component: Beta,
|
|
377
376
|
path: '/feed',
|
|
378
377
|
when: ({ rawSearch }) => rawSearch.has('beta'),
|
|
378
|
+
component: Beta,
|
|
379
379
|
});
|
|
380
380
|
```
|
|
381
381
|
|
|
@@ -387,7 +387,7 @@ use `MemoryHistory` to test routing behavior in environments without a DOM:
|
|
|
387
387
|
import { MemoryHistory } from '@oomfware/stacker/testing';
|
|
388
388
|
|
|
389
389
|
const router = new Router({
|
|
390
|
-
history: new MemoryHistory({ initialEntries: ['/', '/profile/alice'] }),
|
|
391
390
|
routes,
|
|
391
|
+
history: new MemoryHistory({ initialEntries: ['/', '/profile/alice'] }),
|
|
392
392
|
});
|
|
393
393
|
```
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as HistoryLocation, c as HistoryUpdate, i as HistoryListener, n as HistoryAction, o as HistoryNavigateOptions, r as HistoryEntry, s as HistoryScrollBehavior, t as History } from "./types
|
|
1
|
+
import { a as HistoryLocation, c as HistoryUpdate, i as HistoryListener, n as HistoryAction, o as HistoryNavigateOptions, r as HistoryEntry, s as HistoryScrollBehavior, t as History } from "./types--iOZjfC1.mjs";
|
|
2
2
|
import { AnchorHTMLAttributes, ComponentType, EffectCallback, LazyExoticComponent, ReactElement, ReactNode } from "react";
|
|
3
3
|
//#region src/codec.d.ts
|
|
4
4
|
/**
|
|
@@ -128,28 +128,28 @@ interface RouteMeta {}
|
|
|
128
128
|
type ReadonlyURLSearchParams = Omit<URLSearchParams, 'append' | 'delete' | 'set' | 'sort'>;
|
|
129
129
|
/** context passed to a route `when` predicate. */
|
|
130
130
|
interface WhenContext {
|
|
131
|
-
readonly hash: string;
|
|
132
131
|
readonly pathname: string;
|
|
133
|
-
readonly rawSearch: ReadonlyURLSearchParams;
|
|
134
132
|
readonly search: string;
|
|
133
|
+
readonly hash: string;
|
|
134
|
+
readonly rawSearch: ReadonlyURLSearchParams;
|
|
135
135
|
}
|
|
136
136
|
interface LeafShared<Params extends CodecRecord, Query extends CodecRecord> {
|
|
137
|
-
/** component to render. */
|
|
138
|
-
readonly component: ComponentType;
|
|
139
|
-
/** suspense fallback component. */
|
|
140
|
-
readonly fallback?: ReactNode;
|
|
141
|
-
/** application metadata. */
|
|
142
|
-
readonly meta?: RouteMeta;
|
|
143
|
-
/** path parameter codecs. */
|
|
144
|
-
readonly params?: Params;
|
|
145
137
|
/** absolute URL path pattern. */
|
|
146
138
|
readonly path: string;
|
|
139
|
+
/** path parameter codecs. */
|
|
140
|
+
readonly params?: Params;
|
|
147
141
|
/** query parameter codecs. */
|
|
148
142
|
readonly query?: Query;
|
|
149
|
-
/** instance-keying strategy. */
|
|
150
|
-
readonly type?: LeafType;
|
|
151
143
|
/** dynamic predicate for route matching. */
|
|
152
144
|
readonly when?: (ctx: WhenContext) => boolean;
|
|
145
|
+
/** instance-keying strategy. */
|
|
146
|
+
readonly type?: LeafType;
|
|
147
|
+
/** component to render. */
|
|
148
|
+
readonly component: ComponentType;
|
|
149
|
+
/** suspense fallback component. */
|
|
150
|
+
readonly fallback?: ReactNode;
|
|
151
|
+
/** application metadata. */
|
|
152
|
+
readonly meta?: RouteMeta;
|
|
153
153
|
}
|
|
154
154
|
/** configuration for a navigable leaf route. */
|
|
155
155
|
type RouteConfig<Params extends CodecRecord, Query extends CodecRecord> = LeafShared<Params, Query>;
|
|
@@ -159,19 +159,19 @@ interface RouteLeaf<Params extends CodecRecord = Empty, Query extends CodecRecor
|
|
|
159
159
|
}
|
|
160
160
|
/** configuration for an interior layout node. */
|
|
161
161
|
interface LayoutConfig<Children extends RouteChildren, Params extends CodecRecord> {
|
|
162
|
-
/**
|
|
163
|
-
|
|
162
|
+
/**
|
|
163
|
+
* codecs for layout parameters. parameter names must appear in all descendant leaf paths, allowing layouts
|
|
164
|
+
* to decode their own values.
|
|
165
|
+
*/
|
|
166
|
+
readonly params?: Params;
|
|
164
167
|
/** layout component rendering an outlet. */
|
|
165
168
|
readonly component: ComponentType;
|
|
166
169
|
/** suspense fallback component. */
|
|
167
170
|
readonly fallback?: ReactNode;
|
|
168
171
|
/** application metadata. */
|
|
169
172
|
readonly meta?: RouteMeta;
|
|
170
|
-
/**
|
|
171
|
-
|
|
172
|
-
* to decode their own values.
|
|
173
|
-
*/
|
|
174
|
-
readonly params?: Params;
|
|
173
|
+
/** nested child nodes. */
|
|
174
|
+
readonly children: Children;
|
|
175
175
|
}
|
|
176
176
|
/** an interior layout node. */
|
|
177
177
|
type LayoutNode<Children extends RouteChildren = RouteChildren, Params extends CodecRecord = Empty> = {
|
|
@@ -204,25 +204,25 @@ interface ResolvedNode {
|
|
|
204
204
|
readonly id: string;
|
|
205
205
|
/** key name within the parent record. */
|
|
206
206
|
readonly key: string;
|
|
207
|
-
readonly node: RouteNode;
|
|
208
|
-
/** local parameter codecs. */
|
|
209
|
-
readonly params: CodecRecord;
|
|
210
207
|
/** absolute path pattern if a route. */
|
|
211
208
|
readonly path: string | undefined;
|
|
209
|
+
/** local parameter codecs. */
|
|
210
|
+
readonly params: CodecRecord;
|
|
211
|
+
readonly node: RouteNode;
|
|
212
212
|
}
|
|
213
213
|
/** a leaf node resolved with its ancestors and metadata. */
|
|
214
214
|
interface ResolvedLeaf {
|
|
215
|
-
/** ancestors list from root to leaf. */
|
|
216
|
-
readonly chain: readonly ResolvedNode[];
|
|
217
|
-
readonly leaf: RouteLeaf;
|
|
218
215
|
/** unique route name. */
|
|
219
216
|
readonly name: string;
|
|
220
|
-
/** path parameter codecs. */
|
|
221
|
-
readonly params: CodecRecord;
|
|
222
217
|
/** absolute path pattern. */
|
|
223
218
|
readonly path: string;
|
|
219
|
+
/** path parameter codecs. */
|
|
220
|
+
readonly params: CodecRecord;
|
|
224
221
|
/** query parameter codecs. */
|
|
225
222
|
readonly query: CodecRecord;
|
|
223
|
+
readonly leaf: RouteLeaf;
|
|
224
|
+
/** ancestors list from root to leaf. */
|
|
225
|
+
readonly chain: readonly ResolvedNode[];
|
|
226
226
|
}
|
|
227
227
|
/** a compiled registry of routes and nodes. */
|
|
228
228
|
interface RouteRegistry<T> {
|
|
@@ -241,8 +241,8 @@ type ValidRoutes<T> = { readonly [K in keyof T]: T[K] extends RouteNode ? T[K] :
|
|
|
241
241
|
declare const defineRoutes: <const T>(tree: T & ValidRoutes<T>) => RouteRegistry<T>;
|
|
242
242
|
type UnionToIntersection<U> = (U extends unknown ? (arg: U) => void : never) extends ((arg: infer I) => void) ? I : never;
|
|
243
243
|
type FlattenChildren<C> = UnionToIntersection<{ [K in keyof C]: C[K] extends {
|
|
244
|
-
readonly children: infer GC;
|
|
245
244
|
readonly kind: 'layout';
|
|
245
|
+
readonly children: infer GC;
|
|
246
246
|
} ? FlattenChildren<GC> : C[K] extends {
|
|
247
247
|
readonly kind: 'route';
|
|
248
248
|
} ? { readonly [P in K]: C[K]; } : never; }[keyof C]>;
|
|
@@ -291,11 +291,11 @@ interface MatchedNode {
|
|
|
291
291
|
}
|
|
292
292
|
/** the result of matching a URL, containing the chain, leaf, name, and parameters. */
|
|
293
293
|
interface RouteMatch {
|
|
294
|
-
readonly chain: readonly MatchedNode[];
|
|
295
|
-
readonly leaf: ResolvedLeaf;
|
|
296
294
|
readonly name: string;
|
|
297
295
|
/** leaf parameters, combining path and query. */
|
|
298
296
|
readonly params: Readonly<Record<string, unknown>>;
|
|
297
|
+
readonly leaf: ResolvedLeaf;
|
|
298
|
+
readonly chain: readonly MatchedNode[];
|
|
299
299
|
}
|
|
300
300
|
/**
|
|
301
301
|
* looks up the nearest metadata value for a key on a matched chain, searching leaf-first.
|
|
@@ -309,6 +309,8 @@ declare const resolveMeta: <K extends keyof RouteMeta>(match: RouteMatch, key: K
|
|
|
309
309
|
//#region src/history/navigation.d.ts
|
|
310
310
|
/** options for navigation-api history. */
|
|
311
311
|
interface NavigationHistoryOptions {
|
|
312
|
+
/** window instance to bind to; defaults to global window. */
|
|
313
|
+
readonly window?: Window;
|
|
312
314
|
/**
|
|
313
315
|
* same-origin destinations the server owns, which the browser should load as a real page rather than the
|
|
314
316
|
* router rendering in-app — an OAuth entry point, a server-rendered admin area, a static file. without this
|
|
@@ -328,8 +330,6 @@ interface NavigationHistoryOptions {
|
|
|
328
330
|
* @param error failure reason
|
|
329
331
|
*/
|
|
330
332
|
readonly onError?: (error: unknown) => void;
|
|
331
|
-
/** window instance to bind to; defaults to global window. */
|
|
332
|
-
readonly window?: Window;
|
|
333
333
|
}
|
|
334
334
|
/**
|
|
335
335
|
* history implementation backed by the web navigation API.
|
|
@@ -358,17 +358,17 @@ declare class NavigationHistory implements History {
|
|
|
358
358
|
//#region src/view-model.d.ts
|
|
359
359
|
/** an instance node within the layout tree structure. */
|
|
360
360
|
interface InstanceNode {
|
|
361
|
-
readonly children: InstanceNode[];
|
|
362
361
|
readonly instanceKey: string;
|
|
363
362
|
readonly node: ResolvedNode;
|
|
364
363
|
readonly params: Readonly<Record<string, unknown>>;
|
|
364
|
+
readonly children: InstanceNode[];
|
|
365
365
|
}
|
|
366
366
|
/** the computed rendering view configuration. */
|
|
367
367
|
interface View {
|
|
368
|
-
/** instance keys along the active chain from root to leaf. */
|
|
369
|
-
readonly activePath: readonly string[];
|
|
370
368
|
/** the top-level pool (instances at the root outlet). */
|
|
371
369
|
readonly roots: readonly InstanceNode[];
|
|
370
|
+
/** instance keys along the active chain from root to leaf. */
|
|
371
|
+
readonly activePath: readonly string[];
|
|
372
372
|
}
|
|
373
373
|
//#endregion
|
|
374
374
|
//#region src/lazy.d.ts
|
|
@@ -394,25 +394,25 @@ declare const lazy: <T extends ComponentType<any>>(init: () => Promise<{
|
|
|
394
394
|
//#region src/router.d.ts
|
|
395
395
|
/** options for a navigation. */
|
|
396
396
|
interface NavigateOptions<R extends RouteRegistry<unknown>> extends HistoryNavigateOptions {
|
|
397
|
-
/** replace the active history entry instead. */
|
|
398
|
-
readonly replace?: boolean;
|
|
399
397
|
/** where to go */
|
|
400
398
|
readonly to: RouteTarget<R> | string;
|
|
399
|
+
/** replace the active history entry instead. */
|
|
400
|
+
readonly replace?: boolean;
|
|
401
401
|
}
|
|
402
402
|
/** options for the router constructor. */
|
|
403
403
|
interface RouterOptions<R extends RouteRegistry<unknown>> {
|
|
404
|
-
/**
|
|
405
|
-
readonly
|
|
404
|
+
/** compiled route registry. */
|
|
405
|
+
readonly routes: R;
|
|
406
406
|
/** backing history instance. */
|
|
407
407
|
readonly history: History;
|
|
408
|
-
/** maximum number of backward entries to keep warm. */
|
|
409
|
-
readonly max?: number;
|
|
410
408
|
/** component rendered when no route matches. */
|
|
411
409
|
readonly notFound?: ComponentType;
|
|
410
|
+
/** default fallback component shown during lazy chunk loading. */
|
|
411
|
+
readonly defaultFallback?: ReactNode;
|
|
412
|
+
/** maximum number of backward entries to keep warm. */
|
|
413
|
+
readonly max?: number;
|
|
412
414
|
/** route names of pathless singleton routes to keep warm for the router's lifetime once first visited. */
|
|
413
415
|
readonly pins?: readonly string[];
|
|
414
|
-
/** compiled route registry. */
|
|
415
|
-
readonly routes: R;
|
|
416
416
|
/** window whose scroll position the router restores, defaulting to the global one where there is one. */
|
|
417
417
|
readonly window?: Window;
|
|
418
418
|
}
|
|
@@ -603,12 +603,12 @@ declare const useIsFocused: () => boolean;
|
|
|
603
603
|
declare const useFocusEffect: (effect: EffectCallback) => void;
|
|
604
604
|
/** typed hooks bound to a specific route registry. */
|
|
605
605
|
interface RouterHooks<R extends RouteRegistry<unknown>> {
|
|
606
|
-
/** gets a route's parameters and a setter that patches its query in place. */
|
|
607
|
-
useParams<K extends RouteName<R>>(name: K): readonly [ParamsOf<R, K>, (patch: QueryPatchOf<R, K>) => void];
|
|
608
606
|
/** gets the typed router instance. */
|
|
609
607
|
useRouter(): Router<R>;
|
|
610
608
|
/** gets the active route's name and parameters. */
|
|
611
609
|
useTarget(): MatchedTarget<R>;
|
|
610
|
+
/** gets a route's parameters and a setter that patches its query in place. */
|
|
611
|
+
useParams<K extends RouteName<R>>(name: K): readonly [ParamsOf<R, K>, (patch: QueryPatchOf<R, K>) => void];
|
|
612
612
|
}
|
|
613
613
|
/**
|
|
614
614
|
* generates typed hooks bound to a specific route registry.
|
|
@@ -625,10 +625,10 @@ declare const createRouterHooks: <R extends RouteRegistry<unknown>>(routes: R) =
|
|
|
625
625
|
//#region src/react/link.d.ts
|
|
626
626
|
/** properties for the link component. */
|
|
627
627
|
interface LinkProps extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {
|
|
628
|
-
/** replace the current history entry. */
|
|
629
|
-
readonly replace?: boolean;
|
|
630
628
|
/** target relative URL path. */
|
|
631
629
|
readonly to: string;
|
|
630
|
+
/** replace the current history entry. */
|
|
631
|
+
readonly replace?: boolean;
|
|
632
632
|
}
|
|
633
633
|
/**
|
|
634
634
|
* anchor element that intercepts clicks for in-app navigation.
|
|
@@ -638,6 +638,6 @@ interface LinkProps extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'
|
|
|
638
638
|
* @param props link properties
|
|
639
639
|
* @returns rendered anchor element
|
|
640
640
|
*/
|
|
641
|
-
declare const Link: ({
|
|
641
|
+
declare const Link: ({ to, replace, children, onClick, ...rest }: LinkProps) => ReactElement;
|
|
642
642
|
//#endregion
|
|
643
643
|
export { type BuildParamsOf, type Codec, type DefaultedCodec, type History, type HistoryAction, type HistoryEntry, type HistoryListener, type HistoryLocation, type HistoryNavigateOptions, type HistoryScrollBehavior, type HistoryUpdate, type Infer, type InstanceNode, type LayoutConfig, type LayoutNode, type LazyComponent, type LeafType, Link, type LinkProps, type MatchedNode, type MatchedTarget, type NavigateOptions, NavigationHistory, type NavigationHistoryOptions, type OptionalCodec, Outlet, type ParamsOf, type QueryPatchOf, type ReadonlyURLSearchParams, type ResolvedLeaf, type ResolvedNode, type RouteChildren, type RouteConfig, type RouteLeaf, type RouteMatch, type RouteMeta, type RouteName, type RouteNode, type RouteRegistry, type RouteTarget, Router, type RouterHooks, type RouterOptions, RouterView, type View, type WhenContext, boolean, createRouterHooks, defineRoutes, enumOf, integer, layout, lazy, nonEmpty, optional, resolveMeta, route, string, useFocusEffect, useIsFocused, useLocation, useParams, useRoute, useRouter, withDefault };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as parsePath, i as encodeSegment, n as decodeRemainder, o as resolvePath, r as encodeRemainder, t as createPath } from "./url-
|
|
1
|
+
import { a as parsePath, i as encodeSegment, n as decodeRemainder, o as resolvePath, r as encodeRemainder, t as createPath } from "./url-ghfWE9j6.mjs";
|
|
2
2
|
import { Suspense, createContext, lazy as lazy$1, useContext, useEffect, useLayoutEffect, useMemo, useSyncExternalStore } from "react";
|
|
3
3
|
import { SimpleEventEmitter } from "@mary-ext/simple-event-emitter";
|
|
4
4
|
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
@@ -185,9 +185,9 @@ const defineRoutes = (tree) => {
|
|
|
185
185
|
kind: node.kind,
|
|
186
186
|
id,
|
|
187
187
|
key,
|
|
188
|
-
node,
|
|
188
|
+
path: node.kind === "route" ? node.path : void 0,
|
|
189
189
|
params,
|
|
190
|
-
|
|
190
|
+
node
|
|
191
191
|
};
|
|
192
192
|
ids.add(id);
|
|
193
193
|
const chain = [...parentChain, resolved];
|
|
@@ -210,12 +210,12 @@ const defineRoutes = (tree) => {
|
|
|
210
210
|
for (const layoutParam of Object.keys(ancestor.params)) if (!ownPathParams.includes(layoutParam)) throw new Error(`stacker: layout '${ancestor.id}' param '${layoutParam}' is not present in route '${key}' path '${node.path}'`);
|
|
211
211
|
}
|
|
212
212
|
leaves.set(key, {
|
|
213
|
-
chain,
|
|
214
|
-
leaf: node,
|
|
215
213
|
name: key,
|
|
216
|
-
params,
|
|
217
214
|
path: node.path,
|
|
218
|
-
|
|
215
|
+
params,
|
|
216
|
+
query,
|
|
217
|
+
leaf: node,
|
|
218
|
+
chain
|
|
219
219
|
});
|
|
220
220
|
}
|
|
221
221
|
};
|
|
@@ -259,16 +259,16 @@ const compile = (path) => {
|
|
|
259
259
|
if (splat !== void 0) {
|
|
260
260
|
paramNames.push(splat);
|
|
261
261
|
return {
|
|
262
|
-
|
|
262
|
+
regex: new RegExp(`^${pattern}(?:/(.*))?$`),
|
|
263
263
|
paramNames,
|
|
264
|
-
|
|
264
|
+
hasSplat: true
|
|
265
265
|
};
|
|
266
266
|
}
|
|
267
267
|
const suffix = pattern.endsWith("/") ? "" : "/?";
|
|
268
268
|
return {
|
|
269
|
-
|
|
269
|
+
regex: new RegExp(`^${pattern}${suffix}$`),
|
|
270
270
|
paramNames,
|
|
271
|
-
|
|
271
|
+
hasSplat: false
|
|
272
272
|
};
|
|
273
273
|
};
|
|
274
274
|
/** matches URLs in declaration order against a compiled route registry. */
|
|
@@ -276,22 +276,22 @@ var Matcher = class {
|
|
|
276
276
|
#compiled;
|
|
277
277
|
constructor(registry) {
|
|
278
278
|
this.#compiled = [...registry.leaves.values()].map((leaf) => {
|
|
279
|
-
const {
|
|
279
|
+
const { regex, paramNames, hasSplat } = compile(leaf.path);
|
|
280
280
|
return {
|
|
281
|
-
hasSplat,
|
|
282
281
|
leaf,
|
|
282
|
+
regex,
|
|
283
283
|
paramNames,
|
|
284
|
-
|
|
284
|
+
hasSplat
|
|
285
285
|
};
|
|
286
286
|
});
|
|
287
287
|
}
|
|
288
288
|
match(pathname, search = "", hash = "") {
|
|
289
289
|
const rawSearch = new URLSearchParams(search);
|
|
290
290
|
const ctx = {
|
|
291
|
-
hash,
|
|
292
291
|
pathname,
|
|
293
|
-
|
|
294
|
-
|
|
292
|
+
search,
|
|
293
|
+
hash,
|
|
294
|
+
rawSearch
|
|
295
295
|
};
|
|
296
296
|
for (const compiled of this.#compiled) {
|
|
297
297
|
const { leaf, regex } = compiled;
|
|
@@ -315,14 +315,14 @@ var Matcher = class {
|
|
|
315
315
|
};
|
|
316
316
|
});
|
|
317
317
|
return {
|
|
318
|
-
chain,
|
|
319
|
-
leaf,
|
|
320
318
|
name: leaf.name,
|
|
321
|
-
params: chain.at(-1)?.params ?? {}
|
|
319
|
+
params: chain.at(-1)?.params ?? {},
|
|
320
|
+
leaf,
|
|
321
|
+
chain
|
|
322
322
|
};
|
|
323
323
|
}
|
|
324
324
|
}
|
|
325
|
-
#rawValues({
|
|
325
|
+
#rawValues({ paramNames, hasSplat }, execed) {
|
|
326
326
|
const splatIndex = hasSplat ? paramNames.length - 1 : -1;
|
|
327
327
|
const raw = {};
|
|
328
328
|
for (const [i, name] of paramNames.entries()) {
|
|
@@ -389,6 +389,7 @@ const toAction = (type) => {
|
|
|
389
389
|
}
|
|
390
390
|
};
|
|
391
391
|
const isAbort = (error) => typeof error === "object" && error !== null && "name" in error && error.name === "AbortError";
|
|
392
|
+
const isWebKit = (userAgent) => /AppleWebKit\//.test(userAgent) && !/(?:Chrome|Chromium)\//.test(userAgent);
|
|
392
393
|
/**
|
|
393
394
|
* history implementation backed by the web navigation API.
|
|
394
395
|
*
|
|
@@ -403,10 +404,12 @@ var NavigationHistory = class {
|
|
|
403
404
|
#win;
|
|
404
405
|
#disposed = false;
|
|
405
406
|
#location;
|
|
407
|
+
#interceptTraversals;
|
|
406
408
|
constructor(options = {}) {
|
|
407
409
|
this.#win = options.window ?? window;
|
|
408
410
|
this.#nav = this.#win.navigation;
|
|
409
411
|
this.#ignore = options.ignore;
|
|
412
|
+
this.#interceptTraversals = !isWebKit(this.#win.navigator.userAgent);
|
|
410
413
|
this.#onError = options.onError ?? ((error) => {
|
|
411
414
|
this.#win.reportError(error);
|
|
412
415
|
});
|
|
@@ -427,12 +430,12 @@ var NavigationHistory = class {
|
|
|
427
430
|
}
|
|
428
431
|
entries() {
|
|
429
432
|
return this.#nav.entries().map((entry) => ({
|
|
433
|
+
key: entry.key,
|
|
430
434
|
id: entry.id,
|
|
431
435
|
index: entry.index,
|
|
432
|
-
|
|
436
|
+
url: toRelative(entry.url),
|
|
433
437
|
sameDocument: entry.sameDocument,
|
|
434
|
-
state: entry.getState()
|
|
435
|
-
url: toRelative(entry.url)
|
|
438
|
+
state: entry.getState()
|
|
436
439
|
}));
|
|
437
440
|
}
|
|
438
441
|
push(to, options = {}) {
|
|
@@ -492,43 +495,65 @@ var NavigationHistory = class {
|
|
|
492
495
|
const url = new URL(to, this.#win.location.href).href;
|
|
493
496
|
const envelope = {
|
|
494
497
|
[ENVELOPE]: true,
|
|
495
|
-
|
|
496
|
-
|
|
498
|
+
scroll: options.scroll ?? "auto",
|
|
499
|
+
info: options.info
|
|
497
500
|
};
|
|
498
501
|
this.#report(this.#track(this.#nav.navigate(url, {
|
|
499
|
-
|
|
502
|
+
state: options.state ?? null,
|
|
500
503
|
info: envelope,
|
|
501
|
-
|
|
504
|
+
history
|
|
502
505
|
})));
|
|
503
506
|
}
|
|
504
507
|
#read() {
|
|
505
508
|
const entry = this.#nav.currentEntry;
|
|
506
509
|
if (entry === null) throw new Error("stacker: the navigation API has no current entry (an opaque origin?)");
|
|
507
|
-
const {
|
|
510
|
+
const { pathname, search, hash } = new URL(entry.url ?? this.#win.location.href);
|
|
508
511
|
return {
|
|
509
|
-
|
|
512
|
+
key: entry.key,
|
|
510
513
|
id: entry.id,
|
|
511
514
|
index: entry.index,
|
|
512
|
-
key: entry.key,
|
|
513
515
|
pathname,
|
|
514
516
|
search,
|
|
517
|
+
hash,
|
|
515
518
|
state: entry.getState()
|
|
516
519
|
};
|
|
517
520
|
}
|
|
518
521
|
#onCurrentEntryChange = (event) => {
|
|
522
|
+
if (event.navigationType === "traverse" && !this.#interceptTraversals) {
|
|
523
|
+
this.#report(this.#traverseUnintercepted());
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
519
526
|
if (event.navigationType !== null) return;
|
|
520
527
|
this.#location = this.#read();
|
|
521
528
|
this.#report(this.#notify({
|
|
522
529
|
action: "update",
|
|
523
|
-
info: void 0,
|
|
524
530
|
location: this.#location,
|
|
525
|
-
scroll: "preserve"
|
|
531
|
+
scroll: "preserve",
|
|
532
|
+
info: void 0
|
|
526
533
|
}));
|
|
527
534
|
};
|
|
535
|
+
async #traverseUnintercepted() {
|
|
536
|
+
const doc = this.#win.document;
|
|
537
|
+
const focused = doc.activeElement;
|
|
538
|
+
this.#location = this.#read();
|
|
539
|
+
await this.#notify({
|
|
540
|
+
action: "traverse",
|
|
541
|
+
location: this.#location,
|
|
542
|
+
scroll: "auto",
|
|
543
|
+
info: void 0
|
|
544
|
+
});
|
|
545
|
+
if (doc.activeElement !== focused) return;
|
|
546
|
+
const body = doc.body;
|
|
547
|
+
const hadTabIndex = body.hasAttribute("tabindex");
|
|
548
|
+
if (!hadTabIndex) body.tabIndex = -1;
|
|
549
|
+
body.focus({ preventScroll: true });
|
|
550
|
+
if (!hadTabIndex) body.removeAttribute("tabindex");
|
|
551
|
+
}
|
|
528
552
|
#onNavigate = (event) => {
|
|
529
553
|
const action = toAction(event.navigationType);
|
|
530
554
|
if (action === void 0 || !event.canIntercept) return;
|
|
531
555
|
if (event.downloadRequest !== null || event.formData !== null) return;
|
|
556
|
+
if (action === "traverse" && !this.#interceptTraversals) return;
|
|
532
557
|
if (action !== "traverse" && this.#ignore?.(new URL(event.destination.url))) return;
|
|
533
558
|
const envelope = isEnvelope(event.info) ? event.info : void 0;
|
|
534
559
|
const info = envelope === void 0 ? event.info : envelope.info;
|
|
@@ -541,9 +566,9 @@ var NavigationHistory = class {
|
|
|
541
566
|
this.#location = this.#read();
|
|
542
567
|
await this.#notify({
|
|
543
568
|
action,
|
|
544
|
-
info,
|
|
545
569
|
location: this.#location,
|
|
546
|
-
scroll
|
|
570
|
+
scroll,
|
|
571
|
+
info
|
|
547
572
|
});
|
|
548
573
|
}
|
|
549
574
|
});
|
|
@@ -555,7 +580,7 @@ var NavigationHistory = class {
|
|
|
555
580
|
};
|
|
556
581
|
const toRelative = (url) => {
|
|
557
582
|
if (url === null) return null;
|
|
558
|
-
const {
|
|
583
|
+
const { pathname, search, hash } = new URL(url);
|
|
559
584
|
return `${pathname}${search}${hash}`;
|
|
560
585
|
};
|
|
561
586
|
//#endregion
|
|
@@ -643,9 +668,9 @@ var Builder = class {
|
|
|
643
668
|
}
|
|
644
669
|
const query = search.toString();
|
|
645
670
|
return createPath({
|
|
646
|
-
hash: "",
|
|
647
671
|
pathname,
|
|
648
|
-
search: query ? `?${query}` : ""
|
|
672
|
+
search: query ? `?${query}` : "",
|
|
673
|
+
hash: ""
|
|
649
674
|
});
|
|
650
675
|
}
|
|
651
676
|
};
|
|
@@ -715,10 +740,10 @@ const computeView = (cached, pinned, activeKey) => {
|
|
|
715
740
|
let inst = byKey.get(key);
|
|
716
741
|
if (inst === void 0) {
|
|
717
742
|
inst = {
|
|
718
|
-
children: [],
|
|
719
743
|
instanceKey: key,
|
|
720
744
|
node: matched.node,
|
|
721
|
-
params: matched.params
|
|
745
|
+
params: matched.params,
|
|
746
|
+
children: []
|
|
722
747
|
};
|
|
723
748
|
byKey.set(key, inst);
|
|
724
749
|
pool.push(inst);
|
|
@@ -731,8 +756,8 @@ const computeView = (cached, pinned, activeKey) => {
|
|
|
731
756
|
const active = cached.find((entry) => entry.key === activeKey);
|
|
732
757
|
if (active === void 0) throw new Error(`stacker: active entry '${activeKey}' is not in the cached set`);
|
|
733
758
|
return {
|
|
734
|
-
|
|
735
|
-
|
|
759
|
+
roots,
|
|
760
|
+
activePath: active.match.chain.map((matched) => instanceKey(matched, active.key))
|
|
736
761
|
};
|
|
737
762
|
};
|
|
738
763
|
//#endregion
|
|
@@ -870,7 +895,7 @@ var Router = class {
|
|
|
870
895
|
* @returns the target it resolves to, or undefined when no route matches
|
|
871
896
|
*/
|
|
872
897
|
match(to) {
|
|
873
|
-
const {
|
|
898
|
+
const { pathname, search, hash } = resolvePath(to, this.#history.location);
|
|
874
899
|
const match = this.#matcher.match(pathname, search, hash);
|
|
875
900
|
return match === void 0 ? void 0 : toTarget(match);
|
|
876
901
|
}
|
|
@@ -880,7 +905,7 @@ var Router = class {
|
|
|
880
905
|
* @param options where to go, and how to get there
|
|
881
906
|
*/
|
|
882
907
|
navigate(options) {
|
|
883
|
-
const { replace = false,
|
|
908
|
+
const { to, replace = false, ...rest } = options;
|
|
884
909
|
const url = typeof to === "string" ? to : this.#builder.build(to);
|
|
885
910
|
if (replace) this.#history.replace(url, rest);
|
|
886
911
|
else this.#history.push(url, rest);
|
|
@@ -906,7 +931,7 @@ var Router = class {
|
|
|
906
931
|
*/
|
|
907
932
|
popTo(to) {
|
|
908
933
|
const url = typeof to === "string" ? to : this.#builder.build(to);
|
|
909
|
-
const {
|
|
934
|
+
const { pathname, search, hash } = resolvePath(url, this.#history.location);
|
|
910
935
|
const wantedMatch = this.#matcher.match(pathname, search, hash);
|
|
911
936
|
if (wantedMatch !== void 0) {
|
|
912
937
|
const wanted = this.#canonical(wantedMatch);
|
|
@@ -953,13 +978,13 @@ var Router = class {
|
|
|
953
978
|
}
|
|
954
979
|
const query = search.toString();
|
|
955
980
|
const to = createPath({
|
|
956
|
-
hash: location.hash,
|
|
957
981
|
pathname: location.pathname,
|
|
958
|
-
search: query ? `?${query}` : ""
|
|
982
|
+
search: query ? `?${query}` : "",
|
|
983
|
+
hash: location.hash
|
|
959
984
|
});
|
|
960
985
|
this.#history.replace(to, {
|
|
961
|
-
|
|
962
|
-
|
|
986
|
+
state: location.state,
|
|
987
|
+
scroll: "preserve"
|
|
963
988
|
});
|
|
964
989
|
}
|
|
965
990
|
/**
|
|
@@ -1051,11 +1076,11 @@ var Router = class {
|
|
|
1051
1076
|
const match = this.#match(location);
|
|
1052
1077
|
preloadAll(match.leaf.chain);
|
|
1053
1078
|
const entry = {
|
|
1054
|
-
index: location.index,
|
|
1055
1079
|
key: location.key,
|
|
1080
|
+
index: location.index,
|
|
1056
1081
|
match,
|
|
1057
|
-
|
|
1058
|
-
|
|
1082
|
+
target: toTarget(match),
|
|
1083
|
+
scrollPos
|
|
1059
1084
|
};
|
|
1060
1085
|
this.#entries.set(location.key, entry);
|
|
1061
1086
|
this.#recency = [entry.key, ...this.#recency.filter((key) => key !== entry.key)];
|
|
@@ -1065,8 +1090,8 @@ var Router = class {
|
|
|
1065
1090
|
#recompute() {
|
|
1066
1091
|
const active = this.#active();
|
|
1067
1092
|
const cachedKeys = computeCachedKeys([...this.#entries.values()].map((entry) => ({
|
|
1068
|
-
|
|
1069
|
-
|
|
1093
|
+
key: entry.key,
|
|
1094
|
+
index: entry.index
|
|
1070
1095
|
})), active.key, this.#recency, { max: this.#max });
|
|
1071
1096
|
const cached = [];
|
|
1072
1097
|
for (const entry of this.#entries.values()) if (cachedKeys.has(entry.key)) cached.push({
|
|
@@ -1083,7 +1108,7 @@ var Router = class {
|
|
|
1083
1108
|
if (leaf === void 0) throw new Error(`stacker: pinned route '${name}' is not in the registry`);
|
|
1084
1109
|
if ((leaf.leaf.type ?? "page") !== "singleton") throw new Error(`stacker: pinned route '${name}' must be a singleton`);
|
|
1085
1110
|
if (Object.keys(leaf.params).length > 0) throw new Error(`stacker: pinned route '${name}' must have no path params`);
|
|
1086
|
-
const {
|
|
1111
|
+
const { pathname, search, hash } = parsePath(leaf.path);
|
|
1087
1112
|
const match = this.#matcher.match(pathname, search, hash);
|
|
1088
1113
|
if (match === void 0) throw new Error(`stacker: pinned route '${name}' (path '${leaf.path}') did not match`);
|
|
1089
1114
|
return {
|
|
@@ -1093,13 +1118,13 @@ var Router = class {
|
|
|
1093
1118
|
}
|
|
1094
1119
|
#notFoundMatch() {
|
|
1095
1120
|
return {
|
|
1121
|
+
name: this.#notFound.leaf.name,
|
|
1122
|
+
params: {},
|
|
1123
|
+
leaf: this.#notFound.leaf,
|
|
1096
1124
|
chain: [{
|
|
1097
1125
|
node: this.#notFound.node,
|
|
1098
1126
|
params: {}
|
|
1099
|
-
}]
|
|
1100
|
-
leaf: this.#notFound.leaf,
|
|
1101
|
-
name: this.#notFound.leaf.name,
|
|
1102
|
-
params: {}
|
|
1127
|
+
}]
|
|
1103
1128
|
};
|
|
1104
1129
|
}
|
|
1105
1130
|
};
|
|
@@ -1110,28 +1135,28 @@ const toTarget = (match) => ({
|
|
|
1110
1135
|
const makeNotFound = (component) => {
|
|
1111
1136
|
const leafNode = {
|
|
1112
1137
|
kind: "route",
|
|
1113
|
-
component,
|
|
1114
|
-
params: {},
|
|
1115
1138
|
path: "*",
|
|
1139
|
+
params: {},
|
|
1116
1140
|
query: {},
|
|
1117
|
-
type: "page"
|
|
1141
|
+
type: "page",
|
|
1142
|
+
component
|
|
1118
1143
|
};
|
|
1119
1144
|
const node = {
|
|
1120
1145
|
kind: "route",
|
|
1121
1146
|
id: " notFound",
|
|
1122
1147
|
key: " notFound",
|
|
1123
|
-
|
|
1148
|
+
path: "*",
|
|
1124
1149
|
params: {},
|
|
1125
|
-
|
|
1150
|
+
node: leafNode
|
|
1126
1151
|
};
|
|
1127
1152
|
return {
|
|
1128
1153
|
leaf: {
|
|
1129
|
-
chain: [node],
|
|
1130
|
-
leaf: leafNode,
|
|
1131
1154
|
name: " notFound",
|
|
1132
|
-
params: {},
|
|
1133
1155
|
path: "*",
|
|
1134
|
-
|
|
1156
|
+
params: {},
|
|
1157
|
+
query: {},
|
|
1158
|
+
leaf: leafNode,
|
|
1159
|
+
chain: [node]
|
|
1135
1160
|
},
|
|
1136
1161
|
node
|
|
1137
1162
|
};
|
|
@@ -1181,11 +1206,11 @@ const ScrollRestorer = ({ router, view }) => {
|
|
|
1181
1206
|
const Pool = ({ nodes }) => {
|
|
1182
1207
|
const activePath = useContext(ActiveChainContext);
|
|
1183
1208
|
return /* @__PURE__ */ jsx(Fragment, { children: nodes.map((node) => /* @__PURE__ */ jsx(Branch, {
|
|
1184
|
-
|
|
1185
|
-
node
|
|
1209
|
+
node,
|
|
1210
|
+
focused: activePath.includes(node.instanceKey)
|
|
1186
1211
|
}, node.instanceKey)) });
|
|
1187
1212
|
};
|
|
1188
|
-
const Branch = ({
|
|
1213
|
+
const Branch = ({ node, focused }) => {
|
|
1189
1214
|
const resolved = node.node;
|
|
1190
1215
|
const Component = resolved.node.component;
|
|
1191
1216
|
const router = useContext(RouterContext);
|
|
@@ -1196,8 +1221,8 @@ const Branch = ({ focused, node }) => {
|
|
|
1196
1221
|
style: { display: focused ? "contents" : "none" },
|
|
1197
1222
|
children: /* @__PURE__ */ jsx(CurrentNodeContext, {
|
|
1198
1223
|
value: {
|
|
1199
|
-
|
|
1200
|
-
|
|
1224
|
+
node,
|
|
1225
|
+
focused
|
|
1201
1226
|
},
|
|
1202
1227
|
children: /* @__PURE__ */ jsx(Suspense, {
|
|
1203
1228
|
fallback: fallback ?? null,
|
|
@@ -1301,6 +1326,11 @@ const createRouterHooks = (routes) => {
|
|
|
1301
1326
|
return router;
|
|
1302
1327
|
};
|
|
1303
1328
|
return {
|
|
1329
|
+
useRouter: useTypedRouter,
|
|
1330
|
+
useTarget() {
|
|
1331
|
+
const router = useTypedRouter();
|
|
1332
|
+
return useSyncExternalStore(router.subscribe, () => router.target);
|
|
1333
|
+
},
|
|
1304
1334
|
useParams(name) {
|
|
1305
1335
|
const router = useTypedRouter();
|
|
1306
1336
|
const current = useContext(CurrentNodeContext);
|
|
@@ -1310,11 +1340,6 @@ const createRouterHooks = (routes) => {
|
|
|
1310
1340
|
if (current === null) throw new Error("stacker: useParams must be used within a route component");
|
|
1311
1341
|
if (current.node.node.kind !== "route" || current.node.node.key !== name) throw new Error(`stacker: useParams('${name}') called under ${current.node.node.kind} '${current.node.node.key}'`);
|
|
1312
1342
|
return [current.node.params, replace];
|
|
1313
|
-
},
|
|
1314
|
-
useRouter: useTypedRouter,
|
|
1315
|
-
useTarget() {
|
|
1316
|
-
const router = useTypedRouter();
|
|
1317
|
-
return useSyncExternalStore(router.subscribe, () => router.target);
|
|
1318
1343
|
}
|
|
1319
1344
|
};
|
|
1320
1345
|
};
|
|
@@ -1336,7 +1361,7 @@ const isRoutable = (to) => {
|
|
|
1336
1361
|
* @param props link properties
|
|
1337
1362
|
* @returns rendered anchor element
|
|
1338
1363
|
*/
|
|
1339
|
-
const Link = ({
|
|
1364
|
+
const Link = ({ to, replace, children, onClick, ...rest }) => {
|
|
1340
1365
|
const router = useRouter();
|
|
1341
1366
|
return /* @__PURE__ */ jsx("a", {
|
|
1342
1367
|
href: to,
|
|
@@ -1345,8 +1370,8 @@ const Link = ({ children, onClick, replace, to, ...rest }) => {
|
|
|
1345
1370
|
if (event.defaultPrevented || isModifiedClick(event) || rest.download !== void 0 || rest.target !== void 0 && rest.target !== "" && rest.target !== "_self" || !isRoutable(to)) return;
|
|
1346
1371
|
event.preventDefault();
|
|
1347
1372
|
router.navigate({
|
|
1348
|
-
|
|
1349
|
-
|
|
1373
|
+
to,
|
|
1374
|
+
replace
|
|
1350
1375
|
});
|
|
1351
1376
|
},
|
|
1352
1377
|
...rest,
|
package/dist/testing.d.mts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { a as HistoryLocation, i as HistoryListener, o as HistoryNavigateOptions, r as HistoryEntry, t as History } from "./types
|
|
1
|
+
import { a as HistoryLocation, i as HistoryListener, o as HistoryNavigateOptions, r as HistoryEntry, t as History } from "./types--iOZjfC1.mjs";
|
|
2
2
|
//#region src/history/memory.d.ts
|
|
3
3
|
/** options for the memory history constructor. */
|
|
4
4
|
interface MemoryHistoryOptions {
|
|
5
|
-
/** initially active entry index. */
|
|
6
|
-
readonly index?: number;
|
|
7
5
|
/** initial entry relative URLs. */
|
|
8
6
|
readonly initialEntries?: readonly string[];
|
|
7
|
+
/** initially active entry index. */
|
|
8
|
+
readonly index?: number;
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
11
|
* in-memory history manager for testing.
|
package/dist/testing.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { o as resolvePath, t as createPath } from "./url-
|
|
1
|
+
import { o as resolvePath, t as createPath } from "./url-ghfWE9j6.mjs";
|
|
2
2
|
import { SimpleEventEmitter } from "@mary-ext/simple-event-emitter";
|
|
3
3
|
//#region src/history/memory.ts
|
|
4
4
|
const ROOT = {
|
|
5
|
-
hash: "",
|
|
6
5
|
pathname: "/",
|
|
7
|
-
search: ""
|
|
6
|
+
search: "",
|
|
7
|
+
hash: ""
|
|
8
8
|
};
|
|
9
9
|
/**
|
|
10
10
|
* in-memory history manager for testing.
|
|
@@ -34,12 +34,12 @@ var MemoryHistory = class {
|
|
|
34
34
|
}
|
|
35
35
|
entries() {
|
|
36
36
|
return this.#entries.map((location) => ({
|
|
37
|
+
key: location.key,
|
|
37
38
|
id: location.id,
|
|
38
39
|
index: location.index,
|
|
39
|
-
|
|
40
|
+
url: createPath(location),
|
|
40
41
|
sameDocument: true,
|
|
41
|
-
state: location.state
|
|
42
|
-
url: createPath(location)
|
|
42
|
+
state: location.state
|
|
43
43
|
}));
|
|
44
44
|
}
|
|
45
45
|
push(to, options = {}) {
|
|
@@ -49,9 +49,9 @@ var MemoryHistory = class {
|
|
|
49
49
|
this.#entries = [...this.#entries.slice(0, this.#index), location];
|
|
50
50
|
this.#emit({
|
|
51
51
|
action: "push",
|
|
52
|
-
info: options.info,
|
|
53
52
|
location,
|
|
54
|
-
scroll: options.scroll ?? "auto"
|
|
53
|
+
scroll: options.scroll ?? "auto",
|
|
54
|
+
info: options.info
|
|
55
55
|
});
|
|
56
56
|
}
|
|
57
57
|
replace(to, options = {}) {
|
|
@@ -60,9 +60,9 @@ var MemoryHistory = class {
|
|
|
60
60
|
this.#entries = this.#entries.with(this.#index, location);
|
|
61
61
|
this.#emit({
|
|
62
62
|
action: "replace",
|
|
63
|
-
info: options.info,
|
|
64
63
|
location,
|
|
65
|
-
scroll: options.scroll ?? "auto"
|
|
64
|
+
scroll: options.scroll ?? "auto",
|
|
65
|
+
info: options.info
|
|
66
66
|
});
|
|
67
67
|
}
|
|
68
68
|
updateState(state) {
|
|
@@ -73,9 +73,9 @@ var MemoryHistory = class {
|
|
|
73
73
|
this.#entries = this.#entries.with(this.#index, location);
|
|
74
74
|
this.#emit({
|
|
75
75
|
action: "update",
|
|
76
|
-
info: void 0,
|
|
77
76
|
location,
|
|
78
|
-
scroll: "preserve"
|
|
77
|
+
scroll: "preserve",
|
|
78
|
+
info: void 0
|
|
79
79
|
});
|
|
80
80
|
}
|
|
81
81
|
traverseTo(key) {
|
|
@@ -90,9 +90,9 @@ var MemoryHistory = class {
|
|
|
90
90
|
this.#index = next;
|
|
91
91
|
this.#emit({
|
|
92
92
|
action: "traverse",
|
|
93
|
-
info: void 0,
|
|
94
93
|
location: this.#current(),
|
|
95
|
-
scroll: "auto"
|
|
94
|
+
scroll: "auto",
|
|
95
|
+
info: void 0
|
|
96
96
|
});
|
|
97
97
|
}
|
|
98
98
|
back() {
|
|
@@ -114,14 +114,14 @@ var MemoryHistory = class {
|
|
|
114
114
|
return `k${this.#keySeq++}`;
|
|
115
115
|
}
|
|
116
116
|
#make(to, base, id, key, index, state) {
|
|
117
|
-
const {
|
|
117
|
+
const { pathname, search, hash } = resolvePath(to, base);
|
|
118
118
|
return {
|
|
119
|
-
|
|
119
|
+
key,
|
|
120
120
|
id,
|
|
121
121
|
index,
|
|
122
|
-
key,
|
|
123
122
|
pathname,
|
|
124
123
|
search,
|
|
124
|
+
hash,
|
|
125
125
|
state: structuredClone(state)
|
|
126
126
|
};
|
|
127
127
|
}
|
|
@@ -10,33 +10,33 @@
|
|
|
10
10
|
type HistoryAction = 'push' | 'replace' | 'traverse' | 'update';
|
|
11
11
|
/** an entry in the browser session history ledger. */
|
|
12
12
|
interface HistoryEntry {
|
|
13
|
+
/** slot identity preserved across replacements. */
|
|
14
|
+
readonly key: string;
|
|
13
15
|
/** unique per-revision identifier. */
|
|
14
16
|
readonly id: string;
|
|
15
17
|
/** entry position in the ledger. */
|
|
16
18
|
readonly index: number;
|
|
17
|
-
/**
|
|
18
|
-
readonly
|
|
19
|
+
/** entry URL or null if hidden. */
|
|
20
|
+
readonly url: string | null;
|
|
19
21
|
/** whether traversal to this entry stays within the current document. */
|
|
20
22
|
readonly sameDocument: boolean;
|
|
21
23
|
/** state associated with the entry. */
|
|
22
24
|
readonly state: unknown;
|
|
23
|
-
/** entry URL or null if hidden. */
|
|
24
|
-
readonly url: string | null;
|
|
25
25
|
}
|
|
26
26
|
/** active history entry with parsed URL components. */
|
|
27
27
|
interface HistoryLocation {
|
|
28
|
-
/**
|
|
29
|
-
readonly
|
|
28
|
+
/** slot identity. */
|
|
29
|
+
readonly key: string;
|
|
30
30
|
/** per-revision identifier. */
|
|
31
31
|
readonly id: string;
|
|
32
32
|
/** entry position in the ledger. */
|
|
33
33
|
readonly index: number;
|
|
34
|
-
/** slot identity. */
|
|
35
|
-
readonly key: string;
|
|
36
34
|
/** URL pathname starting with a slash. */
|
|
37
35
|
readonly pathname: string;
|
|
38
36
|
/** URL query string including the leading question mark. */
|
|
39
37
|
readonly search: string;
|
|
38
|
+
/** URL fragment including the leading hash symbol. */
|
|
39
|
+
readonly hash: string;
|
|
40
40
|
/** state associated with the entry. */
|
|
41
41
|
readonly state: unknown;
|
|
42
42
|
}
|
|
@@ -52,21 +52,21 @@ interface HistoryLocation {
|
|
|
52
52
|
type HistoryScrollBehavior = 'auto' | 'preserve';
|
|
53
53
|
/** options for history write operations. */
|
|
54
54
|
interface HistoryNavigateOptions {
|
|
55
|
-
/** arbitrary metadata passed to history listeners. */
|
|
56
|
-
readonly info?: unknown;
|
|
57
|
-
/** scroll and focus behavior. */
|
|
58
|
-
readonly scroll?: HistoryScrollBehavior;
|
|
59
55
|
/** state data associated with the entry. */
|
|
60
56
|
readonly state?: unknown;
|
|
57
|
+
/** scroll and focus behavior. */
|
|
58
|
+
readonly scroll?: HistoryScrollBehavior;
|
|
59
|
+
/** arbitrary metadata passed to history listeners. */
|
|
60
|
+
readonly info?: unknown;
|
|
61
61
|
}
|
|
62
62
|
/** history update details dispatched to listeners. */
|
|
63
63
|
interface HistoryUpdate {
|
|
64
64
|
readonly action: HistoryAction;
|
|
65
|
-
/** metadata passed to the write operation. */
|
|
66
|
-
readonly info: unknown;
|
|
67
65
|
readonly location: HistoryLocation;
|
|
68
66
|
/** viewport behavior, which listeners apply. */
|
|
69
67
|
readonly scroll: HistoryScrollBehavior;
|
|
68
|
+
/** metadata passed to the write operation. */
|
|
69
|
+
readonly info: unknown;
|
|
70
70
|
}
|
|
71
71
|
/**
|
|
72
72
|
* history update callback subscriber.
|
|
@@ -76,37 +76,18 @@ interface HistoryUpdate {
|
|
|
76
76
|
type HistoryListener = (update: HistoryUpdate) => void | Promise<void>;
|
|
77
77
|
/** history manager interface modeled on the web navigation API. */
|
|
78
78
|
interface History {
|
|
79
|
+
/** active history location. */
|
|
80
|
+
readonly location: HistoryLocation;
|
|
79
81
|
/** whether a backward entry exists. */
|
|
80
82
|
readonly canGoBack: boolean;
|
|
81
83
|
/** whether a forward entry exists. */
|
|
82
84
|
readonly canGoForward: boolean;
|
|
83
|
-
/** active history location. */
|
|
84
|
-
readonly location: HistoryLocation;
|
|
85
|
-
/** navigates back one entry. */
|
|
86
|
-
back(): void;
|
|
87
|
-
/** detaches listeners and cleans up resources. */
|
|
88
|
-
dispose(): void;
|
|
89
85
|
/**
|
|
90
86
|
* lists all entries in the ledger.
|
|
91
87
|
*
|
|
92
88
|
* @returns array of history entries
|
|
93
89
|
*/
|
|
94
90
|
entries(): readonly HistoryEntry[];
|
|
95
|
-
/** navigates forward one entry. */
|
|
96
|
-
forward(): void;
|
|
97
|
-
/**
|
|
98
|
-
* navigates by a delta offset.
|
|
99
|
-
*
|
|
100
|
-
* @param delta number of entries to move
|
|
101
|
-
*/
|
|
102
|
-
go(delta: number): void;
|
|
103
|
-
/**
|
|
104
|
-
* subscribes to history updates.
|
|
105
|
-
*
|
|
106
|
-
* @param listener update callback
|
|
107
|
-
* @returns unsubscribe function
|
|
108
|
-
*/
|
|
109
|
-
listen(listener: HistoryListener): () => void;
|
|
110
91
|
/**
|
|
111
92
|
* pushes a new history entry.
|
|
112
93
|
*
|
|
@@ -121,6 +102,16 @@ interface History {
|
|
|
121
102
|
* @param options navigation options
|
|
122
103
|
*/
|
|
123
104
|
replace(to: string, options?: HistoryNavigateOptions): void;
|
|
105
|
+
/**
|
|
106
|
+
* rewrites the state of the active entry in place, without navigating.
|
|
107
|
+
*
|
|
108
|
+
* the entry keeps its id, key and URL, so no revision is minted and the user's place on the page is left
|
|
109
|
+
* alone. listeners are notified with the `update` action, since the caller is only ever one of them.
|
|
110
|
+
*
|
|
111
|
+
* @param state state data to store on the entry, replacing whatever is there
|
|
112
|
+
* @throws when the state cannot be structured-cloned
|
|
113
|
+
*/
|
|
114
|
+
updateState(state: unknown): void;
|
|
124
115
|
/**
|
|
125
116
|
* navigates to an existing entry by key.
|
|
126
117
|
*
|
|
@@ -129,15 +120,24 @@ interface History {
|
|
|
129
120
|
*/
|
|
130
121
|
traverseTo(key: string): Promise<void>;
|
|
131
122
|
/**
|
|
132
|
-
*
|
|
123
|
+
* navigates by a delta offset.
|
|
133
124
|
*
|
|
134
|
-
*
|
|
135
|
-
|
|
125
|
+
* @param delta number of entries to move
|
|
126
|
+
*/
|
|
127
|
+
go(delta: number): void;
|
|
128
|
+
/** navigates back one entry. */
|
|
129
|
+
back(): void;
|
|
130
|
+
/** navigates forward one entry. */
|
|
131
|
+
forward(): void;
|
|
132
|
+
/**
|
|
133
|
+
* subscribes to history updates.
|
|
136
134
|
*
|
|
137
|
-
* @param
|
|
138
|
-
* @
|
|
135
|
+
* @param listener update callback
|
|
136
|
+
* @returns unsubscribe function
|
|
139
137
|
*/
|
|
140
|
-
|
|
138
|
+
listen(listener: HistoryListener): () => void;
|
|
139
|
+
/** detaches listeners and cleans up resources. */
|
|
140
|
+
dispose(): void;
|
|
141
141
|
}
|
|
142
142
|
//#endregion
|
|
143
143
|
export { HistoryLocation as a, HistoryUpdate as c, HistoryListener as i, HistoryAction as n, HistoryNavigateOptions as o, HistoryEntry as r, HistoryScrollBehavior as s, History as t };
|
|
@@ -14,17 +14,17 @@ const parsePath = (to) => {
|
|
|
14
14
|
pathname = pathname.slice(0, searchIndex);
|
|
15
15
|
}
|
|
16
16
|
return {
|
|
17
|
-
hash,
|
|
18
17
|
pathname,
|
|
19
|
-
search
|
|
18
|
+
search,
|
|
19
|
+
hash
|
|
20
20
|
};
|
|
21
21
|
};
|
|
22
22
|
const resolvePath = (to, base) => {
|
|
23
23
|
const resolved = new URL(to, `http://_${createPath(base)}`);
|
|
24
24
|
return {
|
|
25
|
-
hash: resolved.hash,
|
|
26
25
|
pathname: resolved.pathname,
|
|
27
|
-
search: resolved.search
|
|
26
|
+
search: resolved.search,
|
|
27
|
+
hash: resolved.hash
|
|
28
28
|
};
|
|
29
29
|
};
|
|
30
30
|
const OUTSIDE_PCHAR = /[^\w!$&'()*+,\-.:;=@~]+/gu;
|
|
@@ -47,7 +47,7 @@ const decodeRemainder = (raw) => raw.split("/").map(decodeURIComponent).join("/"
|
|
|
47
47
|
const encodeRemainder = (value) => {
|
|
48
48
|
return value.split("/").map((segment) => encodeSegment(segment)).join("/");
|
|
49
49
|
};
|
|
50
|
-
const createPath = ({
|
|
50
|
+
const createPath = ({ pathname, search, hash }) => {
|
|
51
51
|
let out = pathname || "/";
|
|
52
52
|
if (search && search !== "?") out += search.startsWith("?") ? search : `?${search}`;
|
|
53
53
|
if (hash && hash !== "#") out += hash.startsWith("#") ? hash : `#${hash}`;
|
package/package.json
CHANGED
|
@@ -1,69 +1,70 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
2
|
+
"name": "@oomfware/stacker",
|
|
3
|
+
"version": "0.2.4",
|
|
4
|
+
"description": "type-safe stack router for React",
|
|
5
|
+
"license": "0BSD",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://codeberg.org/oomfware/stacker"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist/"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"types": "./dist/index.d.mts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
|
+
"default": "./dist/index.mjs"
|
|
19
|
+
},
|
|
20
|
+
"./testing": {
|
|
21
|
+
"types": "./dist/testing.d.mts",
|
|
22
|
+
"default": "./dist/testing.mjs"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "oxlint && tsdown",
|
|
31
|
+
"dev": "tsdown --watch",
|
|
32
|
+
"fmt": "oxfmt",
|
|
33
|
+
"lint": "oxlint",
|
|
34
|
+
"play": "vite",
|
|
35
|
+
"prepublishOnly": "pnpm run build",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"test:types": "vitest run --typecheck.only",
|
|
38
|
+
"test:watch": "vitest",
|
|
39
|
+
"typecheck": "tsc -b"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@mary-ext/simple-event-emitter": "^1.0.2"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@testing-library/dom": "^10.4.1",
|
|
46
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
47
|
+
"@testing-library/react": "^16.3.2",
|
|
48
|
+
"@types/node": "^26.1.1",
|
|
49
|
+
"@types/react": "^19.2.17",
|
|
50
|
+
"@types/react-dom": "^19.2.3",
|
|
51
|
+
"@vitejs/plugin-react": "^6.0.3",
|
|
52
|
+
"@vitest/browser": "^4.1.10",
|
|
53
|
+
"@vitest/browser-playwright": "^4.1.10",
|
|
54
|
+
"bumpp": "^11.1.0",
|
|
55
|
+
"oxfmt": "^0.59.0",
|
|
56
|
+
"oxlint": "^1.74.0",
|
|
57
|
+
"oxlint-tsgolint": "^0.25.0",
|
|
58
|
+
"playwright": "^1.61.1",
|
|
59
|
+
"react": "^19.2.7",
|
|
60
|
+
"react-dom": "^19.2.7",
|
|
61
|
+
"tsdown": "^0.22.9",
|
|
62
|
+
"typescript": "^7.0.2",
|
|
63
|
+
"vite": "^8.1.5",
|
|
64
|
+
"vitest": "^4.1.10"
|
|
65
|
+
},
|
|
66
|
+
"peerDependencies": {
|
|
67
|
+
"react": ">=19.0.0",
|
|
68
|
+
"react-dom": ">=19.0.0"
|
|
69
|
+
}
|
|
70
|
+
}
|