()
| 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) { |
no test coverage detected