Skip to content

Commit e1da41f

Browse files
atscottAndrewKushnir
authored andcommitted
fix(router): Scroll restoration should use instant scroll behavior for traversals (angular#64299)
When the scroll position is being restored, this change upates the behavior to use 'instant' rather than the default 'auto', which will be whatever the browser behavior is for 'window.scrollTo'. The 'smooth' behavior does not match how browsers behavior when performing a traversal navigation for MPAs, which is 'instant'. related to angular#58258 PR Close angular#64299
1 parent 3c9ed3d commit e1da41f

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

packages/router/src/router_scroller.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,13 @@ export class RouterScroller implements OnDestroy {
8585
private consumeScrollEvents() {
8686
return this.transitions.events.subscribe((e) => {
8787
if (!(e instanceof Scroll)) return;
88+
const instantScroll: ScrollOptions = {behavior: 'instant'};
8889
// a popstate event. The pop state event will always ignore anchor scrolling.
8990
if (e.position) {
9091
if (this.options.scrollPositionRestoration === 'top') {
91-
this.viewportScroller.scrollToPosition([0, 0]);
92+
this.viewportScroller.scrollToPosition([0, 0], instantScroll);
9293
} else if (this.options.scrollPositionRestoration === 'enabled') {
93-
this.viewportScroller.scrollToPosition(e.position);
94+
this.viewportScroller.scrollToPosition(e.position, instantScroll);
9495
}
9596
// imperative navigation "forward"
9697
} else {

packages/router/test/router_scroller.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ describe('RouterScroller', () => {
9696
events.next(new NavigationStart(3, '/a', 'popstate', {navigationId: 1}));
9797
events.next(new NavigationEnd(3, '/a', '/a'));
9898
await nextScrollEvent(events);
99-
expect(viewportScroller.scrollToPosition).toHaveBeenCalledWith([10, 100]);
99+
expect(viewportScroller.scrollToPosition).toHaveBeenCalledWith([10, 100], {
100+
behavior: 'instant',
101+
});
100102
});
101103
});
102104

@@ -146,7 +148,7 @@ describe('RouterScroller', () => {
146148
events.next(new NavigationEnd(3, '/a#anchor', '/a#anchor'));
147149
await nextScrollEvent(events);
148150
expect(viewportScroller.scrollToAnchor).not.toHaveBeenCalled();
149-
expect(viewportScroller.scrollToPosition).toHaveBeenCalledWith([0, 0]);
151+
expect(viewportScroller.scrollToPosition).toHaveBeenCalledWith([0, 0], {behavior: 'instant'});
150152
});
151153
});
152154

0 commit comments

Comments
 (0)