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

Method setupScrollRestoration

adev/src/app/app-scroller.ts:44–95  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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 ),
81 );
82 }),
83 )
84 .subscribe(() => {
85 this.scroll();
86 });
87
88 if (windowWidth < xsBreakpoint) {
89 this.viewportScroller.setOffset([0, 64]);
90 } else if (windowWidth <= mdBreakpoint) {
91 this.viewportScroller.setOffset([0, 140]);
92 } else {
93 this.viewportScroller.setOffset([0, 24]);
94 }
95 }
96
97 private scroll(injector?: Injector) {
98 if (!this._lastScrollEvent || !this.canScroll) {

Callers 1

constructorMethod · 0.95

Calls 10

scrollMethod · 0.95
injectFunction · 0.90
firstValueFromFunction · 0.85
onDestroyMethod · 0.65
disconnectMethod · 0.65
subscribeMethod · 0.65
observeMethod · 0.45
setOffsetMethod · 0.45

Tested by

no test coverage detected