@real-router/angular 0.24.6 → 0.24.8

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 CHANGED
@@ -10,7 +10,7 @@
10
10
  npm install @real-router/angular @real-router/core @real-router/browser-plugin
11
11
  ```
12
12
 
13
- **Peer dependencies:** `@angular/core` >= 22.0.0, `@angular/common` >= 22.0.0
13
+ **Peer dependencies:** `@real-router/core`, `@angular/core` >= 22.0.0, `@angular/common` >= 22.0.0
14
14
 
15
15
  ## Quick Start
16
16
 
@@ -571,7 +571,7 @@ interface RealRouterOptions {
571
571
  }
572
572
  ```
573
573
 
574
- Restores scroll on back/forward, scrolls to top (or `#hash`) on push. Three modes: `"restore"` (default), `"top"`, `"native"`. Custom containers via `scrollContainer: () => HTMLElement | null`. The utility is created by `provideEnvironmentInitializer` and torn down via `inject(DestroyRef)`. Options are a snapshot at bootstrap — not reactive to runtime changes. Under `@real-router/browser-plugin`, replace transitions now preserve scroll position and programmatic reloads restore from `sessionStorage` (portable via `state.transition.replace` / `state.transition.reload`). See [Scroll Restoration guide](https://github.com/greydragon888/real-router/wiki/Scroll-Restoration) for the full behaviour matrix.
574
+ Restores scroll on back/forward, scrolls to top (or `#hash`) on push. Three modes: `"restore"` (default), `"top"`, `"native"`. Custom containers via `scrollContainer: () => HTMLElement | null`. The utility is created by `provideEnvironmentInitializer` and torn down via `inject(DestroyRef)`. Options are a snapshot at bootstrap — not reactive to runtime changes. Under `@real-router/browser-plugin`, replace transitions now preserve scroll position and programmatic reloads restore from `sessionStorage` (portable via `state.transition.replace` / `state.transition.reload`). In `"restore"` mode under `@real-router/hash-plugin`, Back and Forward leave the scroll where it is: the plugin publishes no step through history. See [Scroll Restoration guide](https://github.com/greydragon888/real-router/wiki/Scroll-Restoration) for the full behaviour matrix.
575
575
 
576
576
  ## Scroll Spy
577
577
 
@@ -591,7 +591,7 @@ The URL hash tracks the topmost visible anchor as the user scrolls, syncing `sta
591
591
 
592
592
  Options: `{ selector: string, rootMargin?: string, scrollContainer?: () => HTMLElement | null }`. Default `rootMargin`: `"-20% 0px -60% 0px"`. Empty `selector` / `undefined` = off. SSR / browsers without `IntersectionObserver` = NOOP. Requires `browser-plugin` or `navigation-plugin` (hash-plugin / memory-plugin → warn-once + NOOP). The utility is installed via `provideEnvironmentInitializer` (`installScrollSpy`) and torn down on `DestroyRef`; options are a snapshot at bootstrap, not reactive.
593
593
 
594
- Available on both `provideRealRouter(router, { scrollSpy })` (SPA) and `provideRealRouterFactory({ baseRouter, scrollSpy })` (SSR). On the SSR path the utility is correctly NOOP'd on the server pass (`document` is undefined). Behaviour is identical to the React adapter — see the [React Scroll Spy demo](../../examples/web/react/hash-examples/scroll-spy/) (12 sections, TOC sidebar, 10 e2e scenarios) and the [Scroll Spy guide](https://github.com/greydragon888/real-router/wiki/Scroll-Spy).
594
+ Available on both `provideRealRouter(router, { scrollSpy })` (SPA) and `provideRealRouterFactory({ baseRouter, scrollSpy })` (SSR). On the SSR path the utility is correctly NOOP'd on the server pass (`document` is undefined). Behaviour is identical to the React adapter — see the [React Scroll Spy demo](../../examples/web/react/hash-examples/scroll-spy/) (12 sections, TOC sidebar, Playwright-tested) and the [Scroll Spy guide](https://github.com/greydragon888/real-router/wiki/Scroll-Spy).
595
595
 
596
596
  ## Server-Side Rendering
597
597
 
@@ -789,7 +789,7 @@ The shared (cross-framework) wiki pages use the `use*` naming convention — the
789
789
 
790
790
  | Package | Description |
791
791
  | ---------------------------------------------------------------------------------------- | --------------------------------------- |
792
- | [@real-router/core](https://www.npmjs.com/package/@real-router/core) | Core router (required dependency) |
792
+ | [@real-router/core](https://www.npmjs.com/package/@real-router/core) | Core router (required peer dependency) |
793
793
  | [@real-router/browser-plugin](https://www.npmjs.com/package/@real-router/browser-plugin) | Browser History API integration |
794
794
  | [@real-router/sources](https://www.npmjs.com/package/@real-router/sources) | Subscription layer (used internally) |
795
795
  | [@real-router/route-utils](https://www.npmjs.com/package/@real-router/route-utils) | Route tree queries (`injectRouteUtils`) |
@@ -14,6 +14,7 @@ function createDirectionTracker(router) {
14
14
  document.documentElement.dataset.navDirection = "forward";
15
15
  let inFlight;
16
16
  let replay;
17
+ let passedOn = false;
17
18
  const hrefOf = (state) => {
18
19
  const buildUrl = router.buildUrl;
19
20
  if (buildUrl === void 0) return;
@@ -42,6 +43,7 @@ function createDirectionTracker(router) {
42
43
  const onSettled = () => {
43
44
  inFlight = void 0;
44
45
  popstateFlag = replay !== void 0;
46
+ passedOn = popstateFlag;
45
47
  replay = void 0;
46
48
  };
47
49
  const onError = (toState) => {
@@ -55,6 +57,7 @@ function createDirectionTracker(router) {
55
57
  const unsubs = [
56
58
  api.addEventListener(events.TRANSITION_START, (toState) => {
57
59
  inFlight = toState;
60
+ if (popstateFlag && !passedOn && landsOnRouterState()) popstateFlag = false;
58
61
  }),
59
62
  api.addEventListener(events.TRANSITION_SUCCESS, onSettled),
60
63
  api.addEventListener(events.TRANSITION_ERROR, onError),
@@ -246,32 +249,28 @@ function createScrollRestoration(router, options) {
246
249
  });
247
250
  };
248
251
  let restoreToken = 0;
249
- const restorePos = (top) => {
250
- if (!getContainer) {
251
- globalThis.scrollTo({
252
- top,
253
- left: 0,
254
- behavior
255
- });
256
- return;
257
- }
252
+ const restorePos = (route, token) => {
253
+ const top = loadStore()[keyOf(route)] ?? 0;
258
254
  let frames = 0;
259
- const token = restoreToken += 1;
255
+ const holds = (scroller) => scroller.scrollHeight - scroller.clientHeight >= top - 1;
260
256
  const attempt = () => {
261
257
  if (destroyed || token !== restoreToken) return;
262
- const element = getContainer();
258
+ const element = getContainer?.();
263
259
  if (element) {
264
260
  element.scrollTo({
265
261
  top,
266
262
  left: 0,
267
263
  behavior
268
264
  });
269
- if (behavior !== "smooth" && Math.abs(element.scrollTop - top) <= 1) return;
270
- } else globalThis.scrollTo({
271
- top,
272
- left: 0,
273
- behavior
274
- });
265
+ if (holds(element)) return;
266
+ } else {
267
+ globalThis.scrollTo({
268
+ top,
269
+ left: 0,
270
+ behavior
271
+ });
272
+ if (!getContainer && holds(document.scrollingElement ?? document.documentElement)) return;
273
+ }
275
274
  if (frames >= RESTORE_RETRY_FRAMES) return;
276
275
  frames += 1;
277
276
  requestAnimationFrame(attempt);
@@ -309,10 +308,24 @@ function createScrollRestoration(router, options) {
309
308
  };
310
309
  let destroyed = false;
311
310
  let scrollSettled = true;
311
+ let landedOnNewEntry = false;
312
+ const onPopState = (event) => {
313
+ landedOnNewEntry = event.state === null || event.state === void 0;
314
+ };
312
315
  const unsubscribe = router.subscribe(({ route, previousRoute }) => {
313
- const nav = route.context.navigation;
314
- if (previousRoute && scrollSettled) putPos(keyOf(previousRoute), readPos());
316
+ const context = route.context;
317
+ const nav = context.navigation;
318
+ const memory = context.memory?.direction;
319
+ const fromPopState = context.browser?.source === "popstate";
320
+ const historyStep = fromPopState || nav?.direction === "back" || nav?.navigationType === "traverse" || memory === "back" || memory === "forward";
321
+ const transition = route.transition;
322
+ const reload = (transition?.reload ?? false) || nav?.navigationType === "reload";
323
+ const replace = (transition?.replace ?? false) || nav?.navigationType === "replace";
324
+ const settled = scrollSettled;
325
+ if (previousRoute && settled) putPos(keyOf(previousRoute), readPos());
315
326
  scrollSettled = false;
327
+ if (!replace || historyStep || reload || previousRoute?.name !== route.name) restoreToken += 1;
328
+ const token = restoreToken;
316
329
  requestAnimationFrame(() => {
317
330
  if (destroyed) return;
318
331
  scrollSettled = true;
@@ -320,15 +333,16 @@ function createScrollRestoration(router, options) {
320
333
  scrollToHashOrTop(route);
321
334
  return;
322
335
  }
323
- if (route.transition?.reload || nav?.navigationType === "reload") {
324
- restorePos(loadStore()[keyOf(route)] ?? 0);
336
+ if (reload) {
337
+ restorePos(route, token);
325
338
  return;
326
339
  }
327
- if (nav?.direction === "back" || nav?.navigationType === "traverse") {
328
- restorePos(loadStore()[keyOf(route)] ?? 0);
340
+ const keepsLocation = settled && previousRoute !== void 0 && keyOf(previousRoute) === keyOf(route);
341
+ if (historyStep && !(fromPopState && (landedOnNewEntry || keepsLocation))) {
342
+ restorePos(route, token);
329
343
  return;
330
344
  }
331
- if (route.transition?.replace || nav?.navigationType === "replace") return;
345
+ if (replace) return;
332
346
  scrollToHashOrTop(route);
333
347
  });
334
348
  });
@@ -337,10 +351,12 @@ function createScrollRestoration(router, options) {
337
351
  if (current) putPos(keyOf(current), readPos());
338
352
  };
339
353
  globalThis.addEventListener("pagehide", onPageHide);
354
+ globalThis.addEventListener("popstate", onPopState);
340
355
  return { destroy: () => {
341
356
  destroyed = true;
342
357
  unsubscribe();
343
358
  globalThis.removeEventListener("pagehide", onPageHide);
359
+ globalThis.removeEventListener("popstate", onPopState);
344
360
  try {
345
361
  history.scrollRestoration = prevScrollRestoration;
346
362
  } catch {}