@real-router/angular 0.24.7 → 0.24.9

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
@@ -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
 
@@ -249,32 +249,28 @@ function createScrollRestoration(router, options) {
249
249
  });
250
250
  };
251
251
  let restoreToken = 0;
252
- const restorePos = (top) => {
253
- if (!getContainer) {
254
- globalThis.scrollTo({
255
- top,
256
- left: 0,
257
- behavior
258
- });
259
- return;
260
- }
252
+ const restorePos = (route, token) => {
253
+ const top = loadStore()[keyOf(route)] ?? 0;
261
254
  let frames = 0;
262
- const token = restoreToken += 1;
255
+ const holds = (scroller) => scroller.scrollHeight - scroller.clientHeight >= top - 1;
263
256
  const attempt = () => {
264
257
  if (destroyed || token !== restoreToken) return;
265
- const element = getContainer();
258
+ const element = getContainer?.();
266
259
  if (element) {
267
260
  element.scrollTo({
268
261
  top,
269
262
  left: 0,
270
263
  behavior
271
264
  });
272
- if (behavior !== "smooth" && Math.abs(element.scrollTop - top) <= 1) return;
273
- } else globalThis.scrollTo({
274
- top,
275
- left: 0,
276
- behavior
277
- });
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
+ }
278
274
  if (frames >= RESTORE_RETRY_FRAMES) return;
279
275
  frames += 1;
280
276
  requestAnimationFrame(attempt);
@@ -312,10 +308,24 @@ function createScrollRestoration(router, options) {
312
308
  };
313
309
  let destroyed = false;
314
310
  let scrollSettled = true;
311
+ let landedOnNewEntry = false;
312
+ const onPopState = (event) => {
313
+ landedOnNewEntry = event.state === null || event.state === void 0;
314
+ };
315
315
  const unsubscribe = router.subscribe(({ route, previousRoute }) => {
316
- const nav = route.context.navigation;
317
- 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());
318
326
  scrollSettled = false;
327
+ if (!replace || historyStep || reload || previousRoute?.name !== route.name) restoreToken += 1;
328
+ const token = restoreToken;
319
329
  requestAnimationFrame(() => {
320
330
  if (destroyed) return;
321
331
  scrollSettled = true;
@@ -323,15 +333,16 @@ function createScrollRestoration(router, options) {
323
333
  scrollToHashOrTop(route);
324
334
  return;
325
335
  }
326
- if (route.transition?.reload || nav?.navigationType === "reload") {
327
- restorePos(loadStore()[keyOf(route)] ?? 0);
336
+ if (reload) {
337
+ restorePos(route, token);
328
338
  return;
329
339
  }
330
- if (nav?.direction === "back" || nav?.navigationType === "traverse") {
331
- 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);
332
343
  return;
333
344
  }
334
- if (route.transition?.replace || nav?.navigationType === "replace") return;
345
+ if (replace) return;
335
346
  scrollToHashOrTop(route);
336
347
  });
337
348
  });
@@ -340,10 +351,12 @@ function createScrollRestoration(router, options) {
340
351
  if (current) putPos(keyOf(current), readPos());
341
352
  };
342
353
  globalThis.addEventListener("pagehide", onPageHide);
354
+ globalThis.addEventListener("popstate", onPopState);
343
355
  return { destroy: () => {
344
356
  destroyed = true;
345
357
  unsubscribe();
346
358
  globalThis.removeEventListener("pagehide", onPageHide);
359
+ globalThis.removeEventListener("popstate", onPopState);
347
360
  try {
348
361
  history.scrollRestoration = prevScrollRestoration;
349
362
  } catch {}