()
| 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((e) => e.scrollBehavior !== 'manual'), |
| 69 | switchMap((e) => { |
| 70 | return firstValueFrom( |
| 71 | this.appRef.isStable.pipe( |
| 72 | filter((stable) => stable), |
| 73 | map(() => e), |
| 74 | ), |
| 75 | ); |
| 76 | }), |
| 77 | ) |
| 78 | .subscribe(() => { |
| 79 | this.scroll(); |
| 80 | }); |
| 81 | |
| 82 | if (windowWidth < xsBreakpoint) { |
| 83 | this.viewportScroller.setOffset([0, 64]); |
| 84 | } else if (windowWidth <= mdBreakpoint) { |
| 85 | this.viewportScroller.setOffset([0, 140]); |
| 86 | } else { |
| 87 | this.viewportScroller.setOffset([0, 24]); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private scroll(injector?: Injector) { |
| 92 | if (!this._lastScrollEvent || !this.canScroll) { |
no test coverage detected