@real-router/angular 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +60 -1
- package/dist/README.md +60 -1
- package/dist/fesm2022/real-router-angular.mjs +408 -8
- package/dist/fesm2022/real-router-angular.mjs.map +1 -1
- package/dist/types/real-router-angular.d.ts +171 -4
- package/dist/types/real-router-angular.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/components/RouteView.ts +14 -0
- package/src/directives/RouteSelf.ts +6 -0
- package/src/dom-utils/direction-tracker.ts +70 -0
- package/src/dom-utils/index.ts +8 -0
- package/src/dom-utils/route-announcer.ts +1 -1
- package/src/dom-utils/view-transitions.ts +142 -0
- package/src/functions/index.ts +16 -0
- package/src/functions/injectRouteEnter.ts +129 -0
- package/src/functions/injectRouteExit.ts +118 -0
- package/src/index.ts +13 -0
- package/src/providers.ts +35 -1
|
@@ -1,11 +1,69 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { signal, inject, DestroyRef, InjectionToken, provideEnvironmentInitializer, makeEnvironmentProviders, input, TemplateRef, Directive, contentChildren, computed, Component, output,
|
|
2
|
+
import { signal, inject, DestroyRef, InjectionToken, provideEnvironmentInitializer, ApplicationRef, makeEnvironmentProviders, assertInInjectionContext, effect, input, TemplateRef, Directive, contentChildren, computed, Component, output, ElementRef } from '@angular/core';
|
|
3
3
|
import { getNavigator, UNKNOWN_ROUTE } from '@real-router/core';
|
|
4
4
|
import { createRouteSource, createRouteNodeSource, getTransitionSource, createActiveRouteSource, createDismissableError } from '@real-router/sources';
|
|
5
5
|
import { getPluginApi } from '@real-router/core/api';
|
|
6
6
|
import { getRouteUtils, startsWithSegment } from '@real-router/route-utils';
|
|
7
7
|
import { NgTemplateOutlet } from '@angular/common';
|
|
8
8
|
|
|
9
|
+
const NOOP_INSTANCE$2 = Object.freeze({
|
|
10
|
+
destroy: () => {
|
|
11
|
+
/* no-op */
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
/**
|
|
15
|
+
* Track navigation direction (forward / back) and write it to
|
|
16
|
+
* `<html data-nav-direction>` on every leave. CSS / JS readers consume
|
|
17
|
+
* the attribute via `html[data-nav-direction="back"]` selectors or
|
|
18
|
+
* `document.documentElement.dataset.navDirection`.
|
|
19
|
+
*
|
|
20
|
+
* Mechanism-agnostic — works identically whether downstream UI uses CSS
|
|
21
|
+
* `@keyframes`, View Transitions pseudo-elements, or library state
|
|
22
|
+
* (motion's `motion.div initial={{ x: ... }}`).
|
|
23
|
+
*
|
|
24
|
+
* Implementation:
|
|
25
|
+
* - On install, set `data-nav-direction="forward"` baseline.
|
|
26
|
+
* - Attach a `popstate` listener that flips an internal flag to
|
|
27
|
+
* `true`. Browser back/forward navigation triggers popstate; user
|
|
28
|
+
* clicks on `<Link>` / programmatic `router.navigate(...)` do not.
|
|
29
|
+
* - On every `subscribeLeave`, write
|
|
30
|
+
* `popstateFlag ? "back" : "forward"` and reset the flag.
|
|
31
|
+
*
|
|
32
|
+
* Returns `{ destroy }` to clean up the listener and clear the dataset
|
|
33
|
+
* attribute.
|
|
34
|
+
*/
|
|
35
|
+
function createDirectionTracker(router) {
|
|
36
|
+
if (typeof document === "undefined") {
|
|
37
|
+
return NOOP_INSTANCE$2;
|
|
38
|
+
}
|
|
39
|
+
let popstateFlag = false;
|
|
40
|
+
document.documentElement.dataset.navDirection = "forward";
|
|
41
|
+
const onPopstate = () => {
|
|
42
|
+
popstateFlag = true;
|
|
43
|
+
};
|
|
44
|
+
// IMPORTANT — listener-ordering: `popstate` fires on `window`, which
|
|
45
|
+
// has no DOM descendants, so capture phase is moot. Listeners are
|
|
46
|
+
// dispatched in registration order. To beat the browser-plugin's own
|
|
47
|
+
// popstate handler, this tracker must be installed **before**
|
|
48
|
+
// `router.usePlugin(browserPluginFactory())` in user code. Otherwise
|
|
49
|
+
// the plugin's handler runs first and synchronously fires
|
|
50
|
+
// `subscribeLeave` while `popstateFlag` is still `false`.
|
|
51
|
+
globalThis.addEventListener("popstate", onPopstate);
|
|
52
|
+
const offLeave = router.subscribeLeave(() => {
|
|
53
|
+
document.documentElement.dataset.navDirection = popstateFlag
|
|
54
|
+
? "back"
|
|
55
|
+
: "forward";
|
|
56
|
+
popstateFlag = false;
|
|
57
|
+
});
|
|
58
|
+
return {
|
|
59
|
+
destroy: () => {
|
|
60
|
+
offLeave();
|
|
61
|
+
globalThis.removeEventListener("popstate", onPopstate);
|
|
62
|
+
delete document.documentElement.dataset.navDirection;
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
9
67
|
const CLEAR_DELAY = 7000;
|
|
10
68
|
const SAFARI_READY_DELAY = 100;
|
|
11
69
|
const ANNOUNCER_ATTR = "data-real-router-announcer";
|
|
@@ -102,7 +160,7 @@ function resolveText(route, prefix, getCustomText, h1) {
|
|
|
102
160
|
if (getCustomText) {
|
|
103
161
|
return getCustomText(route);
|
|
104
162
|
}
|
|
105
|
-
const h1Text = h1?.textContent
|
|
163
|
+
const h1Text = (h1?.textContent ?? "").trim();
|
|
106
164
|
const routeName = route.name.startsWith(INTERNAL_ROUTE_PREFIX)
|
|
107
165
|
? ""
|
|
108
166
|
: route.name;
|
|
@@ -120,21 +178,21 @@ function manageFocus(h1) {
|
|
|
120
178
|
}
|
|
121
179
|
|
|
122
180
|
const STORAGE_KEY = "real-router:scroll";
|
|
123
|
-
const NOOP_INSTANCE = Object.freeze({
|
|
181
|
+
const NOOP_INSTANCE$1 = Object.freeze({
|
|
124
182
|
destroy: () => {
|
|
125
183
|
/* no-op */
|
|
126
184
|
},
|
|
127
185
|
});
|
|
128
186
|
function createScrollRestoration(router, options) {
|
|
129
187
|
if (typeof globalThis.window === "undefined") {
|
|
130
|
-
return NOOP_INSTANCE;
|
|
188
|
+
return NOOP_INSTANCE$1;
|
|
131
189
|
}
|
|
132
190
|
const mode = options?.mode ?? "restore";
|
|
133
191
|
// mode "manual" = utility does nothing. Don't flip history.scrollRestoration,
|
|
134
192
|
// don't subscribe, don't register pagehide — leave the browser's native
|
|
135
193
|
// auto-restore intact for the app to override if it wants to.
|
|
136
194
|
if (mode === "manual") {
|
|
137
|
-
return NOOP_INSTANCE;
|
|
195
|
+
return NOOP_INSTANCE$1;
|
|
138
196
|
}
|
|
139
197
|
const anchorEnabled = options?.anchorScrolling ?? true;
|
|
140
198
|
const getContainer = options?.scrollContainer;
|
|
@@ -277,6 +335,125 @@ function canonicalReplacer(_key, val) {
|
|
|
277
335
|
return val;
|
|
278
336
|
}
|
|
279
337
|
|
|
338
|
+
const NOOP_INSTANCE = Object.freeze({
|
|
339
|
+
destroy: () => {
|
|
340
|
+
/* no-op */
|
|
341
|
+
},
|
|
342
|
+
});
|
|
343
|
+
function createViewTransitions(router) {
|
|
344
|
+
if (typeof document === "undefined" ||
|
|
345
|
+
typeof document.startViewTransition !== "function") {
|
|
346
|
+
return NOOP_INSTANCE;
|
|
347
|
+
}
|
|
348
|
+
let closeVT = null;
|
|
349
|
+
let currentVT = null;
|
|
350
|
+
// Tracks whether TRANSITION_SUCCESS fired for the current leave. Used to
|
|
351
|
+
// distinguish "benign cleanup abort" (router's async path aborts its own
|
|
352
|
+
// controller in a finally block after successful navigation) from "real
|
|
353
|
+
// cancellation" (concurrent navigate, guard rejection, dispose).
|
|
354
|
+
let successFired = false;
|
|
355
|
+
const resolveAndClear = () => {
|
|
356
|
+
closeVT?.();
|
|
357
|
+
closeVT = null;
|
|
358
|
+
};
|
|
359
|
+
const offLeave = router.subscribeLeave(({ signal }) => {
|
|
360
|
+
// Reentrant abort: signal already aborted when we're called. Open no VT
|
|
361
|
+
// — router will fall through to TRANSITION_CANCELLED via isCurrentNav()
|
|
362
|
+
// after leave resolves. addEventListener("abort", ...) does not re-fire
|
|
363
|
+
// for past events, so skipping startViewTransition is the safe path.
|
|
364
|
+
if (signal.aborted) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
successFired = false;
|
|
368
|
+
resolveAndClear();
|
|
369
|
+
// Return a Promise so the router awaits until the browser invokes
|
|
370
|
+
// updateCallback. This ensures old DOM snapshot is captured BEFORE the
|
|
371
|
+
// router commits the new state — giving correct exit→state→entry
|
|
372
|
+
// ordering (vs fire-and-forget, where URL changes before VT captures).
|
|
373
|
+
return new Promise((resolveLeave) => {
|
|
374
|
+
// Capture the resolver synchronously BEFORE startViewTransition() is
|
|
375
|
+
// called. The browser invokes updateCallback in a later task, but
|
|
376
|
+
// router.subscribe (TRANSITION_SUCCESS) can fire before that. If we
|
|
377
|
+
// captured `resolve` inside the callback, subscribe would see closeVT
|
|
378
|
+
// still null and skip resolving — the deferred would hang for 4s
|
|
379
|
+
// until the VT API aborts with TimeoutError.
|
|
380
|
+
const deferred = new Promise((resolve) => {
|
|
381
|
+
closeVT = resolve;
|
|
382
|
+
});
|
|
383
|
+
signal.addEventListener("abort", () => {
|
|
384
|
+
if (successFired) {
|
|
385
|
+
// Router's async path (#finishAsyncNavigation) aborts its own
|
|
386
|
+
// controller in a finally block AFTER completeTransition (and
|
|
387
|
+
// thus AFTER subscribe fired). This is cleanup, not
|
|
388
|
+
// cancellation — VT is progressing normally, do nothing.
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
// Real cancellation (concurrent navigate, dispose). Resolve the
|
|
392
|
+
// deferred so updateCallback can complete, skip the VT so no
|
|
393
|
+
// stale animation leaks, and unblock the router if the abort
|
|
394
|
+
// fires before updateCallback was invoked.
|
|
395
|
+
resolveAndClear();
|
|
396
|
+
currentVT?.skipTransition?.();
|
|
397
|
+
resolveLeave();
|
|
398
|
+
}, { once: true });
|
|
399
|
+
try {
|
|
400
|
+
currentVT = document.startViewTransition(() => {
|
|
401
|
+
// Resolving here unblocks the router at the moment the browser
|
|
402
|
+
// enters updateCallback — by spec, old DOM snapshot is captured
|
|
403
|
+
// before this callback runs. Router now proceeds through
|
|
404
|
+
// activation guards and setState; the VT animation waits on
|
|
405
|
+
// `deferred`, which is resolved from router.subscribe after a
|
|
406
|
+
// task-queue tick (see NOTE on setTimeout below).
|
|
407
|
+
resolveLeave();
|
|
408
|
+
return deferred;
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
catch {
|
|
412
|
+
// Defensive: spec says startViewTransition doesn't throw under
|
|
413
|
+
// normal conditions, but Chromium has had edge cases (detached
|
|
414
|
+
// document, extension interference). Clean up and unblock router.
|
|
415
|
+
resolveAndClear();
|
|
416
|
+
resolveLeave();
|
|
417
|
+
}
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
const offSuccess = router.subscribe(() => {
|
|
421
|
+
const resolver = closeVT;
|
|
422
|
+
successFired = true;
|
|
423
|
+
closeVT = null;
|
|
424
|
+
if (resolver === null) {
|
|
425
|
+
currentVT = null;
|
|
426
|
+
}
|
|
427
|
+
else {
|
|
428
|
+
// CRITICAL: CANNOT use requestAnimationFrame here. When the router
|
|
429
|
+
// takes the async path (leave returned a Promise), subscribe fires
|
|
430
|
+
// AFTER the browser has already transitioned VT into the
|
|
431
|
+
// "update-callback-called" phase. In that phase Chromium sets
|
|
432
|
+
// rendering suppression to true, which ALSO blocks rAF callbacks.
|
|
433
|
+
// rAF would never fire → deferred never resolves → browser aborts
|
|
434
|
+
// vt.ready with TimeoutError after 4s (observed in Chromium).
|
|
435
|
+
//
|
|
436
|
+
// setTimeout runs on the task queue independent of the rendering
|
|
437
|
+
// pipeline, so it fires regardless of suppression. React's scheduler
|
|
438
|
+
// uses MessageChannel tasks, which are queued before our setTimeout,
|
|
439
|
+
// so the new DOM is committed by the time our callback runs.
|
|
440
|
+
setTimeout(() => {
|
|
441
|
+
resolver();
|
|
442
|
+
currentVT = null;
|
|
443
|
+
}, 0);
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
return {
|
|
447
|
+
destroy: () => {
|
|
448
|
+
offLeave();
|
|
449
|
+
offSuccess();
|
|
450
|
+
currentVT?.skipTransition?.();
|
|
451
|
+
currentVT = null;
|
|
452
|
+
resolveAndClear();
|
|
453
|
+
},
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
|
|
280
457
|
function shouldNavigate(evt) {
|
|
281
458
|
return (evt.button === 0 &&
|
|
282
459
|
!evt.metaKey &&
|
|
@@ -399,6 +576,32 @@ function provideRealRouter(router, options) {
|
|
|
399
576
|
});
|
|
400
577
|
}));
|
|
401
578
|
}
|
|
579
|
+
if (options?.viewTransitions === true) {
|
|
580
|
+
providers.push(provideEnvironmentInitializer(() => {
|
|
581
|
+
const appRef = inject(ApplicationRef);
|
|
582
|
+
// Force synchronous change detection on every transition success
|
|
583
|
+
// BEFORE the VT utility resolves its deferred. The utility uses
|
|
584
|
+
// `setTimeout(0)` to release the new-snapshot capture, which is
|
|
585
|
+
// load-bearing because Chromium blocks rAF callbacks while VT sits
|
|
586
|
+
// in the `update-callback-called` phase. Angular's zoneless CD is
|
|
587
|
+
// rAF-driven by default — without this synchronous tick the new
|
|
588
|
+
// DOM is not committed when the browser captures the new snapshot,
|
|
589
|
+
// so old and new snapshots end up identical and animations finish
|
|
590
|
+
// in ~0 ms with no visible work (the inner-route `products.list ↔
|
|
591
|
+
// products.detail` morph in the example example was the canary).
|
|
592
|
+
// Subscribers fire in registration order; this one runs BEFORE
|
|
593
|
+
// `createViewTransitions` registers its own subscriber,
|
|
594
|
+
// guaranteeing CD completes first.
|
|
595
|
+
const offTick = router.subscribe(() => {
|
|
596
|
+
appRef.tick();
|
|
597
|
+
});
|
|
598
|
+
const vt = createViewTransitions(router);
|
|
599
|
+
inject(DestroyRef).onDestroy(() => {
|
|
600
|
+
offTick();
|
|
601
|
+
vt.destroy();
|
|
602
|
+
});
|
|
603
|
+
}));
|
|
604
|
+
}
|
|
402
605
|
return makeEnvironmentProviders(providers);
|
|
403
606
|
}
|
|
404
607
|
|
|
@@ -450,6 +653,182 @@ function injectIsActiveRoute(routeName, params, options) {
|
|
|
450
653
|
return sourceToSignal(source);
|
|
451
654
|
}
|
|
452
655
|
|
|
656
|
+
/**
|
|
657
|
+
* Subscribe to the router's leave-window with the universal guards baked
|
|
658
|
+
* in. Wraps `router.subscribeLeave` so consumers don't repeat the same
|
|
659
|
+
* boilerplate every time:
|
|
660
|
+
*
|
|
661
|
+
* - **Reentrant abort pre-check**: if `signal.aborted` is already `true`
|
|
662
|
+
* when the handler would run (rapid navigation superseded a slower
|
|
663
|
+
* one), the handler is skipped entirely.
|
|
664
|
+
* - **Same-route skip**: by default, `route.name === nextRoute.name`
|
|
665
|
+
* short-circuits the handler — query-only navigations skip the work.
|
|
666
|
+
* Opt out with `skipSameRoute: false`.
|
|
667
|
+
*
|
|
668
|
+
* Cleanup is bound to the injection context's `DestroyRef`. Must be
|
|
669
|
+
* called within an injection context (constructor, field initializer,
|
|
670
|
+
* or `runInInjectionContext`).
|
|
671
|
+
*
|
|
672
|
+
* If the handler returns a Promise, the router blocks on it. If the
|
|
673
|
+
* Promise resolves, navigation proceeds. If it rejects, the router emits
|
|
674
|
+
* `TRANSITION_CANCELLED`.
|
|
675
|
+
*
|
|
676
|
+
* **Handler reactivity (Angular):** `inject*` functions run **once**
|
|
677
|
+
* during component construction; `handler` is captured in closure at the
|
|
678
|
+
* call site. The common Angular pattern is to pass a class method
|
|
679
|
+
* (`this.onExit.bind(this)` or an arrow-property) — its identity is
|
|
680
|
+
* stable across change detection. To vary behavior over time, read
|
|
681
|
+
* signals **inside** the handler body — do not rely on swapping the
|
|
682
|
+
* handler reference.
|
|
683
|
+
*
|
|
684
|
+
* @example Animation
|
|
685
|
+
* ```ts
|
|
686
|
+
* \@Component({ ... })
|
|
687
|
+
* class FadeOutComponent {
|
|
688
|
+
* private el = inject(ElementRef<HTMLElement>);
|
|
689
|
+
*
|
|
690
|
+
* constructor() {
|
|
691
|
+
* injectRouteExit(async ({ signal }) => {
|
|
692
|
+
* const el = this.el.nativeElement;
|
|
693
|
+
* el.classList.add("fade-out");
|
|
694
|
+
* const cleanup = () => el.classList.remove("fade-out");
|
|
695
|
+
* signal.addEventListener("abort", cleanup, { once: true });
|
|
696
|
+
* try {
|
|
697
|
+
* el.getBoundingClientRect();
|
|
698
|
+
* await Promise.allSettled(el.getAnimations().map((a) => a.finished));
|
|
699
|
+
* } finally {
|
|
700
|
+
* cleanup();
|
|
701
|
+
* }
|
|
702
|
+
* });
|
|
703
|
+
* }
|
|
704
|
+
* }
|
|
705
|
+
* ```
|
|
706
|
+
*
|
|
707
|
+
* @example Auto-save form draft
|
|
708
|
+
* ```ts
|
|
709
|
+
* injectRouteExit(async ({ signal }) => {
|
|
710
|
+
* if (this.formState.dirty) {
|
|
711
|
+
* await this.api.saveDraft(this.formState, { signal });
|
|
712
|
+
* }
|
|
713
|
+
* });
|
|
714
|
+
* ```
|
|
715
|
+
*/
|
|
716
|
+
function injectRouteExit(handler, options) {
|
|
717
|
+
assertInInjectionContext(injectRouteExit);
|
|
718
|
+
const router = injectRouter();
|
|
719
|
+
const destroyRef = inject(DestroyRef);
|
|
720
|
+
const skipSameRoute = options?.skipSameRoute ?? true;
|
|
721
|
+
const off = router.subscribeLeave(({ route, nextRoute, signal }) => {
|
|
722
|
+
if (skipSameRoute && route.name === nextRoute.name) {
|
|
723
|
+
return;
|
|
724
|
+
}
|
|
725
|
+
if (signal.aborted) {
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
return handler({ route, nextRoute, signal });
|
|
729
|
+
});
|
|
730
|
+
destroyRef.onDestroy(off);
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* Fire `handler` once when the component is created as a result of a
|
|
735
|
+
* navigation. Mirror of `injectRouteExit` for the entry side.
|
|
736
|
+
*
|
|
737
|
+
* What this function covers that an ad-hoc `effect()` + `injectRoute()`
|
|
738
|
+
* doesn't:
|
|
739
|
+
*
|
|
740
|
+
* - **Skip-initial**: handler is skipped when there is no
|
|
741
|
+
* `route.transition.from` (i.e. first-load mount). Most consumers
|
|
742
|
+
* want to fire side effects only on real navigations, not on
|
|
743
|
+
* hydration.
|
|
744
|
+
* - **Same-route skip** (default): handler is skipped when
|
|
745
|
+
* `route.transition.from === route.name`. Sort/filter/query-only
|
|
746
|
+
* navigations re-run the effect (because the `route` reference
|
|
747
|
+
* changes), but they are not "entries" in the animation / analytics
|
|
748
|
+
* sense. Opt out with `skipSameRoute: false`.
|
|
749
|
+
* - **Mount-time `route` / `previousRoute` snapshot**: handler receives
|
|
750
|
+
* the values that were live at the moment of effect activation.
|
|
751
|
+
*
|
|
752
|
+
* Effect cleanup is wired through the injection context's `DestroyRef`
|
|
753
|
+
* (Angular's `effect()` ties into the active context automatically).
|
|
754
|
+
* Must be called within an injection context (constructor, field
|
|
755
|
+
* initializer, or `runInInjectionContext`).
|
|
756
|
+
*
|
|
757
|
+
* **Handler reactivity (Angular):** `inject*` functions run **once**
|
|
758
|
+
* during component construction; `handler` is captured in closure at the
|
|
759
|
+
* call site. The common Angular pattern is to pass a class method —
|
|
760
|
+
* its identity is stable across change detection. To vary behavior
|
|
761
|
+
* over time, read signals **inside** the handler body.
|
|
762
|
+
*
|
|
763
|
+
* @example Direction-aware entry animation
|
|
764
|
+
* ```ts
|
|
765
|
+
* \@Component({ ... })
|
|
766
|
+
* class PageComponent {
|
|
767
|
+
* private el = inject(ElementRef<HTMLElement>);
|
|
768
|
+
*
|
|
769
|
+
* constructor() {
|
|
770
|
+
* injectRouteEnter(({ route }) => {
|
|
771
|
+
* const direction = route.context.browser?.direction;
|
|
772
|
+
* this.el.nativeElement.classList.add(
|
|
773
|
+
* direction === "back" ? "slide-from-left" : "slide-from-right",
|
|
774
|
+
* );
|
|
775
|
+
* });
|
|
776
|
+
* }
|
|
777
|
+
* }
|
|
778
|
+
* ```
|
|
779
|
+
*
|
|
780
|
+
* @example Analytics page-enter event (skip-initial built-in)
|
|
781
|
+
* ```ts
|
|
782
|
+
* injectRouteEnter(({ route, previousRoute }) => {
|
|
783
|
+
* analytics.track("page_enter", {
|
|
784
|
+
* route: route.name,
|
|
785
|
+
* from: previousRoute.name,
|
|
786
|
+
* });
|
|
787
|
+
* });
|
|
788
|
+
* ```
|
|
789
|
+
*/
|
|
790
|
+
function injectRouteEnter(handler, options) {
|
|
791
|
+
assertInInjectionContext(injectRouteEnter);
|
|
792
|
+
const { routeState } = injectRoute();
|
|
793
|
+
const skipSameRoute = options?.skipSameRoute ?? true;
|
|
794
|
+
let lastHandledRoute = null;
|
|
795
|
+
effect(() => {
|
|
796
|
+
const { route, previousRoute } = routeState();
|
|
797
|
+
// Early-exit guards, top-down:
|
|
798
|
+
//
|
|
799
|
+
// - **Defensive**: `route` may be undefined during SSR or
|
|
800
|
+
// pre-start hydration. Not testable from vitest, v8-ignored.
|
|
801
|
+
// - **Skip-initial**: `state.transition.from` is undefined only
|
|
802
|
+
// for the very first state committed by `router.start()`.
|
|
803
|
+
// - **Skip-same-route**: query-only navigations have
|
|
804
|
+
// `transition.from === route.name`. Opt-out via
|
|
805
|
+
// `skipSameRoute: false`.
|
|
806
|
+
// - **Defensive dedupe + missing `previousRoute`**: same `route`
|
|
807
|
+
// ref between effect re-runs is unexpected on Angular (the
|
|
808
|
+
// signal only fires on real reference changes); `!previousRoute`
|
|
809
|
+
// is unreachable once `transition.from` is set (core populates
|
|
810
|
+
// them together). Both kept for parity with React; v8-ignored.
|
|
811
|
+
/* v8 ignore start */
|
|
812
|
+
if (!route) {
|
|
813
|
+
return;
|
|
814
|
+
}
|
|
815
|
+
/* v8 ignore stop */
|
|
816
|
+
if (!route.transition.from) {
|
|
817
|
+
return;
|
|
818
|
+
}
|
|
819
|
+
if (skipSameRoute && route.transition.from === route.name) {
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
/* v8 ignore start */
|
|
823
|
+
if (lastHandledRoute === route || !previousRoute) {
|
|
824
|
+
return;
|
|
825
|
+
}
|
|
826
|
+
/* v8 ignore stop */
|
|
827
|
+
lastHandledRoute = route;
|
|
828
|
+
handler({ route, previousRoute });
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
|
|
453
832
|
class RouteMatch {
|
|
454
833
|
routeMatch = input.required(...(ngDevMode ? [{ debugName: "routeMatch" }] : /* istanbul ignore next */ []));
|
|
455
834
|
templateRef = inject(TemplateRef);
|
|
@@ -471,6 +850,16 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.8", ngImpor
|
|
|
471
850
|
args: [{ selector: "ng-template[routeNotFound]" }]
|
|
472
851
|
}] });
|
|
473
852
|
|
|
853
|
+
class RouteSelf {
|
|
854
|
+
templateRef = inject(TemplateRef);
|
|
855
|
+
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: RouteSelf, deps: [], target: i0.ɵɵFactoryTarget.Directive });
|
|
856
|
+
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.2.8", type: RouteSelf, isStandalone: true, selector: "ng-template[routeSelf]", ngImport: i0 });
|
|
857
|
+
}
|
|
858
|
+
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: RouteSelf, decorators: [{
|
|
859
|
+
type: Directive,
|
|
860
|
+
args: [{ selector: "ng-template[routeSelf]" }]
|
|
861
|
+
}] });
|
|
862
|
+
|
|
474
863
|
const EMPTY_SNAPSHOT = Object.freeze({
|
|
475
864
|
route: undefined,
|
|
476
865
|
previousRoute: undefined,
|
|
@@ -478,6 +867,7 @@ const EMPTY_SNAPSHOT = Object.freeze({
|
|
|
478
867
|
class RouteView {
|
|
479
868
|
nodeName = input("", { ...(ngDevMode ? { debugName: "nodeName" } : /* istanbul ignore next */ {}), alias: "routeNode" });
|
|
480
869
|
matches = contentChildren(RouteMatch, { ...(ngDevMode ? { debugName: "matches" } : /* istanbul ignore next */ {}), descendants: true });
|
|
870
|
+
selfs = contentChildren(RouteSelf, { ...(ngDevMode ? { debugName: "selfs" } : /* istanbul ignore next */ {}), descendants: true });
|
|
481
871
|
notFounds = contentChildren(RouteNotFound, { ...(ngDevMode ? { debugName: "notFounds" } : /* istanbul ignore next */ {}), descendants: true });
|
|
482
872
|
activeTemplate = computed(() => {
|
|
483
873
|
const snapshot = this.routeState();
|
|
@@ -492,6 +882,16 @@ class RouteView {
|
|
|
492
882
|
return match.templateRef;
|
|
493
883
|
}
|
|
494
884
|
}
|
|
885
|
+
// Self has priority over NotFound. First-wins to mirror NotFound's
|
|
886
|
+
// last-wins inversion would be inconsistent with React/Preact/Solid/Vue
|
|
887
|
+
// adapters where Self is "first wins"; Angular's contentChildren returns
|
|
888
|
+
// declaration order, so picking [0] gives first-wins.
|
|
889
|
+
if (routeName === this.nodeName()) {
|
|
890
|
+
const first = this.selfs().at(0);
|
|
891
|
+
if (first) {
|
|
892
|
+
return first.templateRef;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
495
895
|
if (routeName === UNKNOWN_ROUTE) {
|
|
496
896
|
const last = this.notFounds().at(-1);
|
|
497
897
|
if (last) {
|
|
@@ -525,7 +925,7 @@ class RouteView {
|
|
|
525
925
|
});
|
|
526
926
|
}
|
|
527
927
|
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.8", ngImport: i0, type: RouteView, deps: [], target: i0.ɵɵFactoryTarget.Component });
|
|
528
|
-
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.2.8", type: RouteView, isStandalone: true, selector: "route-view", inputs: { nodeName: { classPropertyName: "nodeName", publicName: "routeNode", isSignal: true, isRequired: false, transformFunction: null } }, queries: [{ propertyName: "matches", predicate: RouteMatch, descendants: true, isSignal: true }, { propertyName: "notFounds", predicate: RouteNotFound, descendants: true, isSignal: true }], ngImport: i0, template: `
|
|
928
|
+
static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "21.2.8", type: RouteView, isStandalone: true, selector: "route-view", inputs: { nodeName: { classPropertyName: "nodeName", publicName: "routeNode", isSignal: true, isRequired: false, transformFunction: null } }, queries: [{ propertyName: "matches", predicate: RouteMatch, descendants: true, isSignal: true }, { propertyName: "selfs", predicate: RouteSelf, descendants: true, isSignal: true }, { propertyName: "notFounds", predicate: RouteNotFound, descendants: true, isSignal: true }], ngImport: i0, template: `
|
|
529
929
|
@if (activeTemplate()) {
|
|
530
930
|
<ng-container [ngTemplateOutlet]="activeTemplate()!" />
|
|
531
931
|
}
|
|
@@ -542,7 +942,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.8", ngImpor
|
|
|
542
942
|
`,
|
|
543
943
|
imports: [NgTemplateOutlet],
|
|
544
944
|
}]
|
|
545
|
-
}], propDecorators: { nodeName: [{ type: i0.Input, args: [{ isSignal: true, alias: "routeNode", required: false }] }], matches: [{ type: i0.ContentChildren, args: [i0.forwardRef(() => RouteMatch), { ...{ descendants: true }, isSignal: true }] }], notFounds: [{ type: i0.ContentChildren, args: [i0.forwardRef(() => RouteNotFound), { ...{ descendants: true }, isSignal: true }] }] } });
|
|
945
|
+
}], propDecorators: { nodeName: [{ type: i0.Input, args: [{ isSignal: true, alias: "routeNode", required: false }] }], matches: [{ type: i0.ContentChildren, args: [i0.forwardRef(() => RouteMatch), { ...{ descendants: true }, isSignal: true }] }], selfs: [{ type: i0.ContentChildren, args: [i0.forwardRef(() => RouteSelf), { ...{ descendants: true }, isSignal: true }] }], notFounds: [{ type: i0.ContentChildren, args: [i0.forwardRef(() => RouteNotFound), { ...{ descendants: true }, isSignal: true }] }] } });
|
|
546
946
|
|
|
547
947
|
class RouterErrorBoundary {
|
|
548
948
|
errorTemplate = input(...(ngDevMode ? [undefined, { debugName: "errorTemplate" }] : /* istanbul ignore next */ []));
|
|
@@ -731,5 +1131,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.8", ngImpor
|
|
|
731
1131
|
* Generated bundle index. Do not edit.
|
|
732
1132
|
*/
|
|
733
1133
|
|
|
734
|
-
export { NAVIGATOR, NavigationAnnouncer, ROUTE, ROUTER, RealLink, RealLinkActive, RouteMatch, RouteNotFound, RouteView, RouterErrorBoundary, injectIsActiveRoute, injectNavigator, injectRoute, injectRouteNode, injectRouteUtils, injectRouter, injectRouterTransition, provideRealRouter, sourceToSignal };
|
|
1134
|
+
export { NAVIGATOR, NavigationAnnouncer, ROUTE, ROUTER, RealLink, RealLinkActive, RouteMatch, RouteNotFound, RouteSelf, RouteView, RouterErrorBoundary, injectIsActiveRoute, injectNavigator, injectRoute, injectRouteEnter, injectRouteExit, injectRouteNode, injectRouteUtils, injectRouter, injectRouterTransition, provideRealRouter, sourceToSignal };
|
|
735
1135
|
//# sourceMappingURL=real-router-angular.mjs.map
|