MCPcopy Index your code
hub / github.com/angular/angular / AppScroller

Class AppScroller

adev/src/app/app-scroller.ts:23–126  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21
22@Service()
23export class AppScroller {
24 private readonly router = inject(Router);
25 private readonly viewportScroller = inject(ViewportScroller);
26 private readonly appRef = inject(ApplicationRef);
27 private readonly injector = inject(EnvironmentInjector);
28 private readonly isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
29
30 private _lastScrollEvent?: Scroll;
31 private canScroll = false;
32 private cancelScroll?: () => void;
33
34 get lastScrollEvent(): Scroll | undefined {
35 return this._lastScrollEvent;
36 }
37
38 constructor() {
39 if (this.isBrowser) {
40 this.setupScrollRestoration();
41 }
42 }
43
44 private setupScrollRestoration(): void {
45 let windowWidth = window.innerWidth;
46 // Setting up a ResizeObserver to update the width on resize. (without triggering a reflow)
47 const windowSizeObserver = new ResizeObserver((entries) => {
48 windowWidth = entries[0].contentRect.width;
49 });
50 windowSizeObserver.observe(document.documentElement);
51 inject(DestroyRef).onDestroy(() => windowSizeObserver.disconnect());
52
53 const root = document.documentElement; // or any element with the variable
54 const styles = getComputedStyle(root);
55 // slice to drop the 'px'
56 const xsBreakpoint = +styles.getPropertyValue('--screen-xs').slice(0, -2);
57 const mdBreakpoint = +styles.getPropertyValue('--screen-md').slice(0, -2);
58
59 this.viewportScroller.setHistoryScrollRestoration('manual');
60 this.router.events
61 .pipe(
62 filter((e): e is Scroll => e instanceof Scroll),
63 tap((e) => {
64 this.cancelScroll?.();
65 this.canScroll = true;
66 this._lastScrollEvent = e;
67 }),
68 filter(() => {
69 const info = this.router.lastSuccessfulNavigation()?.extras.info as Record<
70 'disableScrolling',
71 boolean
72 >;
73 return !info?.['disableScrolling'];
74 }),
75 switchMap((e) => {
76 return firstValueFrom(
77 this.appRef.isStable.pipe(
78 filter((stable) => stable),
79 map(() => e),
80 ),

Callers

nothing calls this directly

Calls 2

injectFunction · 0.90
isPlatformBrowserFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…