@oomfware/stacker 0.2.3 → 0.2.5
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 +16 -16
- package/dist/index.d.mts +73 -95
- package/dist/index.mjs +238 -125
- package/dist/testing.d.mts +5 -7
- package/dist/testing.mjs +19 -20
- package/dist/{types-kax-GwM6.d.mts → types-BUru23Cr.d.mts} +41 -43
- package/dist/{url-DerpiRjh.mjs → url-0EHjozDe.mjs} +6 -7
- package/package.json +69 -68
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
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-0EHjozDe.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";
|
|
5
|
-
|
|
5
|
+
// src/codec.ts
|
|
6
6
|
const OPTIONAL = Symbol("stacker.optional");
|
|
7
7
|
const DEFAULT = Symbol("stacker.default");
|
|
8
8
|
/**
|
|
@@ -103,8 +103,7 @@ const isOptional = (codec) => {
|
|
|
103
103
|
const getDefault = (codec) => {
|
|
104
104
|
return DEFAULT in codec ? [codec[DEFAULT]] : void 0;
|
|
105
105
|
};
|
|
106
|
-
|
|
107
|
-
//#region src/routes.ts
|
|
106
|
+
// src/routes.ts
|
|
108
107
|
/**
|
|
109
108
|
* defines a navigable leaf route.
|
|
110
109
|
*
|
|
@@ -185,9 +184,9 @@ const defineRoutes = (tree) => {
|
|
|
185
184
|
kind: node.kind,
|
|
186
185
|
id,
|
|
187
186
|
key,
|
|
188
|
-
node,
|
|
187
|
+
path: node.kind === "route" ? node.path : void 0,
|
|
189
188
|
params,
|
|
190
|
-
|
|
189
|
+
node
|
|
191
190
|
};
|
|
192
191
|
ids.add(id);
|
|
193
192
|
const chain = [...parentChain, resolved];
|
|
@@ -210,12 +209,12 @@ const defineRoutes = (tree) => {
|
|
|
210
209
|
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
210
|
}
|
|
212
211
|
leaves.set(key, {
|
|
213
|
-
chain,
|
|
214
|
-
leaf: node,
|
|
215
212
|
name: key,
|
|
216
|
-
params,
|
|
217
213
|
path: node.path,
|
|
218
|
-
|
|
214
|
+
params,
|
|
215
|
+
query,
|
|
216
|
+
leaf: node,
|
|
217
|
+
chain
|
|
219
218
|
});
|
|
220
219
|
}
|
|
221
220
|
};
|
|
@@ -225,8 +224,7 @@ const defineRoutes = (tree) => {
|
|
|
225
224
|
tree
|
|
226
225
|
};
|
|
227
226
|
};
|
|
228
|
-
|
|
229
|
-
//#region src/match.ts
|
|
227
|
+
// src/match.ts
|
|
230
228
|
/**
|
|
231
229
|
* looks up the nearest metadata value for a key on a matched chain, searching leaf-first.
|
|
232
230
|
*
|
|
@@ -259,16 +257,16 @@ const compile = (path) => {
|
|
|
259
257
|
if (splat !== void 0) {
|
|
260
258
|
paramNames.push(splat);
|
|
261
259
|
return {
|
|
262
|
-
|
|
260
|
+
regex: new RegExp(`^${pattern}(?:/(.*))?$`),
|
|
263
261
|
paramNames,
|
|
264
|
-
|
|
262
|
+
hasSplat: true
|
|
265
263
|
};
|
|
266
264
|
}
|
|
267
265
|
const suffix = pattern.endsWith("/") ? "" : "/?";
|
|
268
266
|
return {
|
|
269
|
-
|
|
267
|
+
regex: new RegExp(`^${pattern}${suffix}$`),
|
|
270
268
|
paramNames,
|
|
271
|
-
|
|
269
|
+
hasSplat: false
|
|
272
270
|
};
|
|
273
271
|
};
|
|
274
272
|
/** matches URLs in declaration order against a compiled route registry. */
|
|
@@ -276,22 +274,22 @@ var Matcher = class {
|
|
|
276
274
|
#compiled;
|
|
277
275
|
constructor(registry) {
|
|
278
276
|
this.#compiled = [...registry.leaves.values()].map((leaf) => {
|
|
279
|
-
const {
|
|
277
|
+
const { regex, paramNames, hasSplat } = compile(leaf.path);
|
|
280
278
|
return {
|
|
281
|
-
hasSplat,
|
|
282
279
|
leaf,
|
|
280
|
+
regex,
|
|
283
281
|
paramNames,
|
|
284
|
-
|
|
282
|
+
hasSplat
|
|
285
283
|
};
|
|
286
284
|
});
|
|
287
285
|
}
|
|
288
286
|
match(pathname, search = "", hash = "") {
|
|
289
287
|
const rawSearch = new URLSearchParams(search);
|
|
290
288
|
const ctx = {
|
|
291
|
-
hash,
|
|
292
289
|
pathname,
|
|
293
|
-
|
|
294
|
-
|
|
290
|
+
search,
|
|
291
|
+
hash,
|
|
292
|
+
rawSearch
|
|
295
293
|
};
|
|
296
294
|
for (const compiled of this.#compiled) {
|
|
297
295
|
const { leaf, regex } = compiled;
|
|
@@ -315,14 +313,14 @@ var Matcher = class {
|
|
|
315
313
|
};
|
|
316
314
|
});
|
|
317
315
|
return {
|
|
318
|
-
chain,
|
|
319
|
-
leaf,
|
|
320
316
|
name: leaf.name,
|
|
321
|
-
params: chain.at(-1)?.params ?? {}
|
|
317
|
+
params: chain.at(-1)?.params ?? {},
|
|
318
|
+
leaf,
|
|
319
|
+
chain
|
|
322
320
|
};
|
|
323
321
|
}
|
|
324
322
|
}
|
|
325
|
-
#rawValues({
|
|
323
|
+
#rawValues({ paramNames, hasSplat }, execed) {
|
|
326
324
|
const splatIndex = hasSplat ? paramNames.length - 1 : -1;
|
|
327
325
|
const raw = {};
|
|
328
326
|
for (const [i, name] of paramNames.entries()) {
|
|
@@ -376,8 +374,9 @@ var Matcher = class {
|
|
|
376
374
|
return out;
|
|
377
375
|
}
|
|
378
376
|
};
|
|
379
|
-
|
|
380
|
-
|
|
377
|
+
// src/webkit.ts
|
|
378
|
+
const isWebKit = (userAgent) => /AppleWebKit\//.test(userAgent) && !/(?:Chrome|Chromium)\//.test(userAgent);
|
|
379
|
+
// src/history/navigation.ts
|
|
381
380
|
const ENVELOPE = Symbol("stacker.historyInfo");
|
|
382
381
|
const isEnvelope = (value) => typeof value === "object" && value !== null && ENVELOPE in value;
|
|
383
382
|
const toAction = (type) => {
|
|
@@ -403,10 +402,12 @@ var NavigationHistory = class {
|
|
|
403
402
|
#win;
|
|
404
403
|
#disposed = false;
|
|
405
404
|
#location;
|
|
405
|
+
#webkit;
|
|
406
406
|
constructor(options = {}) {
|
|
407
407
|
this.#win = options.window ?? window;
|
|
408
408
|
this.#nav = this.#win.navigation;
|
|
409
409
|
this.#ignore = options.ignore;
|
|
410
|
+
this.#webkit = isWebKit(this.#win.navigator.userAgent);
|
|
410
411
|
this.#onError = options.onError ?? ((error) => {
|
|
411
412
|
this.#win.reportError(error);
|
|
412
413
|
});
|
|
@@ -414,6 +415,7 @@ var NavigationHistory = class {
|
|
|
414
415
|
this.#nav.addEventListener("currententrychange", this.#onCurrentEntryChange);
|
|
415
416
|
this.#nav.addEventListener("navigate", this.#onNavigate);
|
|
416
417
|
this.#nav.addEventListener("navigateerror", this.#onNavigateError);
|
|
418
|
+
if (this.#webkit) this.#win.addEventListener("popstate", this.#onPopState);
|
|
417
419
|
this.#win.history.scrollRestoration = "manual";
|
|
418
420
|
}
|
|
419
421
|
get location() {
|
|
@@ -427,12 +429,12 @@ var NavigationHistory = class {
|
|
|
427
429
|
}
|
|
428
430
|
entries() {
|
|
429
431
|
return this.#nav.entries().map((entry) => ({
|
|
432
|
+
key: entry.key,
|
|
430
433
|
id: entry.id,
|
|
431
434
|
index: entry.index,
|
|
432
|
-
|
|
435
|
+
url: toRelative(entry.url),
|
|
433
436
|
sameDocument: entry.sameDocument,
|
|
434
|
-
state: entry.getState()
|
|
435
|
-
url: toRelative(entry.url)
|
|
437
|
+
state: entry.getState()
|
|
436
438
|
}));
|
|
437
439
|
}
|
|
438
440
|
push(to, options = {}) {
|
|
@@ -445,6 +447,7 @@ var NavigationHistory = class {
|
|
|
445
447
|
this.#nav.updateCurrentEntry({ state });
|
|
446
448
|
}
|
|
447
449
|
traverseTo(key) {
|
|
450
|
+
if (this.#webkit) return this.#traverseLegacy(key);
|
|
448
451
|
return this.#track(this.#nav.traverseTo(key));
|
|
449
452
|
}
|
|
450
453
|
go(delta) {
|
|
@@ -454,10 +457,20 @@ var NavigationHistory = class {
|
|
|
454
457
|
this.#report(this.traverseTo(target.key));
|
|
455
458
|
}
|
|
456
459
|
back() {
|
|
457
|
-
if (this.#nav.canGoBack)
|
|
460
|
+
if (!this.#nav.canGoBack) return;
|
|
461
|
+
if (this.#webkit) {
|
|
462
|
+
this.#win.history.back();
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
this.#report(this.#track(this.#nav.back()));
|
|
458
466
|
}
|
|
459
467
|
forward() {
|
|
460
|
-
if (this.#nav.canGoForward)
|
|
468
|
+
if (!this.#nav.canGoForward) return;
|
|
469
|
+
if (this.#webkit) {
|
|
470
|
+
this.#win.history.forward();
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
this.#report(this.#track(this.#nav.forward()));
|
|
461
474
|
}
|
|
462
475
|
listen(listener) {
|
|
463
476
|
this.#listeners.add(listener);
|
|
@@ -470,8 +483,21 @@ var NavigationHistory = class {
|
|
|
470
483
|
this.#nav.removeEventListener("currententrychange", this.#onCurrentEntryChange);
|
|
471
484
|
this.#nav.removeEventListener("navigate", this.#onNavigate);
|
|
472
485
|
this.#nav.removeEventListener("navigateerror", this.#onNavigateError);
|
|
486
|
+
this.#win.removeEventListener("popstate", this.#onPopState);
|
|
473
487
|
this.#listeners.clear();
|
|
474
488
|
}
|
|
489
|
+
async #traverseLegacy(key) {
|
|
490
|
+
const target = this.#nav.entries().find((entry) => entry.key === key);
|
|
491
|
+
if (target === void 0) throw new DOMException(`no history entry with the key '${key}'`, "InvalidStateError");
|
|
492
|
+
const delta = target.index - this.#location.index;
|
|
493
|
+
if (delta !== 0) this.#win.history.go(delta);
|
|
494
|
+
}
|
|
495
|
+
#onPopState = () => {
|
|
496
|
+
const entry = this.#nav.currentEntry;
|
|
497
|
+
if (entry !== null && entry.key === this.#location.key) return;
|
|
498
|
+
this.dispose();
|
|
499
|
+
this.#win.location.reload();
|
|
500
|
+
};
|
|
475
501
|
#track(result) {
|
|
476
502
|
result.finished?.catch(() => {});
|
|
477
503
|
return result.committed?.then(() => void 0) ?? Promise.resolve();
|
|
@@ -492,43 +518,65 @@ var NavigationHistory = class {
|
|
|
492
518
|
const url = new URL(to, this.#win.location.href).href;
|
|
493
519
|
const envelope = {
|
|
494
520
|
[ENVELOPE]: true,
|
|
495
|
-
|
|
496
|
-
|
|
521
|
+
scroll: options.scroll ?? "auto",
|
|
522
|
+
info: options.info
|
|
497
523
|
};
|
|
498
524
|
this.#report(this.#track(this.#nav.navigate(url, {
|
|
499
|
-
|
|
525
|
+
state: options.state ?? null,
|
|
500
526
|
info: envelope,
|
|
501
|
-
|
|
527
|
+
history
|
|
502
528
|
})));
|
|
503
529
|
}
|
|
504
530
|
#read() {
|
|
505
531
|
const entry = this.#nav.currentEntry;
|
|
506
532
|
if (entry === null) throw new Error("stacker: the navigation API has no current entry (an opaque origin?)");
|
|
507
|
-
const {
|
|
533
|
+
const { pathname, search, hash } = new URL(entry.url ?? this.#win.location.href);
|
|
508
534
|
return {
|
|
509
|
-
|
|
535
|
+
key: entry.key,
|
|
510
536
|
id: entry.id,
|
|
511
537
|
index: entry.index,
|
|
512
|
-
key: entry.key,
|
|
513
538
|
pathname,
|
|
514
539
|
search,
|
|
540
|
+
hash,
|
|
515
541
|
state: entry.getState()
|
|
516
542
|
};
|
|
517
543
|
}
|
|
518
544
|
#onCurrentEntryChange = (event) => {
|
|
545
|
+
if (event.navigationType === "traverse" && this.#webkit) {
|
|
546
|
+
this.#report(this.#traverseUnintercepted());
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
519
549
|
if (event.navigationType !== null) return;
|
|
520
550
|
this.#location = this.#read();
|
|
521
551
|
this.#report(this.#notify({
|
|
522
552
|
action: "update",
|
|
523
|
-
info: void 0,
|
|
524
553
|
location: this.#location,
|
|
525
|
-
scroll: "preserve"
|
|
554
|
+
scroll: "preserve",
|
|
555
|
+
info: void 0
|
|
526
556
|
}));
|
|
527
557
|
};
|
|
558
|
+
async #traverseUnintercepted() {
|
|
559
|
+
const doc = this.#win.document;
|
|
560
|
+
const focused = doc.activeElement;
|
|
561
|
+
this.#location = this.#read();
|
|
562
|
+
await this.#notify({
|
|
563
|
+
action: "traverse",
|
|
564
|
+
location: this.#location,
|
|
565
|
+
scroll: "auto",
|
|
566
|
+
info: void 0
|
|
567
|
+
});
|
|
568
|
+
if (doc.activeElement !== focused) return;
|
|
569
|
+
const body = doc.body;
|
|
570
|
+
const hadTabIndex = body.hasAttribute("tabindex");
|
|
571
|
+
if (!hadTabIndex) body.tabIndex = -1;
|
|
572
|
+
body.focus({ preventScroll: true });
|
|
573
|
+
if (!hadTabIndex) body.removeAttribute("tabindex");
|
|
574
|
+
}
|
|
528
575
|
#onNavigate = (event) => {
|
|
529
576
|
const action = toAction(event.navigationType);
|
|
530
577
|
if (action === void 0 || !event.canIntercept) return;
|
|
531
578
|
if (event.downloadRequest !== null || event.formData !== null) return;
|
|
579
|
+
if (action === "traverse" && this.#webkit) return;
|
|
532
580
|
if (action !== "traverse" && this.#ignore?.(new URL(event.destination.url))) return;
|
|
533
581
|
const envelope = isEnvelope(event.info) ? event.info : void 0;
|
|
534
582
|
const info = envelope === void 0 ? event.info : envelope.info;
|
|
@@ -541,9 +589,9 @@ var NavigationHistory = class {
|
|
|
541
589
|
this.#location = this.#read();
|
|
542
590
|
await this.#notify({
|
|
543
591
|
action,
|
|
544
|
-
info,
|
|
545
592
|
location: this.#location,
|
|
546
|
-
scroll
|
|
593
|
+
scroll,
|
|
594
|
+
info
|
|
547
595
|
});
|
|
548
596
|
}
|
|
549
597
|
});
|
|
@@ -555,11 +603,10 @@ var NavigationHistory = class {
|
|
|
555
603
|
};
|
|
556
604
|
const toRelative = (url) => {
|
|
557
605
|
if (url === null) return null;
|
|
558
|
-
const {
|
|
606
|
+
const { pathname, search, hash } = new URL(url);
|
|
559
607
|
return `${pathname}${search}${hash}`;
|
|
560
608
|
};
|
|
561
|
-
|
|
562
|
-
//#region src/lazy.ts
|
|
609
|
+
// src/lazy.ts
|
|
563
610
|
const noop = () => {};
|
|
564
611
|
/**
|
|
565
612
|
* defines a lazily-loaded component that can be preloaded.
|
|
@@ -597,8 +644,7 @@ const preloadAll = (chain) => {
|
|
|
597
644
|
}
|
|
598
645
|
return Promise.all(pending).then(noop);
|
|
599
646
|
};
|
|
600
|
-
|
|
601
|
-
//#region src/build.ts
|
|
647
|
+
// src/build.ts
|
|
602
648
|
const SEGMENT = /:([A-Za-z_]\w*)/g;
|
|
603
649
|
var Builder = class {
|
|
604
650
|
#registry;
|
|
@@ -643,14 +689,13 @@ var Builder = class {
|
|
|
643
689
|
}
|
|
644
690
|
const query = search.toString();
|
|
645
691
|
return createPath({
|
|
646
|
-
hash: "",
|
|
647
692
|
pathname,
|
|
648
|
-
search: query ? `?${query}` : ""
|
|
693
|
+
search: query ? `?${query}` : "",
|
|
694
|
+
hash: ""
|
|
649
695
|
});
|
|
650
696
|
}
|
|
651
697
|
};
|
|
652
|
-
|
|
653
|
-
//#region src/cache.ts
|
|
698
|
+
// src/cache.ts
|
|
654
699
|
/** selects the active entry and recent backward entries for caching. */
|
|
655
700
|
const computeCachedKeys = (entries, activeKey, recency, policy) => {
|
|
656
701
|
const cached = /* @__PURE__ */ new Set();
|
|
@@ -669,8 +714,7 @@ const computeCachedKeys = (entries, activeKey, recency, policy) => {
|
|
|
669
714
|
for (const entry of [...backward.values()].toSorted((a, b) => b.index - a.index)) take(entry.key);
|
|
670
715
|
return cached;
|
|
671
716
|
};
|
|
672
|
-
|
|
673
|
-
//#region src/keys.ts
|
|
717
|
+
// src/keys.ts
|
|
674
718
|
const SEP = "\0";
|
|
675
719
|
/**
|
|
676
720
|
* serializes path parameters into a stable, sorted, percent-encoded string.
|
|
@@ -697,8 +741,7 @@ const instanceKey = (matched, entryKey) => {
|
|
|
697
741
|
if (resolved.kind === "layout") return `${resolved.id}${SEP}${canonical(matched)}`;
|
|
698
742
|
return (resolved.node.kind === "route" ? resolved.node.type ?? "page" : "page") === "singleton" ? `${resolved.id}${SEP}${canonical(matched)}` : `${resolved.id}${SEP}entry:${entryKey}`;
|
|
699
743
|
};
|
|
700
|
-
|
|
701
|
-
//#region src/view-model.ts
|
|
744
|
+
// src/view-model.ts
|
|
702
745
|
/**
|
|
703
746
|
* builds the active view and computes which entries and instances to prune.
|
|
704
747
|
*
|
|
@@ -715,10 +758,10 @@ const computeView = (cached, pinned, activeKey) => {
|
|
|
715
758
|
let inst = byKey.get(key);
|
|
716
759
|
if (inst === void 0) {
|
|
717
760
|
inst = {
|
|
718
|
-
children: [],
|
|
719
761
|
instanceKey: key,
|
|
720
762
|
node: matched.node,
|
|
721
|
-
params: matched.params
|
|
763
|
+
params: matched.params,
|
|
764
|
+
children: []
|
|
722
765
|
};
|
|
723
766
|
byKey.set(key, inst);
|
|
724
767
|
pool.push(inst);
|
|
@@ -731,12 +774,11 @@ const computeView = (cached, pinned, activeKey) => {
|
|
|
731
774
|
const active = cached.find((entry) => entry.key === activeKey);
|
|
732
775
|
if (active === void 0) throw new Error(`stacker: active entry '${activeKey}' is not in the cached set`);
|
|
733
776
|
return {
|
|
734
|
-
|
|
735
|
-
|
|
777
|
+
roots,
|
|
778
|
+
activePath: active.match.chain.map((matched) => instanceKey(matched, active.key))
|
|
736
779
|
};
|
|
737
780
|
};
|
|
738
|
-
|
|
739
|
-
//#region src/router.ts
|
|
781
|
+
// src/router.ts
|
|
740
782
|
const DefaultNotFound = () => null;
|
|
741
783
|
const TOP = {
|
|
742
784
|
x: 0,
|
|
@@ -768,6 +810,8 @@ var Router = class {
|
|
|
768
810
|
#commitWaiters = [];
|
|
769
811
|
#viewAttached = false;
|
|
770
812
|
#pendingScroll = null;
|
|
813
|
+
#guardScrolls;
|
|
814
|
+
#stopScrollGuard = null;
|
|
771
815
|
constructor(options) {
|
|
772
816
|
this.#history = options.history;
|
|
773
817
|
this.#routes = options.routes;
|
|
@@ -778,6 +822,7 @@ var Router = class {
|
|
|
778
822
|
this.#notFound = makeNotFound(options.notFound ?? DefaultNotFound);
|
|
779
823
|
this.#pins = new Map((options.pins ?? []).map((name) => [name, this.#makePin(name)]));
|
|
780
824
|
this.#win = options.window ?? (typeof window === "undefined" ? null : window);
|
|
825
|
+
this.#guardScrolls = this.#win !== null && isWebKit(this.#win.navigator.userAgent);
|
|
781
826
|
const location = this.#history.location;
|
|
782
827
|
this.#activeKey = location.key;
|
|
783
828
|
this.#record(location, TOP);
|
|
@@ -787,21 +832,31 @@ var Router = class {
|
|
|
787
832
|
this.#updates.emit();
|
|
788
833
|
return;
|
|
789
834
|
}
|
|
790
|
-
const win = this.#win;
|
|
791
|
-
const
|
|
792
|
-
|
|
835
|
+
const win = scroll === "auto" ? this.#win : null;
|
|
836
|
+
const departed = win === null ? null : {
|
|
837
|
+
x: win.scrollX,
|
|
838
|
+
y: win.scrollY
|
|
839
|
+
};
|
|
840
|
+
if (departed !== null) {
|
|
793
841
|
const outgoing = this.#entries.get(this.#activeKey);
|
|
794
|
-
if (outgoing !== void 0) outgoing.scrollPos =
|
|
795
|
-
x: win.scrollX,
|
|
796
|
-
y: win.scrollY
|
|
797
|
-
};
|
|
842
|
+
if (outgoing !== void 0) outgoing.scrollPos = departed;
|
|
798
843
|
}
|
|
799
844
|
this.#prune();
|
|
800
845
|
const target = (action === "traverse" ? this.#entries.get(next.key)?.scrollPos : void 0) ?? TOP;
|
|
801
846
|
this.#record(next, target);
|
|
802
847
|
this.#activeKey = next.key;
|
|
803
848
|
this.#view = this.#recompute();
|
|
804
|
-
|
|
849
|
+
if (win !== null && departed !== null) {
|
|
850
|
+
this.#pendingScroll = {
|
|
851
|
+
target,
|
|
852
|
+
guard: this.#guardScrolls && Math.abs(departed.y - target.y) >= 1
|
|
853
|
+
};
|
|
854
|
+
win.scrollTo({
|
|
855
|
+
behavior: "instant",
|
|
856
|
+
left: target.x,
|
|
857
|
+
top: target.y
|
|
858
|
+
});
|
|
859
|
+
} else this.#pendingScroll = null;
|
|
805
860
|
const committed = this.#awaitCommit();
|
|
806
861
|
this.#updates.emit();
|
|
807
862
|
await committed;
|
|
@@ -870,7 +925,7 @@ var Router = class {
|
|
|
870
925
|
* @returns the target it resolves to, or undefined when no route matches
|
|
871
926
|
*/
|
|
872
927
|
match(to) {
|
|
873
|
-
const {
|
|
928
|
+
const { pathname, search, hash } = resolvePath(to, this.#history.location);
|
|
874
929
|
const match = this.#matcher.match(pathname, search, hash);
|
|
875
930
|
return match === void 0 ? void 0 : toTarget(match);
|
|
876
931
|
}
|
|
@@ -880,7 +935,7 @@ var Router = class {
|
|
|
880
935
|
* @param options where to go, and how to get there
|
|
881
936
|
*/
|
|
882
937
|
navigate(options) {
|
|
883
|
-
const { replace = false,
|
|
938
|
+
const { to, replace = false, ...rest } = options;
|
|
884
939
|
const url = typeof to === "string" ? to : this.#builder.build(to);
|
|
885
940
|
if (replace) this.#history.replace(url, rest);
|
|
886
941
|
else this.#history.push(url, rest);
|
|
@@ -906,7 +961,7 @@ var Router = class {
|
|
|
906
961
|
*/
|
|
907
962
|
popTo(to) {
|
|
908
963
|
const url = typeof to === "string" ? to : this.#builder.build(to);
|
|
909
|
-
const {
|
|
964
|
+
const { pathname, search, hash } = resolvePath(url, this.#history.location);
|
|
910
965
|
const wantedMatch = this.#matcher.match(pathname, search, hash);
|
|
911
966
|
if (wantedMatch !== void 0) {
|
|
912
967
|
const wanted = this.#canonical(wantedMatch);
|
|
@@ -953,13 +1008,13 @@ var Router = class {
|
|
|
953
1008
|
}
|
|
954
1009
|
const query = search.toString();
|
|
955
1010
|
const to = createPath({
|
|
956
|
-
hash: location.hash,
|
|
957
1011
|
pathname: location.pathname,
|
|
958
|
-
search: query ? `?${query}` : ""
|
|
1012
|
+
search: query ? `?${query}` : "",
|
|
1013
|
+
hash: location.hash
|
|
959
1014
|
});
|
|
960
1015
|
this.#history.replace(to, {
|
|
961
|
-
|
|
962
|
-
|
|
1016
|
+
state: location.state,
|
|
1017
|
+
scroll: "preserve"
|
|
963
1018
|
});
|
|
964
1019
|
}
|
|
965
1020
|
/**
|
|
@@ -1016,16 +1071,79 @@ var Router = class {
|
|
|
1016
1071
|
this.#pendingScroll = null;
|
|
1017
1072
|
win.scrollTo({
|
|
1018
1073
|
behavior: "instant",
|
|
1019
|
-
left: pending.x,
|
|
1020
|
-
top: pending.y
|
|
1074
|
+
left: pending.target.x,
|
|
1075
|
+
top: pending.target.y
|
|
1021
1076
|
});
|
|
1077
|
+
if (pending.guard) this.#guardScroll(win);
|
|
1022
1078
|
}
|
|
1023
1079
|
/** detaches history listeners and disposes resources. */
|
|
1024
1080
|
dispose() {
|
|
1081
|
+
this.#stopScrollGuard?.();
|
|
1025
1082
|
this.#unlisten();
|
|
1026
1083
|
this.#history.dispose();
|
|
1027
1084
|
this.notifyCommit();
|
|
1028
1085
|
}
|
|
1086
|
+
#guardScroll(win) {
|
|
1087
|
+
this.#stopScrollGuard?.();
|
|
1088
|
+
const controller = new AbortController();
|
|
1089
|
+
const { signal } = controller;
|
|
1090
|
+
const stop = () => {
|
|
1091
|
+
controller.abort();
|
|
1092
|
+
};
|
|
1093
|
+
const timer = setTimeout(stop, 1e3);
|
|
1094
|
+
this.#stopScrollGuard = stop;
|
|
1095
|
+
signal.addEventListener("abort", () => {
|
|
1096
|
+
clearTimeout(timer);
|
|
1097
|
+
if (this.#stopScrollGuard === stop) this.#stopScrollGuard = null;
|
|
1098
|
+
});
|
|
1099
|
+
const root = win.document.scrollingElement ?? win.document.documentElement;
|
|
1100
|
+
const read = () => ({
|
|
1101
|
+
x: Math.max(0, win.scrollX),
|
|
1102
|
+
y: Math.max(0, win.scrollY)
|
|
1103
|
+
});
|
|
1104
|
+
let expected = read();
|
|
1105
|
+
const scrollTo = win.scrollTo.bind(win);
|
|
1106
|
+
for (const method of [
|
|
1107
|
+
"scroll",
|
|
1108
|
+
"scrollTo",
|
|
1109
|
+
"scrollBy"
|
|
1110
|
+
]) {
|
|
1111
|
+
const original = win[method];
|
|
1112
|
+
win[method] = function(...args) {
|
|
1113
|
+
const [options] = args;
|
|
1114
|
+
if (typeof options === "object" && options !== null && "behavior" in options && options.behavior === "smooth") stop();
|
|
1115
|
+
Reflect.apply(original, this, args);
|
|
1116
|
+
expected = read();
|
|
1117
|
+
};
|
|
1118
|
+
signal.addEventListener("abort", () => {
|
|
1119
|
+
win[method] = original;
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
win.addEventListener("scroll", () => {
|
|
1123
|
+
const current = read();
|
|
1124
|
+
if (Math.abs(current.y - expected.y) < 1 && Math.abs(current.x - expected.x) < 1) return;
|
|
1125
|
+
const maxY = Math.max(0, root.scrollHeight - root.clientHeight);
|
|
1126
|
+
if (expected.y > maxY && Math.abs(current.y - maxY) < 1) {
|
|
1127
|
+
expected = current;
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
scrollTo({
|
|
1131
|
+
behavior: "instant",
|
|
1132
|
+
left: expected.x,
|
|
1133
|
+
top: expected.y
|
|
1134
|
+
});
|
|
1135
|
+
}, { signal });
|
|
1136
|
+
for (const type of [
|
|
1137
|
+
"touchstart",
|
|
1138
|
+
"wheel",
|
|
1139
|
+
"keydown",
|
|
1140
|
+
"pointerdown"
|
|
1141
|
+
]) win.addEventListener(type, stop, {
|
|
1142
|
+
capture: true,
|
|
1143
|
+
passive: true,
|
|
1144
|
+
signal
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1029
1147
|
#active() {
|
|
1030
1148
|
const active = this.#entries.get(this.#activeKey);
|
|
1031
1149
|
if (active === void 0) throw new Error(`stacker: no warm entry for the active key '${this.#activeKey}'`);
|
|
@@ -1051,11 +1169,11 @@ var Router = class {
|
|
|
1051
1169
|
const match = this.#match(location);
|
|
1052
1170
|
preloadAll(match.leaf.chain);
|
|
1053
1171
|
const entry = {
|
|
1054
|
-
index: location.index,
|
|
1055
1172
|
key: location.key,
|
|
1173
|
+
index: location.index,
|
|
1056
1174
|
match,
|
|
1057
|
-
|
|
1058
|
-
|
|
1175
|
+
target: toTarget(match),
|
|
1176
|
+
scrollPos
|
|
1059
1177
|
};
|
|
1060
1178
|
this.#entries.set(location.key, entry);
|
|
1061
1179
|
this.#recency = [entry.key, ...this.#recency.filter((key) => key !== entry.key)];
|
|
@@ -1064,10 +1182,11 @@ var Router = class {
|
|
|
1064
1182
|
}
|
|
1065
1183
|
#recompute() {
|
|
1066
1184
|
const active = this.#active();
|
|
1067
|
-
const
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
}))
|
|
1185
|
+
const refs = [...this.#entries.values()].map((entry) => ({
|
|
1186
|
+
key: entry.key,
|
|
1187
|
+
index: entry.index
|
|
1188
|
+
}));
|
|
1189
|
+
const cachedKeys = computeCachedKeys(refs, active.key, this.#recency, { max: this.#max });
|
|
1071
1190
|
const cached = [];
|
|
1072
1191
|
for (const entry of this.#entries.values()) if (cachedKeys.has(entry.key)) cached.push({
|
|
1073
1192
|
key: entry.key,
|
|
@@ -1083,7 +1202,7 @@ var Router = class {
|
|
|
1083
1202
|
if (leaf === void 0) throw new Error(`stacker: pinned route '${name}' is not in the registry`);
|
|
1084
1203
|
if ((leaf.leaf.type ?? "page") !== "singleton") throw new Error(`stacker: pinned route '${name}' must be a singleton`);
|
|
1085
1204
|
if (Object.keys(leaf.params).length > 0) throw new Error(`stacker: pinned route '${name}' must have no path params`);
|
|
1086
|
-
const {
|
|
1205
|
+
const { pathname, search, hash } = parsePath(leaf.path);
|
|
1087
1206
|
const match = this.#matcher.match(pathname, search, hash);
|
|
1088
1207
|
if (match === void 0) throw new Error(`stacker: pinned route '${name}' (path '${leaf.path}') did not match`);
|
|
1089
1208
|
return {
|
|
@@ -1093,13 +1212,13 @@ var Router = class {
|
|
|
1093
1212
|
}
|
|
1094
1213
|
#notFoundMatch() {
|
|
1095
1214
|
return {
|
|
1215
|
+
name: this.#notFound.leaf.name,
|
|
1216
|
+
params: {},
|
|
1217
|
+
leaf: this.#notFound.leaf,
|
|
1096
1218
|
chain: [{
|
|
1097
1219
|
node: this.#notFound.node,
|
|
1098
1220
|
params: {}
|
|
1099
|
-
}]
|
|
1100
|
-
leaf: this.#notFound.leaf,
|
|
1101
|
-
name: this.#notFound.leaf.name,
|
|
1102
|
-
params: {}
|
|
1221
|
+
}]
|
|
1103
1222
|
};
|
|
1104
1223
|
}
|
|
1105
1224
|
};
|
|
@@ -1110,39 +1229,37 @@ const toTarget = (match) => ({
|
|
|
1110
1229
|
const makeNotFound = (component) => {
|
|
1111
1230
|
const leafNode = {
|
|
1112
1231
|
kind: "route",
|
|
1113
|
-
component,
|
|
1114
|
-
params: {},
|
|
1115
1232
|
path: "*",
|
|
1233
|
+
params: {},
|
|
1116
1234
|
query: {},
|
|
1117
|
-
type: "page"
|
|
1235
|
+
type: "page",
|
|
1236
|
+
component
|
|
1118
1237
|
};
|
|
1119
1238
|
const node = {
|
|
1120
1239
|
kind: "route",
|
|
1121
1240
|
id: " notFound",
|
|
1122
1241
|
key: " notFound",
|
|
1123
|
-
|
|
1242
|
+
path: "*",
|
|
1124
1243
|
params: {},
|
|
1125
|
-
|
|
1244
|
+
node: leafNode
|
|
1126
1245
|
};
|
|
1127
1246
|
return {
|
|
1128
1247
|
leaf: {
|
|
1129
|
-
chain: [node],
|
|
1130
|
-
leaf: leafNode,
|
|
1131
1248
|
name: " notFound",
|
|
1132
|
-
params: {},
|
|
1133
1249
|
path: "*",
|
|
1134
|
-
|
|
1250
|
+
params: {},
|
|
1251
|
+
query: {},
|
|
1252
|
+
leaf: leafNode,
|
|
1253
|
+
chain: [node]
|
|
1135
1254
|
},
|
|
1136
1255
|
node
|
|
1137
1256
|
};
|
|
1138
1257
|
};
|
|
1139
|
-
|
|
1140
|
-
//#region src/react/context.ts
|
|
1258
|
+
// src/react/context.ts
|
|
1141
1259
|
const RouterContext = createContext(null);
|
|
1142
1260
|
const ActiveChainContext = createContext([]);
|
|
1143
1261
|
const CurrentNodeContext = createContext(null);
|
|
1144
|
-
|
|
1145
|
-
//#region src/react/router-view.tsx
|
|
1262
|
+
// src/react/router-view.tsx
|
|
1146
1263
|
/**
|
|
1147
1264
|
* renders the current view tree of the router.
|
|
1148
1265
|
*
|
|
@@ -1181,11 +1298,11 @@ const ScrollRestorer = ({ router, view }) => {
|
|
|
1181
1298
|
const Pool = ({ nodes }) => {
|
|
1182
1299
|
const activePath = useContext(ActiveChainContext);
|
|
1183
1300
|
return /* @__PURE__ */ jsx(Fragment, { children: nodes.map((node) => /* @__PURE__ */ jsx(Branch, {
|
|
1184
|
-
|
|
1185
|
-
node
|
|
1301
|
+
node,
|
|
1302
|
+
focused: activePath.includes(node.instanceKey)
|
|
1186
1303
|
}, node.instanceKey)) });
|
|
1187
1304
|
};
|
|
1188
|
-
const Branch = ({
|
|
1305
|
+
const Branch = ({ node, focused }) => {
|
|
1189
1306
|
const resolved = node.node;
|
|
1190
1307
|
const Component = resolved.node.component;
|
|
1191
1308
|
const router = useContext(RouterContext);
|
|
@@ -1196,8 +1313,8 @@ const Branch = ({ focused, node }) => {
|
|
|
1196
1313
|
style: { display: focused ? "contents" : "none" },
|
|
1197
1314
|
children: /* @__PURE__ */ jsx(CurrentNodeContext, {
|
|
1198
1315
|
value: {
|
|
1199
|
-
|
|
1200
|
-
|
|
1316
|
+
node,
|
|
1317
|
+
focused
|
|
1201
1318
|
},
|
|
1202
1319
|
children: /* @__PURE__ */ jsx(Suspense, {
|
|
1203
1320
|
fallback: fallback ?? null,
|
|
@@ -1206,8 +1323,7 @@ const Branch = ({ focused, node }) => {
|
|
|
1206
1323
|
})
|
|
1207
1324
|
});
|
|
1208
1325
|
};
|
|
1209
|
-
|
|
1210
|
-
//#region src/react/outlet.tsx
|
|
1326
|
+
// src/react/outlet.tsx
|
|
1211
1327
|
/**
|
|
1212
1328
|
* renders the child branch pool within a layout component.
|
|
1213
1329
|
*
|
|
@@ -1218,8 +1334,7 @@ const Outlet = () => {
|
|
|
1218
1334
|
if (current === null) return null;
|
|
1219
1335
|
return /* @__PURE__ */ jsx(Pool, { nodes: current.node.children });
|
|
1220
1336
|
};
|
|
1221
|
-
|
|
1222
|
-
//#region src/react/hooks.ts
|
|
1337
|
+
// src/react/hooks.ts
|
|
1223
1338
|
/**
|
|
1224
1339
|
* gets the active router instance.
|
|
1225
1340
|
*
|
|
@@ -1301,6 +1416,11 @@ const createRouterHooks = (routes) => {
|
|
|
1301
1416
|
return router;
|
|
1302
1417
|
};
|
|
1303
1418
|
return {
|
|
1419
|
+
useRouter: useTypedRouter,
|
|
1420
|
+
useTarget() {
|
|
1421
|
+
const router = useTypedRouter();
|
|
1422
|
+
return useSyncExternalStore(router.subscribe, () => router.target);
|
|
1423
|
+
},
|
|
1304
1424
|
useParams(name) {
|
|
1305
1425
|
const router = useTypedRouter();
|
|
1306
1426
|
const current = useContext(CurrentNodeContext);
|
|
@@ -1310,16 +1430,10 @@ const createRouterHooks = (routes) => {
|
|
|
1310
1430
|
if (current === null) throw new Error("stacker: useParams must be used within a route component");
|
|
1311
1431
|
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
1432
|
return [current.node.params, replace];
|
|
1313
|
-
},
|
|
1314
|
-
useRouter: useTypedRouter,
|
|
1315
|
-
useTarget() {
|
|
1316
|
-
const router = useTypedRouter();
|
|
1317
|
-
return useSyncExternalStore(router.subscribe, () => router.target);
|
|
1318
1433
|
}
|
|
1319
1434
|
};
|
|
1320
1435
|
};
|
|
1321
|
-
|
|
1322
|
-
//#region src/react/link.tsx
|
|
1436
|
+
// src/react/link.tsx
|
|
1323
1437
|
const isModifiedClick = (event) => event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey;
|
|
1324
1438
|
const isRoutable = (to) => {
|
|
1325
1439
|
try {
|
|
@@ -1336,7 +1450,7 @@ const isRoutable = (to) => {
|
|
|
1336
1450
|
* @param props link properties
|
|
1337
1451
|
* @returns rendered anchor element
|
|
1338
1452
|
*/
|
|
1339
|
-
const Link = ({
|
|
1453
|
+
const Link = ({ to, replace, children, onClick, ...rest }) => {
|
|
1340
1454
|
const router = useRouter();
|
|
1341
1455
|
return /* @__PURE__ */ jsx("a", {
|
|
1342
1456
|
href: to,
|
|
@@ -1345,13 +1459,12 @@ const Link = ({ children, onClick, replace, to, ...rest }) => {
|
|
|
1345
1459
|
if (event.defaultPrevented || isModifiedClick(event) || rest.download !== void 0 || rest.target !== void 0 && rest.target !== "" && rest.target !== "_self" || !isRoutable(to)) return;
|
|
1346
1460
|
event.preventDefault();
|
|
1347
1461
|
router.navigate({
|
|
1348
|
-
|
|
1349
|
-
|
|
1462
|
+
to,
|
|
1463
|
+
replace
|
|
1350
1464
|
});
|
|
1351
1465
|
},
|
|
1352
1466
|
...rest,
|
|
1353
1467
|
children
|
|
1354
1468
|
});
|
|
1355
1469
|
};
|
|
1356
|
-
//#endregion
|
|
1357
1470
|
export { Link, NavigationHistory, Outlet, Router, RouterView, boolean, createRouterHooks, defineRoutes, enumOf, integer, layout, lazy, nonEmpty, optional, resolveMeta, route, string, useFocusEffect, useIsFocused, useLocation, useParams, useRoute, useRouter, withDefault };
|