-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add custom directive to restore position - attemp fail
- Loading branch information
Showing
5 changed files
with
73 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...hared/typescript/util/src/lib/directives/disable-scroll-position-restoration.directive.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { ViewportScroller } from '@angular/common'; | ||
import { Directive, OnInit, DestroyRef } from '@angular/core'; | ||
import { Router, Scroll } from '@angular/router'; | ||
import { filter, map, switchMap, BehaviorSubject, tap } from 'rxjs'; | ||
|
||
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | ||
|
||
@Directive({ | ||
selector: '[sageDisableScrollPositionRestoration]', | ||
standalone: true, | ||
}) | ||
export class DisableScrollPositionRestorationDirective implements OnInit { | ||
shouldScroll = new BehaviorSubject<boolean>(false); | ||
private readonly shouldScroll$ = this.shouldScroll.asObservable(); | ||
|
||
constructor( | ||
private router: Router, | ||
private viewportScroller: ViewportScroller, | ||
private destroyRef: DestroyRef | ||
) {} | ||
|
||
ngOnInit() { | ||
const position$ = this.router.events.pipe( | ||
tap((ss) => console.log(ss)), | ||
filter((event: any) => event instanceof Scroll), | ||
map((event: Scroll) => event.position) | ||
); | ||
|
||
position$ | ||
.pipe( | ||
switchMap((position) => | ||
this.shouldScroll$.pipe( | ||
filter(Boolean), | ||
map(() => position) | ||
) | ||
), | ||
takeUntilDestroyed(this.destroyRef) | ||
) | ||
.subscribe({ | ||
next: (position) => { | ||
this.viewportScroller.scrollToPosition(position || [0, 0]); | ||
}, | ||
}); | ||
} | ||
|
||
ngAfterViewInit(): void { | ||
this.shouldScroll.next(true); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...shared/typescript/util/src/lib/directives/disable-scroll-position-restoration.spec.ts.off
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { DisableScrollPositionRestorationDirective } from './disable-scroll-position-restoration.directive'; | ||
|
||
describe('DisableScrollPositionRestorationDirective', () => { | ||
it('should create an instance', () => { | ||
const directive = new DisableScrollPositionRestorationDirective(); | ||
expect(directive).toBeTruthy(); | ||
}); | ||
}); |