@real-router/angular 0.2.2 → 0.4.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/src/providers.ts CHANGED
@@ -1,13 +1,18 @@
1
1
  import {
2
+ DestroyRef,
2
3
  InjectionToken,
4
+ inject,
3
5
  makeEnvironmentProviders,
6
+ provideEnvironmentInitializer,
4
7
  type EnvironmentProviders,
5
8
  } from "@angular/core";
6
9
  import { getNavigator, type Router, type Navigator } from "@real-router/core";
7
10
  import { createRouteSource } from "@real-router/sources";
8
11
 
12
+ import { createScrollRestoration } from "./dom-utils";
9
13
  import { sourceToSignal } from "./sourceToSignal";
10
14
 
15
+ import type { ScrollRestorationOptions } from "./dom-utils";
11
16
  import type { RouteSignals } from "./types";
12
17
 
13
18
  export const ROUTER = new InjectionToken<Router>("ROUTER");
@@ -16,10 +21,17 @@ export const NAVIGATOR = new InjectionToken<Navigator>("NAVIGATOR");
16
21
 
17
22
  export const ROUTE = new InjectionToken<RouteSignals>("ROUTE");
18
23
 
19
- export function provideRealRouter(router: Router): EnvironmentProviders {
24
+ export interface RealRouterOptions {
25
+ scrollRestoration?: ScrollRestorationOptions;
26
+ }
27
+
28
+ export function provideRealRouter(
29
+ router: Router,
30
+ options?: RealRouterOptions,
31
+ ): EnvironmentProviders {
20
32
  const navigator = getNavigator(router);
21
33
 
22
- return makeEnvironmentProviders([
34
+ const providers: Parameters<typeof makeEnvironmentProviders>[0] = [
23
35
  { provide: ROUTER, useValue: router },
24
36
  { provide: NAVIGATOR, useValue: navigator },
25
37
  {
@@ -29,5 +41,21 @@ export function provideRealRouter(router: Router): EnvironmentProviders {
29
41
  navigator,
30
42
  }),
31
43
  },
32
- ]);
44
+ ];
45
+
46
+ if (options?.scrollRestoration) {
47
+ const scrollOpts = options.scrollRestoration;
48
+
49
+ providers.push(
50
+ provideEnvironmentInitializer(() => {
51
+ const sr = createScrollRestoration(router, scrollOpts);
52
+
53
+ inject(DestroyRef).onDestroy(() => {
54
+ sr.destroy();
55
+ });
56
+ }),
57
+ );
58
+ }
59
+
60
+ return makeEnvironmentProviders(providers);
33
61
  }